problem with singly linked list

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 12
Reputation: xeption12 is an unknown quantity at this point 
Solved Threads: 0
xeption12 xeption12 is offline Offline
Newbie Poster

problem with singly linked list

 
0
  #1
Dec 22nd, 2007
Ok i'm trying to build a header file which then i can include it for programs that use singly linked lists but somthing is fishy here...:

here's the code
  1. #include <iostream.h>
  2.  
  3. struct nod
  4. {
  5. int info;
  6. nod* next_adr;};
  7.  
  8. void add(nod* &v, nod* &sf, int val)
  9. {
  10. nod *c;
  11. if(v==0) //if list empty
  12. {
  13. v=new nod;
  14. v->info=val;
  15. v->next_adr=0;
  16. sf=v;
  17. }
  18. else
  19. {
  20. c=new nod;
  21. sf->next_adr=c;
  22. c->info=val;
  23. c->next_adr=0;
  24. sf=c;
  25. }
  26. }
  27.  
  28. void insert_after(nod* v, nod*& sf, int val, int val1)
  29. {
  30. nod* c=v,*d;
  31. while(c->info!=val) c=c->next_adr;
  32. d=new nod;
  33. d->info=val1; //the member _info_ of the node represented by _d_ takes the value of _val1_
  34. d->next_adr=c->next_adr;
  35. if(d->next_adr==0) sf=d;
  36. }
  37.  
  38. void insert_before(nod* &v, int val, int val1)
  39. {
  40. nod* c,* d;
  41. if(v->info==val)
  42. {
  43. d=new nod;
  44. d->info=val1;
  45. d->next_adr=v;
  46. v=d;
  47. }
  48. else
  49. {
  50. c=v;
  51. while(c->next_adr->info!=val)
  52. c=c->next_adr;
  53. d=new nod;
  54. d->info=val1;
  55. d->next_adr=c->next_adr;
  56. c->next_adr=d;
  57. }
  58. }
  59.  
  60. void _delete(nod* &v, nod* &sf, int val)
  61. {
  62. nod *c,*man;
  63. if(v->info==val)
  64. {
  65. man=v;
  66. v=v->next_adr;
  67. }
  68. else
  69. {
  70. c=v;
  71. while(c->next_adr->info!=val) c=c->next_adr;
  72. man=c->next_adr;
  73. c->next_adr=man->next_adr;
  74. if(man==sf) sf=c;
  75. }
  76. delete man;
  77. }
  78.  
  79. void list_it(nod* v)
  80. {
  81. nod* c=v;
  82. while(c)
  83. {
  84. cout<<c->info<<endl;
  85. c=c->next_adr;
  86. }
  87. cout<<endl;
  88. }

and here's the program which tests these functions:
  1. #include <liste.h>
  2. #include <conio.h>
  3.  
  4. nod* v,* sf;
  5.  
  6. void main()
  7. {
  8. clrscr();
  9.  
  10. int i;
  11. for(i=1;i<=10;i++) add(v,sf,i);
  12. list_it(v);
  13.  
  14. insert_after(v,sf,7,11);
  15. insert_after(v,sf,10,12);
  16. insert_after(v,sf,1,13);
  17. list_it(v);
  18.  
  19. /// PROBLEM: There are no changes made
  20.  
  21. insert_before(v,13,14);
  22. insert_before(v,1,15);
  23. list_it(v);
  24.  
  25. /// PROBLEM: Program enters in an infinite loop and when breaked points me to "while(c->next_adr->info!=val)" @ insert_before() function in the header file
  26. _delete(v,sf,15);
  27. _delete(v,sf,13);
  28. _delete(v,sf,12);
  29. list_it(v);
  30.  
  31. /// PROBLEM: same problem as before except this time points me to the while in the _delete() function
  32. getch();
  33. }
Last edited by xeption12; Dec 22nd, 2007 at 10:19 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 12
Reputation: xeption12 is an unknown quantity at this point 
Solved Threads: 0
xeption12 xeption12 is offline Offline
Newbie Poster

Re: problem with singly linked list

 
0
  #2
Dec 22nd, 2007
solved the bug was in insert_after() ... i forgot to link the node that has the value val to the new node...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC