By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

adding node from the back in a linked list and deleting a node

P: 4
Hello,

am a new member and this is my first posting. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have written a program in c++ in linked list but am facing two problems. (1) when i delete an item that is not stored the program hangs and (2) I cannot modify the program to insert the node at the back i can only do it to insert at the front. here is the coding for my program

Expand|Select|Wrap|Line Numbers
  1. void del(node **record)
  2. {
  3. char name[20];
  4.  
  5. node *current, *previous;
  6.  
  7. current = *record;
  8.  
  9.  
  10. cout<<"\n\nEnter name to delete: ";
  11. cin.getline(name,20);
  12.  
  13.  
  14. if ( strcmp(current->name,name) == 0) //if the target is the first node
  15. {
  16. *record = current->nextadd;
  17.  
  18. delete(current);
  19. }
  20.  
  21. else
  22. {
  23.  
  24.  
  25. while ( strcmp(current->name,name) != 0)
  26. {
  27. previous = current;
  28. current = current->nextadd;
  29.  
  30. }
  31.  
  32. previous->nextadd = current->nextadd;
  33. cout<<"\n\nDeleted";
  34. delete(current);
  35. }
Mar 22 '07 #1
Share this Question
Share on Google+
4 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hello,

am a new member and this is my first posting. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have written a program in c++ in linked list but am facing two problems. (1) when i delete an item that is not stored the program hangs and (2) I cannot modify the program to insert the node at the back i can only do it to insert at the front. here is the coding for my program
dabbakal-

Glad to hear we've helped, and even more happy to have you with us and contributing! As per our FAQ, I removed the portions of code that were not relevant to your questions. Also, if you could use code tags, it would be appreciated!

2. May I ask you to post what methods you have tried to create for the insertBack(node) ?
Mar 22 '07 #2

P: 4
dabbakal-

Glad to hear we've helped, and even more happy to have you with us and contributing! As per our FAQ, I removed the portions of code that were not relevant to your questions. Also, if you could use code tags, it would be appreciated!

2. May I ask you to post what methods you have tried to create for the insertBack(node) ?
I do not know any other method because am following a text book. As i have said earlier am having limited resources and am just doing this on my own so i was frusturated and discourage when i couldn't solve it. I decided to stop but i thought of this forum. Hope someone will help me make it work so that my interest will not die.
Mar 23 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
I do not know any other method because am following a text book. As i have said earlier am having limited resources and am just doing this on my own so i was frusturated and discourage when i couldn't solve it. I decided to stop but i thought of this forum. Hope someone will help me make it work so that my interest will not die.
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'?
Mar 23 '07 #4

P: 4
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);
}
}
Mar 24 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.