Advertisement

trouble with linked lists

Started by August 08, 2002 02:36 PM
4 comments, last by jar 22 years, 1 month ago
i''ve been reading "teach yourself c++ in 21 days" when i came across a section i had trouble understanding. linked lists. i understand the concept (a little bit) but i guess my main problem is trying to implement it. i went to cprogramming.com and made a program similar to it... #include <iostream.h> struct node { int data; node* next; }; int main() { node * head=0; head = new node; head->data=43; node * tail = 0; tail = head; tail->next = new node; tail = tail->next; tail->data = 12; tail->next = new node; tail = tail->next; tail->data = 76; while(tail->next != NULL) { cout << tail->data << endl; tail = tail->next; } return(0); } except this program crashes every time, and i don''t know why! please, could someone explain to me what i''m doing wrong? thank you!
Maybe I'm wrong but you don't seem to be telling head where to go after you create it.

After you create a tail, try this:

head->next = tail;

It also seems that you're just 'round-robin'ing you loop on tail.

ie: tail = tail->next;

You created a new node but you pointed it right back at the old location.

I think I see three 'new node;' but you only have two placeholders for the 'next' address.

[edited by - Like2Byte on August 8, 2002 3:50:44 PM]
- Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
Advertisement
First off, you need to set tail->next = NULL for the last element in your linked list to denote that there are no more elements. In your code example, put tail->next = NULL after the line tail->data = 76. Not setting tail->next = NULL for the last item in the list is causing the crash.
Another problem is that after adding elements to the linked list, tail is already pointing at the last item, so tail->next will be NULL and the code in the while loop will never execute. In order to preserve the tail pointer, I would recommend creating a new pointer variable you can use to traverse the list, and set it to point to the first element in the list.
For example:

      node *elm = head;while (elm != NULL){    cout << elm->data << endl;    elm = elm->next;}      


I don't see any problem with the way you are allocating your nodes. I would probably do it a little different myself, but your way should work just fine.

Now, as a little assignment, write the code to free the nodes. You don't want to leak any memory!

I hope this helps.


[edited by - ShawnO on August 8, 2002 4:12:58 PM]
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
It''s kind of difficult to wad through your code, but I''m sure you meant to put another "tail = head;" before your while loop. While that may actually fix the problem, I doubt it. Working with pointers requires very careful thought.

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Do yourself a favor... at the very least throw in a constructor, i.e.:

struct Node
{
int data;
Node *node;

inline Node(): data(0), node(0) { }
inline Node(int d, Node *n = 0): data(d), node (n) { }
};

The second one will make extending your list a little easier:

Node *tail= new Node(5);
Node *head = new Node(7, tail);
head = new Node(9, head);

Actually, it would be even better to write a decent List class that handles everything or use STL.



Jim Offerman
lead developer
Crevace Games
www.crevace.com
Jim OffermanNixxes Software
Thanks for all the help! i see my mistakes, and now the program works and doesn''t crash! once again, thanks for all your responses!

This topic is closed to new replies.

Advertisement