Well, we can't write your code for you, but I can say that you can look at the method to add to the front and use that as a guide.
As for the delete problem, you use record, current, previous, and nextadd as variables interchangeably (or so it seems to me). What are 'nextadd' and 'record'?
I appreciate your replies and responses but i just want to clarify a little bit. I am a beginner and still struggle with way to write the codes. I have a book that define the algorithm but i cannot implement it. That is why i posted it here.
Secondly, the variables refered to wouldn't have cause any difficulty in understanding if the whole program were there, but it was edited and only the functions part were left. Therefore I will post it again.
I still look up to someone who can help me with the coding part because to the best of my ability that is the far i can reach.
#include <iostream.h> //Creating a dynamic linked list
#include <iomanip.h>
#include <string.h>
#include <stdlib.h>
struct node {
char name[20];
int id_num;
node *nextadd;
};
void insert(node **); //function prototype
void display( node * );
void del(node **); //function prototype
node *list;
main()
{
int i,option;
char ans;
bool cont = true;
//two pointers to structure
//get a pointer to the first structure
list = NULL;
//insert the current structure and create the remaining structures
do {
system("cls");
cout<<"\n\n1 : Add record";
cout<<"\n\n2 : Delete record";
cout<<"\n\n3 : Display record";
cout<<"\n\n4 : Exit";
cout<<"\n\n\nEnter your option: ";
cin>>option;
cin.get();
switch (option) {
case 1: insert(&list);
cont = true;
break;
case 2: del(&list);
cont = true;
break;
case 3: display(list);
cont = true;
break;
case 4: exit(1);
cont = false;
};
}while(true);
}
void insert(node **list) //record is pointer to a structure
{
node *newrec = new node;
cout<<"Enter the a name: "; //inserting infronth of a list
cin.getline(newrec->name,20);
cout<<"Enter the id number: ";
cin>>newrec->id_num;
cin.get();
newrec->nextadd = NULL;
if( list== NULL)
*list = newrec;
else
{
newrec->nextadd = *list;
*list = newrec;
}
}
void display(node *contents)
{
if (contents == NULL)
cout<<"\n\nList is empty"<<endl;
else
{
while(contents != NULL)
{
cout<<setw(30)<<contents->name
<<setw(20)<<contents->id_num<<endl;
contents = contents->nextadd;
}
}
cin.get();
}
void del(node **record)
{
char name[20];
node *current, *previous;
current = *record;
cout<<"\n\nEnter name to delete: ";
cin.getline(name,20);
if ( strcmp(current->name,name) == 0) //if the target is the first node
{
*record = current->nextadd;
delete(current);
}
else
{
while ( current && ( strcmp(current->name, name) != 0 ) )
{
previous = current;
current = current->nextadd;
}
previous->nextadd = current->nextadd;
cout<<"\n\nDeleted";
delete(current);
}
}