How to create double linked list
I am trying to write code for double linked list. My code create single linked list where ecah new node points to previous node. I found, in double linked list each node contains two pointers
Code:
#include<stdio.h>
#include<stdlib.h>
//declare a Node structure
struct node
{
int data;
struct node * next;
struct node * prev;
};
struct node *Add_New_Node (struct node *head, int value)
{
struct node * new = malloc(sizeof(*new));
if ( new != NULL)
{
new -> data = value;
new -> prev = head;
}
return new;
}
void display (struct node *head)
{
struct node * current = NULL;
for (current = head; current != NULL; current = current-> prev)
{
printf("%d\n", current-> data);
}
}
int main ()
{
struct node * head = NULL;
head = Add_New_Node ( head, 10);
head = Add_New_Node ( head, 20);
head = Add_New_Node ( head, 30);
head = Add_New_Node ( head, 40);
head = Add_New_Node ( head, 50);
display (head);
return 0;
}
50
40
30
20
10