Thread: Linked lists

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    82

    Linked lists

    Hello,
    I created a structure with two nodes in it, one that points to the next node and the other points to previous node:

    Code:
    #include<stdio.h>
    
    typedef struct node
    {
            int         value;
            struct node *next;
            struct node *prev;
    }node;
    
    int main(void)
    {
        node *head = NULL;
        int  i;
        
        head = malloc(sizeof(node));
        head->value = 1;
        head->prev = NULL;
        
        for (i = 2; i < 11; i++)
        {
            head->next = malloc(sizeof(node));
            head->next->prev = malloc(sizeof(node));
            
            head->next->prev = head;
            head = head->next;
            
            head->value = i;
        }
        head->next = NULL;
        
        while(head != NULL)
        {
              printf("%d\n",head->value);
              head = head->prev;
        }
        
        /*Not working*/
        while(head)
        {
              printf("%d\n",head->value);
              head = head->next;
        }
        
        system("pause");
        return 0;
    }
    Why is the second printing not working???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you changed what head points to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by laserlight View Post
    Notice that you changed what head points to.
    Ya I changed so that head goes back where it started from; printing out numbers in reverse order, and then print the numbers again from start to end.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There are other anomalies in your linked list code.
    Look at these two lines. Can you see a problem?
    Code:
            head->next->prev = malloc(sizeof(node));
            head->next->prev = head;
    And you shouldn't be mallocing two nodes each iteration of the loop.
    Just malloc one new node and attach it to the list by adjusting pointers.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by oogabooga View Post
    There are other anomalies in your linked list code.
    Look at these two lines. Can you see a problem?
    Code:
            head->next->prev = malloc(sizeof(node));
            head->next->prev = head;
    And you shouldn't be mallocing two nodes each iteration of the loop.
    Just malloc one new node and attach it to the list by adjusting pointers.
    Okay, I removed
    Code:
    head->next->prev = malloc(sizeof(node));
    , but still its not working.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The program is advancing head through the list as it creates the list. The program should use a separate pointer to node to build the list with, so that head isn't modified. The other option is to follow the list backwards using head until head->prev == NULL to set head back to the beginning of the list.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by rcgldr View Post
    The other option is to follow the list backwards using head until head->prev == NULL to set head back to the beginning of the list.
    That's what I have done, haven't I?

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by laserlight View Post
    What is your current code and how does it not work?
    Code:
    #include<stdio.h>
    
    typedef struct node
    {
            int         value;
            struct node *next;
            struct node *prev;
    }node;
    
    int main(void)
    {
        node *head = NULL;
        int  i;
        
        head = malloc(sizeof(node));
        head->value = 1;
        head->prev = NULL;
        
        for (i = 2; i < 11; i++)
        {
            head->next = malloc(sizeof(node));
            
            head->next->prev = head;
            head = head->next;
            
            head->value = i;
        }
        head->next = NULL;
        
        /*this one works well*/
        while(head != NULL)
        {
              printf("%d\n",head->value);
              head = head->prev;
        }
        
        /*This is not working: its not printing anything*/
        while(head != NULL)
        {
              printf("%d\n",head->value);
              head = head->next;
        }
        
        system("pause");
        return 0;
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, it looks like you still have the same problem: notice that head is changed, hence in the second loop to print, head is already NULL, so nothing is printed.

    A solution is to avoid changing head when printing by introducing another variable that starts life as a copy of head. One way to do this is to implement a function to print your linked list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    One thing to do is print the adresses of where you are in the list. This is as simple as just printing the pointer.

  12. #12
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by laserlight View Post
    Well, it looks like you still have the same problem: notice that head is changed, hence in the second loop to print, head is already NULL, so nothing is printed.

    A solution is to avoid changing head when printing by introducing another variable that starts life as a copy of head. One way to do this is to implement a function to print your linked list.
    Okay I did as you said:
    Code:
    #include<stdio.h>
    
    typedef struct node
    {
            int         value;
            struct node *next;
            struct node *prev;
    }node;
    
    int main(void)
    {
        node *head = NULL;
        node *cpy;
        node *cpy2;
        int  i;
        
        head = malloc(sizeof(node));
        head->value = 1;
        head->prev = NULL;
        
        for (i = 2; i < 11; i++)
        {
            head->next = malloc(sizeof(node));
            //head->next->prev = malloc(sizeof(node));
            
            head->next->prev = head;
            head = head->next;
            
            head->value = i;
        }
        head->next = NULL;
        
        cpy = head;
        while(cpy != NULL)
        {
              printf("%d\n",cpy->value);
              cpy = cpy->prev;
        }
       
        while(cpy)
        {
              printf("%d\n",cpy->value);
              cpy = cpy->next;
        }
        
        system("pause");
        return 0;
    }
    Now how can I bring the head to the starting, and print by going next???

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Just before that for(i = 2; ...), insert cpy = head; into the code, then use cpy in the for loop instead of head, so the line after the for loop would be cpy->next = NULL; .

  14. #14
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by rcgldr View Post
    Just before that for(i = 2; ...), insert cpy = head; into the code, then use cpy in the for loop instead of head, so the line after the for loop would be cpy->next = NULL; .
    Ya, I know this method, just wanted to try the next and previous method.

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by rcgldr View Post
    Just before that for(i = 2; ...), insert cpy = head; into the code, then use cpy in the for loop instead of head, so the line after the for loop would be cpy->next = NULL; .
    Quote Originally Posted by Harith View Post
    Ya, I know this method, just wanted to try the next and previous method.
    I meant this change:

    Code:
    /* ... */
        cpy = head;
        for (i = 2; i < 11; i++)
        {
            cpy->next = malloc(sizeof(node));
            cpy->next->prev = cpy;
            cpy = cpy->next;
            cpy->value = i;
        }
        cpy->next = NULL;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM

Tags for this Thread