-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS_LL2.c
196 lines (193 loc) · 5.1 KB
/
DS_LL2.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//DOUBLY LINKED LIST
//OPERATIONS IN DOUBLY LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct node { //NODE STRUCTURE OF LINKED LIST
int data;
struct node *prev, *next;
};
struct node* start = NULL;
void traverse(){ //FUNCTION TO PRINT DOUBLY LINKED LIST
if (start == NULL) { //IF LIST IS EMPTY
printf("\nEMPTY LIST\n");
return;
}
struct node* temp; //PRINTS DATA OF NODE
temp = start;
while (temp != NULL) {
printf("DATA= %d\n", temp->data);
temp = temp->next;
}
}
void insertAtFront(){ //FUNCTION TO INSERT AT FRONT
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nENTER NUMBER TO BE INSERTED= ");
scanf("%d", &data);
temp->data = data;
temp->prev = NULL;
temp->next = start;//POINTER OF TEMP WILL BE ASSIGNED TO START
start = temp;
}
void insertAtEnd(){ //FUNCTION TO INSERT AT THE END
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nENTER NUMBER TO BE INSERTED= ");
scanf("%d", &data);
temp->data = data;
temp->next = NULL;
trav = start;
if (start == NULL) { //IF START IS NULL
start = temp;
}
else { //CHANGES LINK
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
void insertAtPosition(){ //FUNCTION TO INSERT AT GIVEN POSITION
int data, pos, i = 1;
struct node *temp, *newnode;
newnode = malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
printf("\nENTER POSITION= "); //ENTER POSITION AND DATA
scanf("%d", &pos);
printf("\nENTER THE NUMBER= ");
scanf("%d", &data);
newnode->data = data;
temp = start;
if (start == NULL) { //IF START IS NULL
start = newnode;
newnode->prev = NULL;
newnode->next = NULL;
}
else if (pos == 1) { //IF POSITION IS 1
newnode->next = start;
newnode->next->prev = newnode;
newnode->prev = NULL;
start = newnode;
}
else { //CHANGE LINKS
while (i < pos - 1) {
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
}
}
void deleteFirst(){ //FUNCTION TO DELETE FROM FRONT
struct node* temp;
if (start == NULL)
printf("\nEMPTY LIST\n");
else {
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
}
}
void deleteEnd(){ //FUNCTION TO DELETE FROM END
struct node* temp;
if (start == NULL)
printf("\nEMPTY LIST\n");
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
}
void deletePosition(){ //FUNCTION TO DELETE FROM GIVEN POSITION
int pos, i = 1;
struct node *temp, *position;
temp = start;
if (start == NULL) //IF DLL IS EMPTY
printf("\nEMPTY LIST\n");
else {
printf("\nENTER POSITION= "); //POSITION
scanf("%d", &pos);
if (pos == 1) { //IF POSITION IS FIRST NODE
position = start;
start = start->next;
if (start != NULL) {
start->prev = NULL;
}
free(position);
return;
}
while (i < pos - 1) { //TRAVERSE TILL POSITION
temp = temp->next;
i++;
}
position = temp->next; //CHANGE LINKS
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
free(position); //FREE MEMORY
}
}
int main(){
int x,y;
printf("ENTER THE SIZE OF DOUBLY LINKED LIST= ");
scanf("%d",&y);
for(x=0;x<y;x++){
insertAtEnd();
}
int choice;
while (1) {
printf("\n\t1- TO PRINT DOUBLY LINKED LIST\n");
printf("\t2- TO INSERT AT FRONT\n");
printf("\t3- TO INSERT AT END\n");
printf("\t4- TO INSERT AT ANY POSITION\n");
printf("\t5- TO DELETE FIRST ELEMENT\n");
printf("\t6- TO DELETE FROM END\n");
printf("\t7- TO DELETE FROM ANY POSITION\n");
printf("\t8- TO EXIT\n");
printf("\nENTER YOUR CHOICE= \n");
scanf("%d", &choice);
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
exit(1);
break;
default:
printf("INVALID CHOICE !! PLEASE TRY AGAIN \n");
continue;
}
}
return 0;
}