#include<stdlib.h>
#include<stdio.h>
typedef struct node{
int data;
struct node *next, *prev;
}nptr;
nptr *start=NULL;
void createList(){
if(start!=NULL){
printf("\nList Exists\n");
return;
}else{
nptr *tmp;
tmp=(nptr *)malloc(sizeof(nptr));
printf("\nEnter Data\n");
scanf("%d",&tmp->data);
tmp->prev=tmp;
tmp->next=tmp;
start=tmp;
printf("\nLinked List Created\n");
}
}
void insertEnd(){
nptr *tmp,*t=start;
tmp=(nptr *)malloc(sizeof(nptr));
printf("\nEnter Data\n");
scanf("%d",&tmp->data);
tmp->next=start;
tmp->prev=start->prev;
tmp->prev->next=tmp;
start->prev=tmp;
printf("\nnode added at end\n");
}
void insertFront(){
nptr *tmp;
tmp=(nptr *)malloc(sizeof(nptr));
printf("\nEnter Data\n");
scanf("%d",&tmp->data);
tmp->next=start;
tmp->prev=start->prev;
tmp->prev->next=tmp;
start->prev=tmp;
start=tmp;
printf("\nnode added at front\n");
}
void insertBetween(){
nptr *tmp,*t=start;
int x;
tmp=(nptr *)malloc(sizeof(nptr));
printf("\nEnter Data\n");
scanf("%d",&tmp->data);
printf("\nEnter data of node after which new node will be inserted\n");
scanf("%d",&x);
while(t->data!=x){
t=t->next;
}
tmp->prev=t;
tmp->next=t->next;
t->next->prev=tmp;
t->next=tmp;
printf("\nnode added in between\n");
}
void deleteEnd(){
nptr *tmp=start->prev;
start->prev=tmp->prev;
start->prev->next=start;
free(tmp);
printf("\nLast node deleted\n");
}
void deleteFront(){
nptr *tmp=start;
start->next->prev=start->prev;
start->prev->next=start->next;
start=start->next;
free(tmp);
printf("\nFirst node deleted\n");
}
void deleteBetween(){
nptr *tmp=start,*t;
int x;
printf("\nEnter data of nptr to be deleted\n");
scanf("%d",&x);
while(tmp->next->data!=x){
tmp=tmp->next;
}
t=tmp->next;
tmp->next=t->next;
t->next->prev=t->prev;
free(t);
printf("\nnode deleted from middle\n");
}
void display(){
nptr *tmp=start;
do{
printf(" %d ",tmp->data);
if(tmp->next!=NULL)
printf(" -> ");
tmp=tmp->next;
}while(tmp!=start);
printf("\n\n\n");
}
void main(){
int ch;
while(1){
printf("\n1 Create\n2 Insert End\n3 Insert Front\n4 Insert Middle\n5 Delete End\n6 Delete Front\n7 Delete middle\n8 Display\n9 Exit\n\n");
scanf("%d",&ch);
switch(ch){
case(1):createList();
break;
case(2):insertEnd();
break;
case(3):insertFront(); break;
case(4):insertBetween(); break;
case(5):deleteEnd(); break;
case(6):deleteFront(); break;
case(7):deleteBetween(); break;
case(8):display(); break;
case(9):exit(0);
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.