Skip to content
Home » Creating a node in doubly linked list

Creating a node in doubly linked list

Below is the code to create a node in doubly linked list.


#include <stdio.h>
#include <stdlib.h>
struct node{
struct node *prev;
int data;
struct node *next;
};
struct node *head=NULL;
void create(int ele){
struct node *temp=malloc(sizeof(struct node));
temp->prev=NULL;
temp->data=ele;
temp->next=NULL;
if(head==NULL){
head=temp;
}
else{
struct node *ptr=head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=temp;
temp->prev=ptr;
}
}
void print(){
struct node *current=head;
while(current!=NULL){
printf("%d ",current->data);
current=current->next;
}
}
int main(){
create(10);
create(20);
create(50);
create(60);
print();
}

OUTPUT:

10 20 50 60