Search code examples
cwhile-loopdoubly-linked-list

Why does the code stop executing after the while loop?


I was doing a doubly linked list but I found out that when I print nodes in reverse order, the code doesn't work. It seems like stuck by the previous while loop.

This is the code:

#include <stdio.h>
#include <stdlib.h>

struct LLNode
{
    char name;
    struct LLNode *left;
    struct LLNode *right;
};

struct LLNode * createNode (char data)
{
    struct LLNode *temp; temp = (struct LLNode *)malloc(sizeof(struct LLNode));
    temp->name = data;
    temp->left = NULL;
    temp->right = NULL;
    return(temp);
};

int main()
{
    struct LLNode *curr=NULL;
    struct LLNode *head=curr;

    head = curr = createNode ('A') ;
    printf ("curr->name = %c\n", curr->name) ;

    printf ("head %c tail %c\n",head->name,curr->name) ;

    curr->right = createNode('B');
    printf ("curr->right->name = %c\n", curr->right->name) ;

    curr->right->left = curr,
    printf ("curr->right->left->name = %c\n", curr->right->left->name) ;

    curr = curr->right;
    printf ( "curr = %c\n" , curr->name) ;

    printf ("head %c tail %c\n",head->name,curr->name) ;

    curr->right = createNode('C');
    printf("curr->right->name = %c\n",curr->right->name);

    curr->right->left = curr;
    printf("curr->right->left->name = %c\n", curr->right->left->name);

    curr = curr->right;
    printf("curr->name= %c\n",curr->name);

    printf ("head %c tail %c\n",head->name,curr->name) ;

    printf ("Linked list from head to tail: ");

    while (curr!= NULL)
        {
            // Print nodes from beginning to end.
            printf("%c ", head->name);
            head = head->right;
        }

    printf ("Linked list in reverse: ");

    while (curr!= NULL)
        {
            // Print nodes in reverse order.
            printf("%c ", curr->name);
            curr = curr->left;
        }

    return 0;
}

The code seems to stop after executing the while loop "Print nodes from beginning to end", but I don't know why. I've tried to use break, continue and other methods but still cannot solve this problem.

Can anyone help?


Solution

  • There are some problems in your code. Apart from not setting curr->left values. I think the way you're iterating over your linked list; head would be NULL at the end of first iteration and control won't come inside reverse printing loop. You would need to break out of loop without moving the head when you reach curr->left == NULL or curr->right == NULL depending upon you're traversing forward or backward.

    I made some code changes to your code and I was able to see desired output:

    int main() {
        struct LLNode *curr=NULL;
        struct LLNode *head = curr;
        struct LLNode *prev = NULL;
    
        // Insert first Element
        head = curr = createNode ('A') ;
        printf ("curr->name = %c\n", curr->name) ;
    
        // Insert Second Element
        curr->right = createNode('B');
        printf ("curr->right->name = %c\n", curr->right->name) ;
    
        prev = curr;
        curr = curr->right;
        printf ( "curr = %c\n" , curr->name) ;
    
        // Insert Third Element
        curr->right = createNode('C');
        curr->left = prev;
        printf("curr->right->name = %c\n",curr->right->name);
    
        prev = curr;
        curr = curr->right;
        curr->left = prev;
        printf("curr->name= %c\n",curr->name);
    
        printf ("\nLinked list from head to tail: ");
        while (1) {
            // Print nodes from beginning to end.
            printf("%c ", head->name);
            if (head->right == NULL)
                break;
            head = head->right;
        }
    
        printf ("\nLinked list in reverse: ");
        while (1) {
            // Print nodes from beginning to end.
            printf("%c ", head->name);
            if (head->left == NULL)
                break;
            head = head->left;
        }
    
        return 0;
    }
    

    When I run this, I was able to see list getting printed correctly:

    curr->name = A
    curr->right->name = B
    curr = B
    curr->right->name = C
    curr->name= C
    
    Linked list from head to tail: A B C 
    Linked list in reverse: C B A