| | |
help with linked list
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2007
Posts: 16
Reputation:
Solved Threads: 0
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
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!!!
} 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.
•
•
Join Date: May 2007
Posts: 16
Reputation:
Solved Threads: 0
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!!!
}
•
•
Join Date: May 2007
Posts: 16
Reputation:
Solved Threads: 0
wel its a bit messy, stilraw. but here we are:
C++ Syntax (Toggle Plain Text)
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);
{ 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.
All my posts may be freely redistributed under the terms of the MIT license.
The other reasons you're experiencing difficulties:
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
Finally, create a destructor that deletes all the nodes, or else you'll end up with a big memory leak.
- 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
temponce. You're going to have to allocate memory each time you loop.
C++ Syntax (Toggle Plain Text)
DVD() : num(0), next(0) {};
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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C++ Forum
- Previous Thread: Move up/down in a tree
- Next Thread: I'm New, Sorry...Please Help
Views: 1196 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






