help with linked list

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

Join Date: May 2007
Posts: 16
Reputation: machine is an unknown quantity at this point 
Solved Threads: 0
machine machine is offline Offline
Newbie Poster

help with linked list

 
0
  #1
May 20th, 2007
Hi
i have to use linked lists to get information from the user and dispaly it. I am having trouble with displaying the linked list. If you look at my second function where i am trying to list the information, you might be able to tell what the problem is? because when i run it, temp is never equal to 0 and the loop keeps running. The temp should equal to 0 when it reaches the empty part of the list.
Please help. Thanks


 
void CD::create_cd () {
start_ptr = NULL;
temp = new DVD;

 cout << "\nPlease enter the title of the CD: "; 
 cin >> temp->title; 
 
 temp->next = NULL; 
 // Set up link to this node 
  if (start_ptr == NULL) 
  {start_ptr = temp;}
    else 
    {temp2 = start_ptr; 
  while (temp2->next != NULL) { temp2 = temp2->next; } 
  temp2->next = temp; }
 
}
 

void CD::list()
{
do 
  {  if (temp == NULL)
       cout << "End of list" << endl;
     else
       { 
  cout<<"Title: "<<temp->title<<endl;

          // Move to next node (if present)
          temp = temp->next;   //this should make temp=NULL eventually
       }
  }
while (temp != NULL);  //Problem here!!!
 
}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 16
Reputation: machine is an unknown quantity at this point 
Solved Threads: 0
machine machine is offline Offline
Newbie Poster

Re: help with linked list

 
0
  #2
May 20th, 2007
and these are pointers of class CD

CD *start_ptr ;
CD *temp, *temp2,*temp3; // Temporary pointers
CD *next;
Last edited by machine; May 20th, 2007 at 2:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: help with linked list

 
0
  #3
May 20th, 2007
Be sure temp = start_ptr at the beginning of the CD :: list() function and write the if(temp == NULL) control before the loop otherwise the program always compares the temp == NULL or not.But you're already doing this control with the do-while's condition (temp != NULL)
Last edited by FoX_; May 20th, 2007 at 4:50 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 16
Reputation: machine is an unknown quantity at this point 
Solved Threads: 0
machine machine is offline Offline
Newbie Poster

Re: help with linked list

 
0
  #4
May 20th, 2007
thanks for the reply. i tried adding temp=start_ptr; but doesnt work.

void CD::list()
{
temp=start_ptr;
do
{ if (temp == NULL)
cout << "End of list" << endl;
else
{
cout<<"Title: "<<temp->title<<endl;

// Move to next node (if present)
temp = temp->next; //this should make temp=NULL eventually
}
}
while (temp != NULL); //Problem here!!!

}
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: help with linked list

 
0
  #5
May 20th, 2007
I suppose you linked the list faulty...
Can you please post the whole code here???(with your class defination)
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 16
Reputation: machine is an unknown quantity at this point 
Solved Threads: 0
machine machine is offline Offline
Newbie Poster

Re: help with linked list

 
0
  #6
May 20th, 2007
wel its a bit messy, stilraw. but here we are:

  1. class DVD
  2. {
  3. private:
  4. string title, actor;
  5.  
  6. string *ptr;
  7. int num;
  8.  
  9. DVD *start_ptr ;
  10. DVD *temp, *temp2,*temp3; // Temporary pointers
  11. DVD *current;
  12.  
  13.  
  14. DVD *next;
  15. public:
  16. void create_dvd(); // sets names of actors to the DVD
  17. void list(); // sets title of the DVD
  18.  
  19.  
  20. };
  21.  
  22. void DVD::create_dvd () {
  23.  
  24. start_ptr = NULL;
  25. temp = new DVD;
  26. cout<<"How many movies would you like to enter? ";
  27. cin>> num;
  28. cin.ignore();
  29. //ptr = new string[num];
  30.  
  31.  
  32. DVD *current = start_ptr; //A working pointer into the linked list.
  33.  
  34.  
  35.  
  36. if ( ptr == 0 )
  37. cout << "Error: memory could not be allocated";
  38. else
  39. {
  40. for ( int a = 0; a < num; a++ )
  41. {
  42. cout << "\nPlease enter the title of the DVD : ";
  43. cin >> temp->title;
  44.  
  45. temp->next = NULL;
  46. // Set up link to this node
  47. if (current == NULL)
  48. {start_ptr=temp;}
  49. else
  50. {
  51. while (current->next ) { current = current->next; }
  52. current->next = temp;
  53. }
  54.  
  55. }
  56. }
  57.  
  58. return;
  59.  
  60. }
  61.  
  62.  
  63. void DVD::list()
  64. {
  65.  
  66. DVD *current=start_ptr;
  67.  
  68.  
  69. while (current);
  70. {
  71. cout<<"Title: "<<current->title<<endl;
  72. // Move to next node (if present)
  73. current=current->next;
  74. }
  75. cout << "End of list!" << endl;
  76. return;
  77.  
  78.  
  79. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with linked list

 
0
  #7
May 20th, 2007
Remove the semicolon here:
void DVD::list()
{
 
DVD *current=start_ptr;
 
 
while (current);
       {
Last edited by cscgal; May 22nd, 2007 at 2:30 am. Reason: Color fixed
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with linked list

 
0
  #8
May 20th, 2007
The other reasons you're experiencing difficulties:
  • You have a lot of useless variables. For example, you declare 'current' as a data member of the class, yet in both member functions you create a local variable named 'current'. 'start_ptr' is never needed, so get rid of it.
  • When you're creating the linked list, you only allocate memory for temp once. You're going to have to allocate memory each time you loop.
To make coding easier, create a constructor and destructor. I would make your constructor like this:
  1. DVD() : num(0), next(0) {};
Do you see what I'm doing here? It's initializing those important pointers/variables to 0 (or NULL), so that you don't have worry about them in the rest of your code. Every node that you create will already have next initialized to 0.

Finally, create a destructor that deletes all the nodes, or else you'll end up with a big memory leak.
Last edited by John A; May 20th, 2007 at 8:18 pm.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1196 | Replies: 7
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC