simple linked list problem

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

simple linked list problem

 
0
  #1
May 2nd, 2008
I have a small problem with adding some items to my linked list....the code compiles with no errors but when run it doesnt complete...it looks like it is still waiting for some input but i dont know where my loops are going wrong..

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct details
  5. {
  6.  
  7. int prod_id;
  8. //int prod_code;
  9. details *link;
  10. };
  11.  
  12. typedef details* detailsPtr;
  13. const int S=3;
  14. void insert_head(detailsPtr& head, int id);
  15. void insert_end();
  16. void delete_head();
  17. void delete_end();
  18. void display(detailsPtr& head,int id);
  19.  
  20. int main()
  21. {
  22. int id, i;
  23.  
  24. detailsPtr head;
  25. int j=1;
  26. for (i=0;i<S;i++)
  27. {
  28. cout<<"Enter product ID "<<j<<": ";
  29.  
  30. cin>>id;
  31. insert_head(head,id);
  32. j++;
  33. // cout<<endl;
  34.  
  35.  
  36. }
  37.  
  38. display(head, id);
  39.  
  40. return 0;
  41.  
  42. }
  43.  
  44. void insert_head(detailsPtr& head, int id)
  45. {
  46.  
  47. detailsPtr tmp_ptr;
  48.  
  49. tmp_ptr = new details;
  50.  
  51. tmp_ptr ->prod_id = id;
  52.  
  53. tmp_ptr -> link = head;
  54.  
  55. head = tmp_ptr;
  56.  
  57. }
  58.  
  59. void display(detailsPtr& head,int id)
  60. {
  61. detailsPtr disp;
  62.  
  63. disp = head;
  64. while(disp!=NULL)
  65. {
  66.  
  67. cout << "ID: ";
  68.  
  69. cout << disp->prod_id<<endl;
  70.  
  71. disp=disp->link;
  72. }
  73. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: simple linked list problem

 
0
  #2
May 2nd, 2008
>>detailsPtr head;
That is an uninitialized pointer. Set it like this and everything should work.
detailsPtr head = NULL;
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: simple linked list problem

 
0
  #3
May 2nd, 2008
oh....didnt notice that...works well now..thanx
Last edited by joshmo; May 2nd, 2008 at 10:57 am.
Reply With Quote Quick reply to this message  
Reply

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




Views: 494 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC