Created
January 13, 2018 10:17
-
-
Save shashank855/1ecd02760cbff973f62194fb8acee219 to your computer and use it in GitHub Desktop.
Doubly linklist Implementation in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include<conio.h> | |
#include <stdlib.h> | |
#include<malloc.h> | |
#define null 0 | |
struct node | |
{ | |
struct node *prev; | |
int data; | |
struct node *next; | |
}*start=null,*temp=null,*last=null; | |
void create(); | |
void display(); | |
void del(); | |
void insert(); | |
void main() | |
{ | |
int a; | |
do | |
{ | |
printf("\n1:Create list"); | |
printf("\n2:Display"); | |
printf("\n3:Delete"); | |
printf("\n4:Insert\n"); | |
printf("\n\tEnter your option"); | |
scanf("%d",&a); | |
switch(a) | |
{ | |
case 1: | |
create(); | |
break; | |
case 2: | |
display(); | |
break; | |
case 3: | |
del(); | |
break; | |
case 4: | |
insert(); | |
} | |
}while(a!=5); | |
} | |
void create() | |
{ | |
int x; | |
temp=(struct node*)malloc(sizeof(struct node)); | |
printf("\nEnter Data"); | |
scanf("%d",&x); | |
temp->data=x; | |
temp->next=null; | |
temp->prev=null; | |
if(start==null) | |
{ | |
start=temp ; | |
last=temp; | |
} | |
else{ | |
last->next=temp; | |
temp->prev=last; | |
last=temp; | |
} | |
printf("\nList Created"); | |
} | |
void display() | |
{ | |
int x; | |
printf("\n\tEnter option"); | |
printf("\n1:Print from beginning"); | |
printf("\n2:print Reverse"); | |
scanf("\n%d",&x); | |
switch(x) | |
{ | |
case 1: | |
temp=start; | |
while(temp!=null) | |
{ | |
printf("\n%d",temp->data); | |
temp=temp->next; | |
} | |
getch(); | |
break; | |
case 2: | |
temp=last; | |
while(temp!=null) | |
{ | |
printf("\n%d",temp->data); | |
temp= temp->prev; | |
} | |
break; | |
}} | |
void del() | |
{ | |
int x,y,z; | |
struct node *ptr; | |
printf("\n1:Del from Beginning"); | |
printf("\n2:Del from middle"); | |
printf("\n3:Del from last"); | |
printf("\nEnter your option"); | |
scanf("%d",&x); | |
switch(x) | |
{ | |
case 1: | |
temp=start; | |
temp=temp->next; | |
start=temp; | |
break; | |
case 3: | |
temp=last; | |
temp=temp->prev; | |
temp->next=null; | |
break; | |
case 2: | |
printf("\nEnter value to be deleted"); | |
scanf("%d",&y); | |
printf("1:One value \n 2: All value"); | |
scanf("%d",&z); | |
if(z==1) | |
{ | |
temp=start; | |
while(temp->data!=y) | |
{ | |
temp=temp->next; | |
} | |
ptr=temp->prev; | |
temp=temp->next; | |
temp->prev=ptr; | |
ptr->next=temp; | |
} | |
else{ | |
temp=start; | |
while(temp->next!=null) | |
{ | |
if(temp->data==y) | |
{ | |
ptr=temp->prev; | |
temp=temp->next; | |
temp->prev=ptr; | |
ptr->next=temp; | |
} temp=temp->next; | |
} | |
} | |
} | |
} | |
void insert() | |
{ | |
int x,y; | |
printf("\nEnter data to Insert"); | |
scanf("%d",&y); | |
printf("\n1:Enter at Beginning"); | |
printf("\n2:Enter at Middle"); | |
printf("\n3:Enter at Last"); | |
printf("\nEnter your option"); | |
scanf("%d",&x); | |
if(x==1) | |
{ | |
temp=(struct node*)malloc(sizeof(struct node)); | |
temp->data=y; | |
temp->prev=null; | |
temp->next=start; | |
start->prev=temp; | |
start=temp; | |
} | |
else if(x==2) | |
{ | |
int z; | |
struct node *ptr,*p; | |
printf("\n\tEnter data After which to Insert"); | |
scanf("%d",&z); | |
temp=start; | |
while(temp->data!=z) | |
{ | |
temp=temp->next; | |
} | |
ptr=temp->next; | |
p=(struct node*)malloc(sizeof(struct node)); | |
p->data=y; | |
temp->next=p; | |
p->next=ptr; | |
ptr->prev=p; | |
p->prev=temp; | |
} | |
else | |
{ | |
temp=(struct node*)malloc(sizeof(struct node)); | |
temp->data=y; | |
last->next=temp; | |
temp->prev=last; | |
temp->next=null; | |
last=temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment