943,942 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1386
  • C++ RSS
Nov 11th, 2007
0

deleting and displaying in a linked list

Expand Post »
i am making a menu driven program on linked lists.
the program's display function is printing garbage values.
i.e if n=3...i add three values and when the values are displayed the last entry is displayed and the rest two enteries are garbage values
for eg.

Quote ...
MENU (Linked List)

1. CREATE
2. INSERT
3. DELETE
4. DISPLAY
5. EXIT

Enter Your Choice: 1

Enter The Number Of Members You Want To Add: 3

Enter Name: sbi
Enter Roll: 44

Enter Name: chi
Enter Roll: 65

Enter Name: csc
Enter Roll: 11
Quote ...
MENU (Linked List)

1. CREATE
2. INSERT
3. DELETE
4. DISPLAY
5. EXIT

Enter Your Choice: 4

Current List:
NAME: csc
ROLL: 11

NAME: ☻∙├H.;♠╜▲├QR♠3╔.ï0►▲↓
ROLL: -29906

NAME: ┤H═!;┌r♂┤H═!r♣ASP
ROLL: 20563
and when choice 3 is used the program doesnt come to an end...
i will be gratefull if somebody helps me out...

c++ Syntax (Toggle Plain Text)
  1. struct student
  2. {
  3. char name[15];
  4. int roll;
  5. student*next;
  6. };
  7.  
  8. void display(student*first)
  9. {
  10. student*ptr;
  11. ptr=first;
  12. cout<<"\nCurrent List:\n";
  13. while(ptr!=NULL)
  14. {
  15. cout<<"NAME: "<<ptr->name<<"\nROLL: "<<ptr->roll<<"\n\n";
  16. ptr=ptr->next;
  17. }
  18. }
  19.  
  20. void delet(student*first, int val)
  21. {
  22. student*ptr,*list;
  23. ptr=first;
  24. while(ptr!=NULL)
  25. {
  26. if(ptr->roll==val)
  27. {
  28. list=ptr;
  29. ptr=ptr->next;
  30. delete list;
  31. }
  32. }
  33. }
  34.  
  35.  
  36. void main()
  37. {
  38. student*first, *last, *x;
  39. first=last=NULL;
  40. int c=0, val, choice, n, i;
  41. char ch='y';
  42.  
  43. while (c==0)
  44. {
  45. clrscr();
  46. **menu**
  47. cout<<"\n\nEnter Your Choice: ";
  48. cin>>choice;
  49.  
  50. switch (choice)
  51. {
  52. case 1:cout<<"\nEnter The Number Of Members You Want To Add: ";//add
  53. cin>>n;
  54. first=new student;
  55. cout<<"\nEnter Name: ";
  56. gets(first->name);
  57. cout<<"Enter Roll: ";
  58. cin>>first->roll;
  59. first->next=NULL;
  60. last=first;
  61. for(i=1; i<n; i++)
  62. {
  63. x=new student;
  64. cout<<"\nEnter Name: ";
  65. gets(first->name);
  66. cout<<"Enter Roll: ";
  67. cin>>first->roll;
  68. x->next=NULL;
  69. last->next=x;
  70. last=x;
  71. }
  72. break;
  73.  
  74. case 3:do//delete
  75. {
  76. if(first!=NULL)
  77. {
  78. cout<<"\nEnter The Roll: ";
  79. cin>>val;
  80. delet(first, val);
  81. display(first);
  82. }
  83. else
  84. {
  85. cout<<"\nLIST EMPTY!!!";
  86. getch();
  87. break;
  88. }
  89. cout<<"\n\nDo you want to continue (y/n)?\n";
  90. cin>>ch;
  91. }while(ch=='y'||ch=='Y');
  92. break;
  93.  
  94. case 4:if (first!=NULL)//display
  95. {
  96. display(first);
  97. }
  98. else
  99. {
  100. cout<<"\nLIST EMPTY!!!";
  101. }
  102. getch();
  103. break;
  104.  
  105. **exit and default**
  106. }
  107. }
  108. }
Similar Threads
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007
Nov 11th, 2007
0

Re: deleting and displaying in a linked list

Er, before anything else, you really need to watch your I/O. Don't mix C++ and C.
Find every getch() (non-standard evil!) and replace it with cin.get();. Find every gets(s); and replace it with getline( cin, s );.

Also, I know everyone teaches and tutorials and etc. this, but try not to use cin>>myint; when getting user input. Better to do what the user expects and head-off any input problems by getting a temporary string and converting it:
C++ Syntax (Toggle Plain Text)
  1. #include <sstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6. string s;
  7. int i;
  8.  
  9. cout << "Please enter an integer> ";
  10. getline( cin, s );
  11.  
  12. if (!(stringstream( s ) >> i))
  13. cout << "That wasn't an integer!\n";
  14. else
  15. cout << "Thank you\n";
  16.  
  17. return EXIT_SUCCESS;
  18. }
This code is very forgiving about input errors (gets a number whenever it can) and never leaves the input stream messed up.


OK, your linked list. You add a first for the first, then you add an x for every additional. However, you are still modifying first, not x in your input statements. Notice that when you display() the list the last one you entered is displayed properly, but all the others are garbage?

Also, you have memory leaks. What if the user chooses '1' twice? You should initialize first to NULL before anything else in your program. Then check it at the case 1 to see if it is still NULL. If not, you need to either delete the whole list, or add, or give the user a choice, or something.

Well, this has gotten long and I haven't looked at delete yet.

Hope this helps.

[EDIT] Oh yeah, you should either use a string in the student struct for the name or verify that your user doesn't try to give you a name that is too long. Never use gets().
Last edited by Duoas; Nov 11th, 2007 at 12:48 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 11th, 2007
0

Re: deleting and displaying in a linked list

Eh, sorry for the double post. Here're your problems (yes, more than one) with delet().

Line 24: ptr never becomes NULL unless you are trying to delete the last element.

However, that leaves a list like this
[1] points to [2]
[2] points to [3]
[3] was deleted!
That's a segmentation fault waiting to happen.

I want you to think about this one a bit instead of just giving you the answer. Please do as I ask and get out a piece of paper and pencil and draw yourself a little linked list, full of arrows and everything, and work your way through removing/deleting an element cleanly. Once you understand how to do it on paper you'll be able to tell the computer how to do it with ease.

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 11th, 2007
0

Re: deleting and displaying in a linked list

i cant stop using getch() and gets()...although i have known that these are non- standard and know the correct ones...since its a school assignment and we have not been taught anything else than what i am using...
sorry for this...
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007
Nov 11th, 2007
0

Re: deleting and displaying in a linked list

and yes i am trying to do what you asked me to do for delet function...it will take some time..so will post my somewhat corrected code after few minutes...hoping you are online till then to help me further..
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007
Nov 11th, 2007
0

Re: deleting and displaying in a linked list

chhanged line 65 and 67 to gets(x->name);
and cin>>x->roll; respectively...
now display function working fine...
still working on delet function...
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007
Nov 11th, 2007
0

Re: deleting and displaying in a linked list

i modified the delet function...now its working fine except when the element to be deleted is first member...

c++ Syntax (Toggle Plain Text)
  1. void delet(student*first, int val)
  2. {
  3. student*ptr,*list,*back;
  4. if(first->roll==val)
  5. {
  6. ptr=first;
  7. first=first->next;
  8. delete ptr;
  9. }
  10. back=first;
  11. ptr=ptr->next;
  12. while(ptr!=NULL)
  13. {
  14. if(ptr->roll==val)
  15. {
  16. list=ptr;
  17. back->next=ptr->next;
  18. delete list;
  19. }
  20. back=ptr;
  21. ptr=ptr->next;
  22. }
  23. }

sample input:
Quote ...
MENU (Linked List)

1. CREATE
2. INSERT
3. DELETE
4. DISPLAY
5. EXIT

Enter Your Choice: 1

Enter The Number Of Members You Want To Add: 3

Enter Name: chi
Enter Roll: 13

Enter Name: rou
Enter Roll: 45

Enter Name: csc
Enter Roll: 24
output on deleting:

Quote ...
1. CREATE
2. INSERT
3. DELETE
4. DISPLAY
5. EXIT

Enter Your Choice: 3

Enter The Roll: 13

Current List:

NAME: ■☼■☼╔.ï▬╜▲B╗  ┤
ROLL: 13

NAME: rou
ROLL: 45

NAME: csc
ROLL: 24



Do you want to continue (y/n)?
n
if the member to be deleted is not the first one...then it deletes the element successfully...
i rechecked it but cant find the problem...!!
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007
Nov 11th, 2007
0

Re: deleting and displaying in a linked list

Your drawing should have a little arrow pointing from a variable named first in the main() function to the beginning of your list. ;-)

I like your new avatar, btw.
Last edited by Duoas; Nov 11th, 2007 at 4:49 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 12th, 2007
0

Re: deleting and displaying in a linked list

changed the return type of delet function from void to student*...
and returned first at the end...
now its working fine...yippie...
i am working on that memory leaks...will prompt user to delete the entire list first through delete option...

thankx a lot...
Last edited by tracethepath; Nov 12th, 2007 at 12:38 am.
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ graphics
Next Thread in C++ Forum Timeline: 8 days and still at the same point





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC