-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab8DS.c
221 lines (220 loc) · 6.61 KB
/
lab8DS.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* A program to implement doubly linked list and develop functions to perform
operations like insertion, deletion and linear search */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
struct Node
{
int data;
struct Node *next; //next is pointer of the last node
struct Node *prev;
};
struct Node *head; //head points to the first node
// creating a function to insert a new node in the end of list
void insertion(int element)
{
struct Node *node = head;
// IF head node is not present, dynamically create a node
if (node == NULL)
{
node = (struct Node *)malloc(sizeof(struct Node)); //Memory for new node
node->data = element;
node->next = NULL; //making next of new node to point to NULL
node->prev = NULL; //making prev of new node to point to NULL
head = node; //making head to point to recently created node
}
else
{
/* if list is not empty, shift the last node,
and create a new node in the last of list */
while (node->next != NULL)
{
node = node->next;
}
// Now node is pointing to last node
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
node->next = newNode; //next of last node will point towards newNode
newNode->prev = node; //prev of newNode will point towards earlier last node
newNode->data = element; //inserting the data value in the newNode
newNode->next = NULL; //setting the next of newNode to point towards NULL
}
}
//creating a function to delete first node of the list
int deletion(int element)
{
int removed;
if (head == NULL){
printf("\nList is empty\n");
return INT_MIN;
}
else {
struct Node *temp = head;
removed = head->data; //moving the data value of head node into the removed variable
head = head->next; //now head will point towards next of earlier head node
if(head != NULL)
head->prev = NULL; //prev of new head node point towards NULL
free(temp); //freeing the memory of node to be deleted
}
return removed;
}
//creating a function to search element in forward list as well as backward list
int linearSearch(int data)
{
struct Node *node = head;
int index = -1;
int count = 0;
int choice;
do{
/* press 1 if you want to search element in forward list, and
press 2 if you want to search element in backward list */
printf("\n\nChoose any option\n\n");
printf("1.Search element in Forward list");
printf("\n2.Search element in Reverse list\n\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
while (node != NULL)
{
if (node->data == data)
{
index = count;
break;
}
count++;
node = node->next;
}
return index;
break;
case 2:
if(node == NULL)
printf("\nList is Empty\n");
else{
while(node->next != NULL){
node = node->next;
}
while (node != NULL)
{
if (node->data == data)
{
index = count;
break;
}
count++;
node = node->prev;
}
}
return index;
break;
default:
printf("\nWrong choice, try again\n");
break;
}
}
while(2);
return -1;
}
//creating a function to print forward list
void printForward()
{
struct Node *node = head;
if (node == NULL)
{
printf("\nForward List is Empty.\n");
}
else
{
printf("\nPrinting the list in forward order:\n");
//loop will run until the node is not equal to NULL and will print all the data values of list
while (node != NULL)
{
printf("%d->", node->data);
node = node->next;
}
printf("\n");
}
}
//creating a function to reverse the list and print in reverse order
void printBackward()
{
struct Node *node = head;
if (node == NULL)
{
printf("\nBackward List is Empty.\n");
}
else
{
while(node->next != NULL){
node = node->next;
}
printf("\nPrinting the list in reverse order:\n");
while (node != NULL)
{
printf("%d->", node->data);
node = node->prev;
}
printf("\n");
}
}
int main()
{
int element, operation, choice;
printf("\n\n\t\t\t\t\t\tWelcome Back Arjun\t\t\n");
do
{
printf("\nEnter the operation\n");
printf("1.Insertion\n");
printf("2.Deletion\n");
printf("3.Linear Search\n");
printf("4.Display\n");
printf("5.Exit\n");
scanf("%d", &operation);
switch (operation)
{
case 1:
printf("\nEnter the element you want to insert:\n");
scanf("%d", &element);
insertion(element);
break;
case 2:;
printf("\nDeleting the head node\n");
int removeElement;
int remove = deletion (removeElement);
if (remove != INT_MIN)
{
printf("%d Removed.", remove);
}
else
{
printf("%d", INT_MIN);
}
break;
case 3:;
int searchElement;
printf("\nEnter the element you want to search\n");
scanf("%d", &searchElement);
int search = linearSearch(searchElement);
if (search != -1)
{
printf("\nElement found at index : %d\n", search);
}
else{
printf("\nElement not found\n");
}
break;
case 4:
printForward();
printBackward();
break;
case 5:
exit(0);
break;
default:
printf("Wrong Choice\n");
break;
}
}while (1);
return 0;
}