-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq11.c
145 lines (133 loc) · 2.96 KB
/
q11.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
/*
/*
/* Design, develop, and execute a program in C to implement a doubly
/* linked list where each node consists of integers. The program should
/* support the following operations:
/* i. Create a doubly linked list by adding each node at the front.
/* ii. Insert a new node to the left of the node whose key value is
/* read as an input.
/* iii. Delete the node of a given data if it is found, otherwise
/* display appropriate message.
/* iv. Display the contents of the list.
/* (Note: Only either (a,b and d) or (a, c and d) may be asked in the
/* examination)
/*
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *prev, *next;
} node;
node *insert(node *start, int val) {
node *n = malloc(sizeof(node));
n->val = val;
n->prev = NULL;
n->next = NULL;
if(start == NULL)
return n;
n->next = start;
start->prev = n;
return n;
}
node *insertBefore(node *start, int val, int before) {
node *realStart = start;
while(start != NULL) {
if(start->val == before) {
node *n = malloc(sizeof(node));
n->val = val;
n->prev = NULL;
n->next = start;
if(start->prev != NULL) {
start->prev->next = n;
n->prev = start->prev;
start->prev = n;
return realStart;
}
else
return n;
}
start = start->next;
}
return realStart;
}
node *delete(node *start, int val) {
node *realStart = start, *toDelete = NULL;
while(start != NULL) {
if(start->val == val)
toDelete = start;
start = start->next;
}
if(toDelete == NULL) {
printf(" Not Found");
return realStart;
}
else {
if(toDelete->prev == NULL)
realStart = toDelete->next;
else
toDelete->prev->next = toDelete->next;
if(toDelete->next == NULL)
toDelete->prev->next = NULL;
else
toDelete->next->prev = toDelete->prev;
printf(" Deleted: %d\n", toDelete->val);
free(toDelete);
}
return realStart;
}
void display(node *start) {
while(start != NULL) {
printf(" %d", start->val);
start = start->next;
}
}
node *freeAll(node *start) {
while(start != NULL) {
node *temp = start->next;
free(start);
start = temp;
}
return start;
}
void main() {
node *start = NULL;
int item, before;
char choice;
printf("\n Enter");
printf("\n i to Insert");
printf("\n b to Insert Before");
printf("\n d to Delete");
printf("\n s to Display");
do {
printf("\n >> ");
scanf("%c", &choice);
switch(choice) {
case 'i':
printf(" Enter element to insert: ");
scanf(" %d", &item);
start = insert(start, item);
break;
case 'b':
printf(" Enter element to insert: ");
scanf(" %d", &item);
printf(" Enter element to insert before: ");
scanf(" %d", &before);
start = insertBefore(start, item, before);
break;
case 'd':
printf(" Enter element to delete: ");
scanf(" %d", &item);
start = delete(start, item);
break;
case 's':
display(start);
printf("\n");
break;
default:
start = freeAll(start);
return;
}
scanf("%c", &choice);
} while(1);
}