-
Notifications
You must be signed in to change notification settings - Fork 2
/
DoublyLinkedList.c
181 lines (166 loc) · 4.6 KB
/
DoublyLinkedList.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *prev;
int data;
struct node* next;
};
struct node *head = NULL;
struct node *tail = NULL;
struct node *ptr = NULL;
struct node *searchPtr = NULL;
struct node *newNode = NULL;
void createNode(){
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter the data:\t");
scanf("%d",&newNode->data);
newNode->next = NULL;
newNode->prev = NULL;
}
void insert_begin(){
createNode();
if(head == NULL){
head = newNode;
tail = newNode;
return;
}
newNode->next = head;
head->prev = newNode;
head = newNode;
}
void insert_end(){
if(head == NULL){
insert_begin();
return;
}
createNode();
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
void insert_before(){
if(searchPtr == head){
insert_begin();
return;
}
createNode();
newNode->prev = searchPtr->prev;
newNode->next = searchPtr;
searchPtr->prev->next = newNode;
searchPtr->prev = newNode;
}
void insert_after(){
if(searchPtr == tail){
insert_end();
return;
}
createNode();
newNode->prev = searchPtr;
newNode->next = searchPtr->next;
searchPtr->next->prev = newNode;
searchPtr->next = newNode;
}
void delete(){
if(searchPtr == head){
head = head->next;
head->prev = NULL;
return;
}
if(searchPtr == tail){
tail = tail->prev;
tail->next = NULL;
return;
}
searchPtr->next->prev = searchPtr->prev;
searchPtr->prev->next = searchPtr->next;
}
struct node* searchElement(int element){
ptr = head;
while(ptr != NULL){
if(ptr->data == element){
printf("hi!");
return ptr;
}
ptr = ptr->next;
}
return NULL;
}
void display(){
if(head == NULL){
printf("\nThe Linked List is empty");
return;
}
printf("\nThe elements of the linked list are:\t");
ptr = head;
while(ptr != NULL){
printf("%d ",ptr->data);
ptr = ptr->next;
}
}
void main(){
while(1){
printf("\n-----------------------------------------------");
printf("\n1. Insert an element in the beginning");
printf("\n2. Insert an element in the end");
printf("\n3. Insert an element before a given element");
printf("\n4. Insert an element after a given element");
printf("\n5. Delete the said element");
printf("\n6. Dislay");
printf("\n7. Exit!");
printf("\n-----------------------------------------------");
int ch,element;
printf("\nEnter your choice:\t");
scanf("%d",&ch);
switch(ch){
case 1:
insert_begin();
printf("\nInserted Successfully");
break;
case 2:
insert_end();
printf("\nInserted Successfully");
break;
case 3:
printf("\nEnter the eleemnt before which you wanna insert:\t");
scanf("%d",&element);
searchPtr = searchElement(element);
if(searchPtr == NULL){
printf("\nElement not found!");
break;
}
insert_before();
printf("\nInserted Successfully");
break;
case 4:
printf("\nEnter the eleemnt after which you wanna insert:\t");
scanf("%d",&element);
searchPtr = searchElement(element);
if(searchPtr == NULL){
printf("\nElement not found!");
break;
}
insert_after();
printf("\nInserted Successfully");
break;
case 5:
printf("\nEnter the eleemnt you wanna delete:\t");
scanf("%d",&element);
searchPtr = searchElement(element);
if(searchPtr == NULL){
printf("\nElement not found!");
break;
}
delete();
printf("\nDeleted Successfully");
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("\nInvalid choice!");
}
}
}