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;   [B]//this should make temp=NULL eventually
[/B]       }
  }
[B]while (temp != NULL);  //Problem here!!![/B]
 
}

and these are pointers of class CD

CD *start_ptr ;
CD *temp, *temp2,*temp3; // Temporary pointers
CD *next;

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)

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

}

I suppose you linked the list faulty...
Can you please post the whole code here???(with your class defination)

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

class DVD 
{ 
private: 
string title, actor;

string *ptr;
int num;

DVD *start_ptr ; 
DVD *temp, *temp2,*temp3; // Temporary pointers
DVD *current;

 
DVD *next;
public: 
void create_dvd();  // sets names of actors to the DVD 
void list();   // sets title of the DVD 

   
}; 
 
void DVD::create_dvd () {
 
start_ptr = NULL;
temp = new DVD;
 cout<<"How many movies would you like to enter? ";
 cin>> num;
 cin.ignore();
 //ptr = new string[num];

    
DVD *current = start_ptr; //A working pointer into the linked list.
    
    
 
  if ( ptr == 0 )
    cout << "Error: memory could not be allocated";
  else 
  {
    for ( int a = 0; a < num; a++ ) 
 {
 cout << "\nPlease enter the title of the DVD : "; 
 cin >> temp->title; 

 temp->next = NULL; 
 // Set up link to this node 
  if (current == NULL) 
  {start_ptr=temp;}
    else 
    {  
  while (current->next ) { current = current->next; } 
  current->next = temp; 
    }
  
 }
  }
  
    return; 
 
}
 

void DVD::list()
{

DVD *current=start_ptr;
 
 
while (current); 
       { 
  cout<<"Title: "<<current->title<<endl;
           // Move to next node (if present)
          current=current->next;
       }
cout << "End of list!" << endl;
    return; 
  
 
}

Remove the semicolon here:

void DVD::list()
{
 
DVD *current=start_ptr;
 
 
while (current)[B];[/B]
       {

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:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.