-
Notifications
You must be signed in to change notification settings - Fork 0
/
arr_as_list.c
159 lines (128 loc) · 3.64 KB
/
arr_as_list.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
#include<stdio.h>
#define SIZE 20
struct ll {
int data,
next, //next index
prev; // previous index
} list[SIZE];
int count = 0; // num of elements
int head_ptr, last_ptr; // indices of the first and last numbers in array
void sorted_insert(int data) {
int i, prev;
if(count == SIZE) {
printf("List is Full!\n");
return;
}
if(count != 0) {
if(data <= list[head_ptr].data) {
list[count].next = head_ptr;
list[count].prev = last_ptr;
list[last_ptr].next = head_ptr = count;
}
else
if(data >= list[last_ptr].data) {
list[last_ptr].next = count;
list[count].prev = last_ptr;
last_ptr = count;
list[count].next = head_ptr;
list[head_ptr].prev = count;
}
else {
i = head_ptr; // current
do {
//prev = i;
i = list[i].next;
} while(list[i].data <= data);
//traverse the list until data is greater
list[count].next = i; // newNode.next = current
list[list[i].prev].next = count; // current.prev.next = newNode
list[count].prev = list[i].prev; // newNode.prev = current.prev
list[i].prev = count; // current.prev = newNode
}
}
else {
head_ptr = last_ptr = list[count].next = list[count].prev = 0;
}
list[count++].data = data;
}
void delete_smallest() {
list[head_ptr].data = 0;
list[last_ptr].next = head_ptr = list[head_ptr].next;
list[head_ptr].prev = last_ptr;
count--;
}
void delete_largest() {
list[last_ptr].data = 0;
list[head_ptr].prev = last_ptr = list[last_ptr].prev;
list[last_ptr].next = head_ptr;
count--;
}
void delete(int pos) {
int i, k = 0;
if(pos - 1 < 0 || pos - 1 > count || pos - 1 > SIZE) {
printf("Invalid Position!\n");
return;
}
if(pos)
delete_smallest();
else
if(pos == count)
delete_largest();
else {
for(i = head_ptr; i != last_ptr && k < pos - 1; k++, i = list[i].next);
list[i].data = 0;
list[list[i].prev].next = list[i].next;
list[list[i].next].prev = list[i].prev;
}
}
void traverse() {
int i;
if(!count) {
printf("List Empty!\n");
return;
}
i = head_ptr;//list[last_ptr].next;
do {
printf("%d -> ", list[i].data);
i = list[i].next;
} while(i != head_ptr);
printf("END\n");
}
void reverse_traverse() {
int i;
if(!count) {
printf("List Empty!\n");
return;
}
i = last_ptr;
do {
printf("%d -> ", list[i].data);
i = list[i].prev;
} while(i != last_ptr);
printf("START\n");
}
int main() {
int ch, temp;
while(1) {
printf("\n\n");
traverse();
printf("\n1.Sorted Insertion\n2.Delete Smallest\n3.Delete Largest\n4.Delete By Position\n5.Reverse Traversal\n6.Exit\n\n>> ");
scanf("%d", &ch);
switch(ch) {
case 1 :
printf("\nEnter Data : ");
scanf("%d", &temp);
sorted_insert(temp);
break;
case 2 : delete_smallest(); break;
case 3 : delete_largest(); break;
case 4 :
printf("\n Enter Position : ");
scanf("%d", &temp);
delete(temp);
break;
case 5 : reverse_traverse(); break;
case 6 : exit(0);
}
}
}