Thread: Help with Doubly Linked Lists!

  1. #1
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22

    Question Help with Doubly Linked Lists!

    Here's a very primitive code for a doubly linked list. Although its only very rudimentary, whats wrong with this code? the code's supposed to print the elements sequentially starting from the first node and later print them in reverse.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    typedef struct tagnode              
    {
            int data;
            struct tagnode *next;
            struct tagnode *prev;
    }node;                       
    
    node* create(int);
    void print(node *);
    int main(void)
    {
       int n;
       node *head; 
        printf("Enter number of nodes to create");
        scanf("%d",&n);
        head=create(n);
        print(head);
       return 0;
    }
    
    
    node* create(int n)
    {
     int data,i;
     
     node *localhead=(node *)malloc(sizeof(node));
     node *movinghead=localhead;
     movinghead->prev=NULL;
     movinghead->next=NULL;
     printf("\nEnter data:");
     scanf("%d",&data);
     movinghead->data=data;
     for(i=1;i<n;i++)
     {
       movinghead->next=(node *)malloc(sizeof(node));
       movinghead->next->prev=movinghead;
       movinghead=movinghead->next;
       scanf("%d",&data);
       movinghead->data=data;
       movinghead->next=NULL;
     }
    return localhead;
    } 
    
    void print(node *p)
    {
       node *movinghead;
       for(movinghead=p;movinghead!=NULL;movinghead=movinghead->next)
       {
        printf("\n%d",movinghead->data);
       }
       
       while(movinghead->prev!=NULL)
       {
         printf("%d",movinghead->data);
         movinghead=movinghead->prev;
    } 
    getch();
    }
    Any help would be appreciated!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by black_stallion View Post
    Although its only very rudimentary, whats wrong with this code?
    How about you tell me? What's it not doing that it's supposed to, or doing that it shouldn't be?


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

  3. #3
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Quote Originally Posted by quzah View Post
    How about you tell me? What's it not doing that it's supposed to, or doing that it shouldn't be?
    It prints the elements of the list entered properly. But it doesn't print them in the reverse order as expected from the code starting at line 56. How should I modify it to print them in reverse?(if at all a modification is absolutely necessary)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
       for(movinghead=p;movinghead!=NULL;movinghead=movinghead->next)
       {
        printf("\n%d",movinghead->data);
       }
        
       while(movinghead->prev!=NULL)
    When does the first loop end? How does that affect the second loop?


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

  5. #5

  6. #6
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Quote Originally Posted by quzah View Post
    Code:
       for(movinghead=p;movinghead!=NULL;movinghead=movinghead->next)
       {
        printf("\n%d",movinghead->data);
       }
        
       while(movinghead->prev!=NULL)
    When does the first loop end? How does that affect the second loop?


    Quzah.
    The first loop ends as soon as movinghead becomes NULL(which is the case for the last node in the list) By my logic, at this time, movinghead now points to the last element in the list. All I have attempted from line 56 onwards in to traverse the list in the reverse order and then print them as movinghead goes up the list.


    Also-I forgot to include stdlib.h in the above code but even after doing so, the same problem exists.

    Btw,while debugging, I got a segmentation fault around line 56 if that helps.

  7. #7
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22
    Quote Originally Posted by rags_to_riches View Post
    I don't see anything wrong with that. It's not like I disrespect people's valuable feedback and opinions; its just that I sometimes like to have more than one viewpoints on a particular problem. Besides, is there any guarantee that users on one forum will necessarily be registered on other forums as well?

  8. #8
    Registered User black_stallion's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    22

    Lightbulb Enlightenment!

    Anyway found my mistake. While exiting the for loop, movinghead became equal to NULL. So in the next loop, any reference to movinghead resulted in the compiler trying to access a NULL address something like a segmentation fault!

    This thread may be marked 'closed'!

    Here's the modified code:
    Code:
    void print(node *p)
    {
       node *movinghead;
       for(movinghead=p;movinghead->next!=NULL;movinghead=movinghead->next)//can't use test condition as movinghead!=NULL because for the last 
                                                                           //iteration,movinghead=movinghead->next becomes invalid since 
                                                                          //movinghead->next is NULL so can't print this 'NULL' value.In other words,
                                                                          //stop when movinghead points to the last node.
                                                                          
       {
        printf("%d",movinghead->data);
       }
        printf("%d",movinghead->data);                                     //print last node data(notice that there is no need to execute
                                                                          //movinghead=movinghead->next once more.This is done only if reverse
                                                                          //printing of list is required.Otherwise incorporate this print stmt
                                                                          //in for loop only.
       
       while(movinghead!=NULL)                                      
       {
         printf("%d",movinghead->data);
         movinghead=movinghead->prev;
       }
    getch();
    }

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, 01:01 AM
  2. Doubly Linked Lists
    By tsut in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2003, 07:53 AM
  3. Doubly Linked Lists ???????
    By Magica in forum C Programming
    Replies: 5
    Last Post: 05-04-2003, 12:14 AM
  4. doubly linked lists
    By cworld in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 09:33 AM
  5. Doubly Linked Lists
    By lenicush in forum C Programming
    Replies: 6
    Last Post: 11-17-2001, 03:26 PM

Tags for this Thread