Home > Mobile >  I want to print a doubly linked list from left to right and then right to left but my program is mis
I want to print a doubly linked list from left to right and then right to left but my program is mis

Time:08-11

Hey guys I want to print a doubly linked list with the output: [23 34 56 52] [52 56 34 23] but my program is missing 52 from the first half and 23 from the second half i.e. my output is coming out as: [23 34 56] [52 56 34] someone please correct my code. Thanks !

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

struct Node{
    int data;
    struct Node* prev;
    struct Node* next;
};

void linkedListTraversal(struct Node* head){
    struct Node* ptr = head;
    printf("[  ");
    while(ptr->next != NULL){
        printf("%d  ",ptr->data);
        ptr = ptr->next;
    }
    printf("]");
    printf("\n");
    printf("[  ");
    while(ptr->prev != NULL){
        printf("%d  ",ptr->data);
        ptr = ptr->prev;
    }
    printf("]");
}

int main(){
    struct Node* head;
    struct Node* second;
    struct Node* third;
    struct Node* fourth;

    head = (struct Node*) malloc(sizeof(struct Node));
    second= (struct Node*) malloc(sizeof(struct Node));
    third = (struct Node*) malloc(sizeof(struct Node));
    fourth = (struct Node*) malloc(sizeof(struct Node));

    head->data = 23;
    head->prev = NULL;
    head->next = second;

    second->data = 34;
    second->prev = head;
    second->next = third;

    third->data = 56;
    third->prev = second;
    third->next = fourth;

    fourth->data = 52;
    fourth->prev = third;
    fourth->next = NULL;

    linkedListTraversal(head);

    return 0;
}

CodePudding user response:

As some of the comments say this is simple enough that you could probably work it out using a debugger, which for the sake of your ongoing coding education I would recommend you look into (They massively stepped up my game when I was starting out!).

However, to answer your question I think you need to be focusing on the logic behind your while loops.

while(ptr->next != NULL){
    printf("%d  ",ptr->data);
    ptr = ptr->next;
}

What this is saying is "While my ptr's next is not null print its data". The problem with this is your final (or first) node, doesn't have a next (or prev) so it doesn't fit the condition to print out its data.

Instead, you want to say "Until my node is null print out its data", which would look like

while(ptr != NULL){
    printf("%d  ",ptr->data);
    ptr = ptr->next;
}

CodePudding user response:

The problem is printing forward without running off the end, and then shifting into reverse. Following is a version with some enhancements.

Use typedef to save yourself lots of typing (and reading!). Use calloc() to initialise the struct to NULL (including ptrs). Always test return values from functions; you can run out out memory. Layout the assignments in main() to make clear what you are doing.

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node_t;

void linkedListTraversal( Node_t *head ) {
    printf("[  ");
    Node_t *pLast = head;
    for( Node_t *pn = head; pn; pLast = pn, pn = pn->next )
        printf( "%d  ", pn->data );

    printf("]\n[  ");

    for( pn = pLast; pn; pn = pn->prev )
        printf("%d  ", pn->data );

    printf("]");
}

int main() {
    Node_t *p1 = (Node_t*)calloc( 1, sizeof *p1 ); // use calloc()
    Node_t *p2 = (Node_t*)calloc( 1, sizeof *p2 );
    Node_t *p3 = (Node_t*)calloc( 1, sizeof *p3 );
    Node_t *p4 = (Node_t*)calloc( 1, sizeof *p4 );

    if( p1 == NULL || p2 == NULL || p3 == NULL || p4 == NULL ) {
        fprintf( stderr, "Calloc failure\n" );
        return -1;
    }

                    p1->data = 23;  p1->next = p2;
    p2->prev = p1;  p2->data = 34;  p2->next = p3;
    p3->prev = p2;  p3->data = 56;  p3->next = p4;
    p4->prev = p3;  p4->data = 52;

    linkedListTraversal( p1 );

    return 0;
}

Output:

[  23  34  56  52  ]
[  52  56  34  23  ]
  • Related