0

I am trying to insert into a doubly linked list. I am then trying to print the list in both forward and reverse direction. I have created a head node and I am trying to insert another one, but I am unable to do so. The program shows a runtime error. Please find my code below. Any help would be appreciated.

#include<stddef.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
    struct node *prev;
};
void insertAfter(struct node *node, int new_data){
    if (node == NULL)
    {
        printf("the given previous node cannot be NULL");
        return;
    }
    struct node* new_node = (struct node*)malloc(sizeof(struct node));
    node->data = new_data;
    node->next = new_node;
    new_node->prev = node;
    new_node->next - node->next;
    if(new_node->next!=NULL)
        new_node->next->prev = new_node;
}
void printlist(struct node *node){
    struct node *last;
    printf("Traversal in forward direction\n");
    while(node!=NULL){
        printf("%d\n",node->data);
        last = node;
        node=node->next;
    }
    printf("Traversal in backward direction\n");
    while(last!=NULL){
        printf("%d\n",last->data);
        last=last->prev;
    }
}
int main()
{
    struct node *head;
    struct node *tail;
    head->data = 5;
    tail->data = 10;
    head->next = tail;
    head->prev = NULL;
    tail->next = NULL;
    insertAfter(head, 8);

    printf("\n Created DLL is: ");
    printlist(head);

    return 0;
}
pat
  • 12,587
  • 1
  • 23
  • 52
Weaver
  • 3
  • 4

3 Answers3

1

There are several problems here.

First, as pointed out by @Igor, you are not allocating any memory for your head and tail nodes. You should also set tail->prev = head.

Second, the order in which insertAfter sets the link pointers causes node->next to be overwritten before it is used in setting new_node->next. This causes new_node->next to point back to new_node instead of to whatever was following node. You should set new_node->next and new_node->prev before you modify node. It also appears that you have used a minus sign instead of an equals in the "assignment" of new_node->next.

Third, in printlist, you should initialize last to NULL in case the list is empty; otherwise, you will attempt to walk the list backwards from an undefined starting (ending) point.

pat
  • 12,587
  • 1
  • 23
  • 52
0

You need to allocate memory for your pointers head and tail.

int main()

    {
        struct node *head;
        struct node *tail;
        head = malloc(sizeof(struct node));   //allocating memory to head
        tail = malloc(sizeof(struct node));   //allocating memory to tail
        head->data = 5;
        tail->data = 10;
        head->next = tail;
        head->prev = NULL;
        tail->next = NULL;
        insertAfter(head, 8);

        printf("\n Created DLL is: ");
        printlist(head);

        return 0;
    }

Also, never cast the return value of malloc, therefore change:

struct node* new_node = (struct node*)malloc(sizeof(struct node));

to

struct node* new_node = malloc(sizeof(struct node));

pat
  • 12,587
  • 1
  • 23
  • 52
Igor Pejic
  • 3,658
  • 1
  • 14
  • 32
0

You want new_node->next to be the same as new_node?

if not, you'd better swap these two lines, in InsertAfter:

node->next = new_node;
new_node->next - node->next;
pat
  • 12,587
  • 1
  • 23
  • 52
Nodarius
  • 366
  • 4
  • 15