Linked Lists Basic Help Needed

This is a discussion on Linked Lists Basic Help Needed within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I'm trying to learn/understand linked lists so I wrote this program just to see if I could do ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Linked Lists Basic Help Needed

    Hey guys, I'm trying to learn/understand linked lists so I wrote this program just to see if I could do it, and to try and understand the process better etc. It compiles fine but when I read it I get memory access violation errors and stuff. Could someone please tell me what's going on. Basically I just wanted it to print 1-29 or whatever, and it would create 30 structure nodes, and each printed digit would be from each seperate node. Here is the code!

    Code:
    #include <iostream>
     
    int main()
    {
    	int i = 0;
    	struct node {
    		int x;
    		node *next;
    	};
    	node *root;
    	node *conductor;
    	root = new node;
    	root->x = i;
    	root->next = new node;
    	conductor = root->next;
    	if (conductor != 0) 
    	{
    		while (conductor != 0 && i < 30)
    		{
    			conductor->x = i++;
    			std::cout<<conductor->x;
    			conductor = conductor->next;
    		}
    	}
    	std::cin.get();
    	return 0;
    }
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    1. You need a 'new' inside the loop, for each node that you add.
    2. You need to append each node to the end of the list (and update your end of list pointer).

    So
    - A pointer to the head (initially NULL). This is assigned ONCE when you create the first node.
    - A pointer to the tail (initially NULL). This is updated whenever you append a node.

    Also, vitally important to to make sure that tail->next is always NULL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by Salem View Post
    1. You need a 'new' inside the loop, for each node that you add.
    2. You need to append each node to the end of the list (and update your end of list pointer).

    So
    - A pointer to the head (initially NULL). This is assigned ONCE when you create the first node.
    - A pointer to the tail (initially NULL). This is updated whenever you append a node.

    Also, vitally important to to make sure that tail->next is always NULL.

    Isn't that the node *root I have? Or am I supposed to create another pointer that points to the first node aswell? And with the "A pointer to the tail" how do I know where the tail is going to be, aka how many nodes there is going to be? Hmmm.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    The whole point of linked lists is that you've no idea in advance how many you need.
    So they're allocated on demand (ie, INSIDE your loop), and chained together (aka LINKED).

    The ->next pointer of the old node points to the new node, and so on as more node are added.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    yeah I get you you said "A pointer to the tail" though, so didn't you mean a pointer to the end node? If so, how do you know where that is :O did you mean create a pointer then assign it to the end node once you know how many there is or?
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    No, I mean something like this

    tail->next = newNode; // add a node to the end of the list
    tail = newNode; // now it's the end of the list

    You can call it whatever you want, I just call them "head" and "tail".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 12:01 AM
  2. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  3. Question On Linked Lists
    By Zildjian in forum C Programming
    Replies: 8
    Last Post: 10-23-2003, 11:57 AM
  4. Replies: 1
    Last Post: 03-21-2002, 05:10 PM
  5. Dynamic number of linked lists
    By foniks munkee in forum C Programming
    Replies: 2
    Last Post: 02-24-2002, 08:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21