-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathlinked_list.c
197 lines (184 loc) · 5.61 KB
/
linked_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
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
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} node;
node *start = NULL, *new_node, *temp = NULL, *prev = NULL;
void create();
void insert_begn();
void insert_end();
void insert_bef();
void insert_aft();
void insert_pos();
void delete_begn();
void delete_end();
void delete_bef();
void delete_aft();
void delete_pos();
void display();
int count();
int main() {
printf(" 1. Insert at Beginning\n");
printf(" 2. Insert at End\n");
printf(" 3. Delete from Beginning\n");
printf(" 4. Delete from End\n");
printf(" 5. Insert before Node\n");
printf(" 6. Insert after Node\n");
printf(" 7. Insert at Position\n");
printf(" 8. Delete before Node\n");
printf(" 9. Delete after Node\n");
printf("10. Delete at Position\n");
printf("11. Display\n");
printf("12. Count Items\n");
printf("13. Exit\n");
while (1) {
int c; printf("\nEnter choice: "); scanf("%d", &c);
switch (c) {
case 1: insert_begn(); break;
case 2: insert_end(); break;
case 3: delete_begn(); break;
case 4: delete_end(); break;
case 5: insert_bef(); break;
case 6: insert_aft(); break;
case 7: insert_pos(); break;
case 11: display(); break;
case 12: count(); break;
case 13: exit(1);
default: printf("Wrong choice!\n");
}
}
return 0;
}
void create() {
int n; printf("Enter data: "); scanf("%d", &n);
new_node = (node*)malloc(sizeof(node)); // malloc to init structure since new_node is pointer type variable
new_node->data = n;
new_node->next = NULL;
}
void display() {
if (start == NULL) {
printf("Underflow!");
} else {
temp = start;
printf("Linked list elements: START -> ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("END");
}
printf("\n");
}
int count() {
int c = 0;
temp = start;
while (temp != NULL) {
c++;
temp = temp->next;
}
printf("Item count: %d\n", c);
return c;
}
void insert_begn() {
create();
if (start == NULL) {
start = new_node;
} else {
new_node->next = start;
start = new_node;
}
}
void insert_end() {
create();
if (start == NULL) {
start = new_node;
} else {
temp = start;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
}
}
void delete_begn() {
if (start == NULL) {
printf("Underflow!");
} else {
temp = start;
printf("Deleted: %d", temp->data);
start = temp->next;
}
printf("\n");
}
void delete_end() {
if (start == NULL) {
printf("Underflow!");
} else {
if (start->next == NULL) { // check if 1st node
printf("Deleted: %d", start->data);
start = NULL;
} else {
temp = start;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
printf("Deleted: %d", temp->data);
prev->next = NULL;
}
}
printf("\n");
}
void insert_bef() {
int data; printf("Enter data item to insert before: "); scanf("%d", &data);
temp = start;
while (temp != NULL && temp->data != data) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Data item %d not found!\n", data);
} else if (!prev) { // if prev is not defined insert at beginning
insert_begn();
} else {
create();
new_node->next = prev->next;
prev->next = new_node;
}
}
void insert_aft() {
int data; printf("Enter data item to insert after: "); scanf("%d", &data);
temp = start;
while (temp != NULL && temp->data != data) {
temp = temp->next;
}
if (temp == NULL) {
printf("Data item %d not found!\n", data);
} else {
create();
new_node->next = temp->next;
temp->next = new_node;
}
}
void insert_pos() {
int c = count();
int pos; printf("Enter position to insert at: "); scanf("%d", &pos);
if (pos == 1) {
insert_begn();
} else if (pos == c) {
insert_end();
} else if (pos > 1 && pos < c) {
int i = 1;
temp = start;
while (temp != NULL && i < pos) { // iterating pos times
temp = temp->next;
i++;
}
create();
new_node->next = temp->next; // inserting after node
temp->next = new_node;
} else {
printf("Invalid postion!\n");
}
}