-
Notifications
You must be signed in to change notification settings - Fork 3
/
doubly_ended_linkedlist.c
186 lines (169 loc) · 3.92 KB
/
doubly_ended_linkedlist.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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
struct Node* prev;
int data;
struct Node* next;
}*first = NULL;
void create(int a[], int n)
{
struct Node* t, * last;
first = (struct Node*)malloc(sizeof(struct Node));
first->data = a[0];
first->prev = first->next = NULL;
last = first;
for (int i = 1;i < n;i++)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = a[i];
t->next = last->next;
t->prev = last;
last->next = t;
last = t;
}
}
int Length(struct Node* p)
{
int len = 0;
while (p)
{
len++;
p = p->next;
}
return len;
}
void Insert(struct Node* p, int index, int x)
{
struct Node* t;
if (index < 0 || index > Length(p))
return;
if (index == 0)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = x;
t->prev = NULL;
t->next = first;
first->prev = t;
first = t;
}
else
{
for (int i = 0;i < index - 1;i++)
p = p->next;
t = (struct Node*)malloc(sizeof(struct Node));
t->data = x;
t->prev = p;
t->next = p->next;
if (p->next)p->next->prev = t;
p->next = t;
}
}
int Delete(struct Node* p, int index)
{
int x = -1;
if (index < 1 || index > Length(p))
return -1;
if (index == 1)
{
first = first->next;
if (first)first->prev = NULL;
x = p->data;
free(p);
}
else
{
for (int i = 0;i < index - 1;i++)
p = p->next;
p->prev->next = p->next;
if (p->next)
p->next->prev = p->prev;
x = p->data;
free(p);
}
return x;
}
void search(struct Node* p, int s)
{
int i = 0;
int f = 0;
while (p)
{
if (p->data == s)
{
printf("Element %d found at position (Taking doubly ended list positions to be starting from 1)=%d ", s, i + 1);
f = 1;
break;
}
i++;
p = p->next;
}
if (f == 0)
{
printf("Element not found!\n");
}
printf("\n");
}
void Display(struct Node* p)
{
printf("\nAll elements of the doubly ended linked list are:\n");
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
printf("\n");
}
int main()
{
printf("Enter the amount of elements:\n");
int n;
scanf("%d", &n);
int a[n];
printf("Enter the elements:\n");
for (int i = 0;i < n;i++)
{
scanf("%d", &a[i]);
}
create(a, n);
Display(first);
int s, z, y, index, index2;
int w = 1;
while (w != 0)
{
printf("Enter 1. to Insert:\n");
printf("Enter 2. to Delete:\n");
printf("Enter 3. to Search:\n");
printf("Enter 4. to Display:\n");
printf("Enter 0. to exit program:\n");
scanf("%d", &w);
switch (w)
{
case 1:
printf("Enter the element to be inserted:\n");
scanf("%d", &y);
printf("Enter index (element positions starting from 0):\n");
scanf("%d", &index);
Insert(first, index, y);
break;
case 2:
printf("Enter position (Considering doubly ended list positions to be starting from 1) of the element to be deleted:\n");
scanf("%d", &index2);
z = Delete(first, index2);
printf("The Deleted element= %d\n", z);
break;
case 3:
printf("Enter element to be found:\n");
scanf("%d", &s);
search(first, s);
break;
case 4:
Display(first);
break;
case 0:
break;
}
}
return 0;
}