Thread: question about a working linked list

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    17

    question about a working linked list

    Hi, this program is the solution to an excercise I found on a book...I wrote it reading tutorials and stuff...my problem is... as you can see in the CreateList function...I have no secondary node pointing to nothing....I have read that you need a second pointer because the first node is unchangeable and the second one is the one that's going to move in the list....but in this code I don't use that second node...so..why does this works correctly? does it have some kind of error? I can't figure it out because I'm kind of new in this kind of programming technique and I have never used linked list before...

    can you please tell what am I doing wrong? thanks..

    Edit:Sorry that I didn't format my code...I don't know HTML or how to place code...I did read the post about using code tags, but it doesn't specify what code one should use to insert tabs or spaces...

    Edit:Ok forget it... I just found out how to do it :P hahaha

    Code:
    #include <iostream>
    using namespace std;
    
    struct linkedList {
      int obj_number;  //Numbers the current list
      linkedList* Next;
    };
    
    void CreateList( linkedList* anyList, int nodes ){
    
      for(int i = 0; i < nodes; i++){
        anyList->obj_number = i + 1;  //put the number of the current node
        anyList->Next = new linkedList;  //creates a new node at the end of the list
        anyList = anyList->Next;  //moves to this new node
        anyList->Next = 0;  
      }
      
    }
    
    void DisplayList( linkedList* anyList ){
    
      while( anyList->Next != NULL ){
        cout << anyList->obj_number << endl;
        anyList = anyList->Next;
      }
    
    }
    
    int main(){
      linkedList* myList;
    
      myList = new linkedList;
      CreateList( myList, 5 );
      DisplayList( myList );
      cin.get();
      delete myList;
    }
    Last edited by cold_dog; 09-01-2006 at 12:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM