Linked List

This is a discussion on Linked List within the C++ Programming forums, part of the General Programming Boards category; I create a new element of a struct inside a function and then send a pointer to that new element ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    87

    Linked List

    I create a new element of a struct inside a function and then send a pointer to that new element into another function where it sets that equal to the head and links the previous link of the head to the link of the new element. When it comes back from the function where it does that the head is set back to NULL though.. can any of you tell me why???

    Code:
    // Function Definitions
    ListType *InitializeList(...);
    void AddToList(ListType * New, ListType * Head);
    
    // Functions Call Sets back to Zero after returning from here
    AddToList(InitializeList(name, size, Bubble, Selection, Insertion), Head)
    
    // Initializes the list, ... is omitted code for putting the data into the list
    ListType *InitializeList(...)
    {
    	ListType * This = new ListType;
    
                    ...
    
    	This->Next = NULL;
    
    	return This;
    }
    
    // Adds a new element to the list
    void AddToList(ListType * New, ListType * Head)
    {
    	ListType * temp = Head;
    	Head = New;
    	Head->Next = temp;
    }
    if someone can help me out here i would be very grateful =]

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    To make a change to the pointer itself and not the memory it currently points to, you need to use a reference or a pointer to the pointer when you pass it to a function.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    Hmm ok but i do wanna change the memory that head is pointing to, i wanna change it to the memory used by the new ListType that was initialized in InitializeList. Basically the problem is that when i get back to main head points back to memory NULL what it was initialized to, not to the memory of New, even though it does indeed point to that memory when AddToList is running.
    Last edited by DirX; 10-29-2003 at 12:30 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    struct Node
    {
       int id;
       Node *next;
    };
    
    struct List
    {
       Node * head;
       Node * current;
       void addToHeadOfList(int);
       
    };
    
    void List::addToHeadOfList(int _id)
    {
       //declare a new Node
       current = new Node;
    
       //assign the desired value to the new Nodes data member
       current->id = _id;
    
       //assign head to current Node internal pointer
       current->next = head;
       
       //head is now second Node in List so
       //make new Node the head node by assignment
       head = current;
    
       //the address of the second Node in the list no longer in head
    }
    Bare bones code to add a new link (aka node) to head of a list.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    hmm ok well first of all i might have explained this wrong but the way i want this is head->new element->old element or null....

    second of all i think the problem is with the functions and the scopes they are in, i think i am passing the variables around the wrong way or something but i can;t quite figure out what i did wrong... perhaps if you can look at the code posted above and somewhat guide me that would be great

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    You are missing twm's point. Run the following code as a "tutorial"
    Code:
    void foo1(int val)
    {
        val = 5;
    }
    
    void foo2(char *p)
    {
        p = (char*)5;
    }
    
    void foo3(char **p)
    {
        *p = (char*)5;
    }
    
    int main()
    {
        int val = 0;
    
        foo1(val);
        cout << "val = " << val << endl;
    
        char *p = 0;
    
        foo2(p);
        cout << "p = " << (int)p << endl;
    
        foo3(&p);
        cout << "p = " << (int)p << endl;
    
        return 0;
    }
    gg

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the code I posted produces exactly what you want. When you declare a List in main() (or whereever) head will be undefined. When you add the first int (say it is 44) to the list it will be placed in head. When you add the second int to the list (say it is 21) it will be placed in front of the first so if printed out it will print as:

    21
    44

    Then if you add 1011 to the list and print it out it will be:

    1011
    21
    44

    etc. Some people have the head pointer to be an administrative node only, containing no data members. That's doable too if you want.

    Your code uses C style list, without encapsulating it in a class. Here's my code using your syntax, the best I can, simplifying it to the essentials. I've not compiled it so it may be buggy, though the syntax should be okay.
    Code:
    struct ListType
    {
      int id;
      ListType * Next;
    };
    
    // Function Declarations
    ListType * InitializeList(int);
    void AddToList(ListType *, ListType *);
    
    int main()
    {
       //declare Head/List, whatever you want to call it now
       ListType * Head;
       Head = NULL;
       
       int i = 44;
      // Function call
      AddToList(InitializeList(i), Head);
      
       i = 21;
       AddToList(IntializeList(i), Head);
      
       i = 1011;
       AddToList(IntializeList(i), Head);
    
       for(i = 0; i < 3; ++i)
       {
          cout << Head->id << endl;
          Head = Head->Next;
       }
    
         ListType * temp;
    
        for(i = 0; i < 3; ++i)
       {
          temp = Head;
          Head = Head->Next;
          delete temp;
       }
    
    }
    
    //function definitions
    ListType *InitializeListType(int _id)
    {
       ListType * This = new ListType;
       
       This->id = _id;
    
      This->Next = NULL;
    
      return This;
    }
    
    // Adds a new element to the list
    void AddToList(ListType * New, ListType * Head)
    {
        if(Head == NULL)
          Head = New;
        else
        { 
           New->Next = Head;
           Head = New;
        }
    }

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    I'm disappointed in you elad.....
    Code:
        ListType *Head;
        Head = NULL;
       
        int i = 44;
        AddToList(InitializeList(i), Head);
        //Head is still NULL
      
        i = 21;
        AddToList(InitializeList(i), Head);
        //Head is still NULL
      
        i = 1011;
        AddToList(InitializeList(i), Head);
        //Head is still NULL
    Run my "tutorial" above.

    gg

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    CodePlug:

    Dealing with customers here at work means other posts are often made between the time I start composing my response and the time I'm able to post it. I didn't mean to contradict you. In fact if I'd have seen your post I probably wouldn't have posted until I did have the chance to compile mine. Unfortunately, that will have to wait 'til I get home to use my compiler. I've tried to work through your code but with the casts and pointer to pointers etc. and with customers coming and going I just can't get enough time to do it justice at this point. I'm not averse to admitting my mistakes when they happen, and they happen more often than I'd like, so I've had lots of practice. Let ya' know later.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >// Adds a new element to the list
    >void AddToList(ListType * New, ListType * Head)

    As Twm and Codeplug explained, change Head to a reference:
    void AddToList(ListType * New, ListType * &Head)
    Last edited by swoopy; 10-29-2003 at 04:00 PM.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    Ok all thanks for you help, guess i need to read up on pointers and reference's again been a while since i used em this intense. Elad thnx for putting it in plain english =]

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    After compiling my code and Codeplug's tutorial I admit I was making the same mistake in my C style version that you were making and that the code does work with the corrections recommended by twm, Codeplug, and others.

    If you haven't figured out how to put the recommedations by twm and Codeplug to work for you post again and I'll post my corrected version.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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