944,164 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1371
  • C++ RSS
May 20th, 2007
0

help with linked list

Expand Post »
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!!!
 
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
machine is offline Offline
16 posts
since May 2007
May 20th, 2007
0

Re: help with linked list

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
machine is offline Offline
16 posts
since May 2007
May 20th, 2007
0

Re: help with linked list

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.
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
FoX_ is offline Offline
53 posts
since Mar 2007
May 20th, 2007
0

Re: help with linked list

thanks for the reply. i tried adding temp=start_ptr; but doesnt work.

Quote ...
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!!!

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
machine is offline Offline
16 posts
since May 2007
May 20th, 2007
0

Re: help with linked list

I suppose you linked the list faulty...
Can you please post the whole code here???(with your class defination)
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
FoX_ is offline Offline
53 posts
since Mar 2007
May 20th, 2007
0

Re: help with linked list

wel its a bit messy, stilraw. but here we are:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
machine is offline Offline
16 posts
since May 2007
May 20th, 2007
0

Re: help with linked list

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
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
May 20th, 2007
0

Re: help with linked list

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:
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

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: Move up/down in a tree
Next Thread in C++ Forum Timeline: I'm New, Sorry...Please Help





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


Follow us on Twitter


© 2011 DaniWeb® LLC