Linked list

This is a discussion on Linked list within the C Programming forums, part of the General Programming Boards category; Hi, I was following the linked list tutorial on this site. I have the code here, but I am not ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    12

    Linked list

    Hi,

    I was following the linked list tutorial on this site. I have the code here, but I am not sure if I am doing it right. Could someone tell me if I am on the right track, or is there a way I could do it better ?

    Thanks in advance.

    Code:
    #include <cstdio>
    #include <iostream>
    
    using namespace std;
    
    struct Node {
        int x;
        Node *next;
    };
    
    void AddNode(Node *node, int value)
    {
        if(node != NULL)
        {
            while(node->next != NULL)
            {
                node = node->next;
            }
        }
        
        node->next = new Node;
        node = node->next;
        node->next = NULL;
        node->x = value;
    }    
        
    void PrintValue(Node *node)
    {
        do 
        {
            cout << node->x << endl;
            node = node->next;
        }while(node != NULL);
    }
            
    int main()
    {
        Node *root = new Node;
        root->next = NULL;
        root->x = 5;
            
        AddNode(root, 10);
        AddNode(root, 30);
        AddNode(root, 50);
     
        PrintValue(root);   
        
        getchar();
        return 0;
    }
    Life is a piece of chocolates

  2. #2
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    I'd suggest you post to the C++ forum.
    R.I.P C89

  3. #3
    Registered User Ephraim's Avatar
    Join Date
    Feb 2004
    Posts
    8
    In the first call of AddNode you will loose the pointer
    to your root.
    You should use a const Node in AddNode param
    and make a copy of it.
    and then iterate until the end.

    Code:
    void AddNode(const Node *root, int value)
    {
        if(root != NULL)
        {
            Node* node = root;
            while(node->next != NULL)
            {
                node = node->next;
            }
        }
        
        node->next = new Node;
        node = node->next;
        node->next = NULL;
        node->x = value;
    }
    Do this in PrintValue also else you will have
    memory leaks and you will never get back to
    your root node !!!!

    Ciao Ephraim
    Last edited by Ephraim; 03-04-2004 at 02:49 AM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    Ok, no problem, I will post it there, but I don't see a lot of C++ in it except for new and cout.
    Life is a piece of chocolates

  5. #5
    Registered User Ephraim's Avatar
    Join Date
    Feb 2004
    Posts
    8
    Originally posted by c99
    I'd suggest you post to the C++ forum.
    Why ?? I thought structs are also available in C!!??

    Or what problem do you have with this code?

    [EDIT]Ohh jep new, cout and using namespace bla are c++, but the main thing
    is the linked list and this can also be programmed with c[/edit]

    Ciao Ephraim
    Last edited by Ephraim; 03-04-2004 at 02:48 AM.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    Originally posted by Ephraim
    In the second call of AddNode you will loose the pointer
    to your root.
    You should use a const Node in AddNode param
    and make a copy of it.
    and then iterate until the end.

    Code:
    void AddNode(const Node *root, int value)
    {
        if(root != NULL)
        {
            Node* node = root;
            while(node->next != NULL)
            {
                node = node->next;
            }
        }
        
        node->next = new Node;
        node = node->next;
        node->next = NULL;
        node->x = value;
    }
    Do this in PrintValue also else you will have
    memory leaks and you will never get back to
    your root node !!!!

    Ciao Ephraim
    If I make it const Node *root, then I wouldn't be able to do

    Code:
    Node* node = root;
    Secondly I dont get it, how would I add data if I make a variable constant like

    const Node *node = root; ?

    Thanks
    Life is a piece of chocolates

  7. #7
    Registered User Ephraim's Avatar
    Join Date
    Feb 2004
    Posts
    8
    Ok i was wrong with the const (to fast replied )
    but you must make a copy of the pointer else
    it is gone if you make this
    Code:
    node = node->next
    By the way without the const it is working now.
    Try both one time with the copy and one time without.

    Ciao Ephraim

  8. #8
    Registered User Ephraim's Avatar
    Join Date
    Feb 2004
    Posts
    8
    F*** forget all what i sad
    it is also working like your first post....
    what is wrong with me today?!

    Ciao Ephraim

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by gautamn
    Ok, no problem, I will post it there, but I don't see a lot of C++ in it except...
    And that's all you needed to see. If any of it is C++, then it can't complie on a C compiler, now can it?

    You should pass a pointer to a pointer (for the root node) to the add function, so you can actually update what it points at.

    Quazh.
    Hope is the first step on the road to disappointment.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    This is the basic idea:

    Code:
     struct Node 
    {
     struct Node * next;
     int value;
    };
    
    
     struct Node * AllocNode(int initial)
    {
     struct Node * node = (Node*)malloc(sizeof(struct Node));
     
         if(node != NULL)
        { 
         node->next = 0;
         node->value = initial;
        }
        
     return node;    
    }
    
    
     void DestroyList(struct Node ** root)
    {
     struct Node * next, * prev = *root;
     
         while(prev != NULL)
        {
         next = prev->next;
         free(prev);
         prev = next;
        } 
        
     *root = 0;   
    }
    
    
     struct Node * AddNode(struct Node ** root, int value)
    {
     struct Node * next = *root;
    
     struct Node * node = AllocNode(value);
     
         if(*root != NULL)
        {
             while(next->next != NULL)
            {
             next = next->next;
            }
            
         next->next = node;     
        } 
         else
        {
         *root = node;
        }  
        
     return node;
    }
    
    
     void PrintList(struct Node * root)
    {
     struct Node * next = root;
     
         while(next != NULL)
        {
         printf("%d\n", next->value);
         
         next = next->next;
        } 
    }
    
    
    
     int main()
    {
     struct Node * root = NULL;
     
     AddNode(&root, 10);
     AddNode(&root, 9);
     AddNode(&root, 8);
     AddNode(&root, 7);
     PrintList(root);
     DestroyList(&root);
    
     return 0;
    }



    ITSA
    Socket Library!

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