-
Notifications
You must be signed in to change notification settings - Fork 4
/
doubleLinkedList.c
97 lines (91 loc) · 1.96 KB
/
doubleLinkedList.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<stdio.h> //invoking standard input output.
#include<stdlib.h> //invoking standard library function.
struct node{
int data;
struct node *next,*prev;
}* start=NULL;
struct node * create(int data){ //using structure to create a node.
struct node *p;
p=(struct node *)malloc(sizeof(struct node)); //malloc
p->data=data;
p->next=NULL;
p->prev=NULL;
return p; // return statement
}
void insert(struct node * n){ //using a insert method to insert a node in the linked list.
struct node *ptr=start;
if(start==NULL)
start=n;
else{
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=n;
n->prev=ptr;
}
}
void del(int data){ //method for deleting a node.
struct node * ptr=start,*tmp;
int f=0;
if(start==NULL)
printf("UNDERFLOW\n");
else{
while(ptr!=NULL){
if(start->data==data){
start=NULL;
f=1;
printf("%d is deleted\n",data);
break; //break statement
}
else if(ptr->data == data){
tmp=ptr->next;
ptr->prev->next=tmp;
tmp->prev=tmp->prev->prev;
printf("%d is deleted\n",data);
f=1;
break;
}
ptr=ptr->next;
}
if(f==0) // if statement
printf("NOT FOUND\n"); //output statement
}
}
void display(){ //method for displaying the output.
struct node * ptr=start;
if(start==NULL)
printf("UNDERFLOW\n");
else{
while(ptr!=NULL){//while loop
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
}
int main(void){ // Main method
int n,data;
do{
printf("Menu:\n1. Insert\n2. Display\n3. Delete\n4. Exit\nenter: "); //
scanf("%d",&n);
switch(n){//switch case
case 1:
printf("enter the data: ");
scanf("%d",&data);
insert(create(data));
break;
case 2:
display();
break;
case 3:
printf("enter the value: ");
scanf("%d",&data);
del(data);
break;
case 4:
printf("Thank You!!\n");
default: // default case.
printf("Wrong Choice!! Try again\n");
}
}while(n!=3);//while loop
return 1; //return statement
} //end of programs