-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglyLinkedList.c
219 lines (171 loc) · 3.64 KB
/
singlyLinkedList.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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* link;
} *head;
void printLinkedlist() {
if (head == NULL) {
printf("List is empty.");
}
else {
int count = 0;
const struct Node* temp = head;
printf("Elements :");
while (temp != NULL) {
printf("[%d]", temp->data);
temp = temp->link;
count++;
}
printf("\nCount :%d\n", count);
}
}
void addNode(int const value) {
struct Node* ptr = malloc(sizeof(struct Node));
ptr->data = value;
ptr->link = NULL;
if (head == NULL) {
head = ptr;
}
else {
struct Node* temp = head;
while (temp->link != NULL) {
temp = temp->link;
}
temp->link = ptr;
}
}
void addNodeFront(int const value) {
struct Node* ptr = malloc(sizeof(struct Node));
ptr->data = value;
ptr->link = NULL;
if (head == NULL) {
head = ptr;
}
else {
ptr->link = head;
head = ptr;
}
}
void removeNodeFront() {
printf("\nRemoving node @1 :\n");
if (head == NULL) {
printf("List is empty.");
}
else {
struct Node* ptr = head;
head = head->link;
free(ptr);
}
}
void removeNodeRear() {
struct Node* ptr = head;
printf("\nRemoving node @rear :\n");
if (head == NULL) {
printf("List is empty.");
}
else {
while (ptr->link->link != NULL) {
ptr = ptr->link;
}
struct Node* temp = ptr->link;
free(temp);
ptr->link = NULL;
}
}
void addNodeAt(int const index, int const value) {
if (head == NULL) {
printf("List is empty.");
}
else {
struct Node* ptr = head;
for (int i = 0; i < index - 2; i++) {
ptr = ptr->link;
}
struct Node* ptr2 = malloc(sizeof(struct Node));
ptr2->data = value;
ptr2->link = ptr->link;
ptr->link = ptr2;
}
}
void removeNodeAt(int const index) {
if (head == NULL) {
printf("List is empty.");
}
else {
struct Node* ptr = head;
struct Node* ptr2 = head;
for (int i = 0; i < index - 1; i++) {
ptr = ptr->link;
}
for (int i = 0; i < index - 2; i++) {
ptr2 = ptr2->link;
}
ptr2->link = ptr->link;
free(ptr);
}
}
void Menu() {
printf("Choose operation :\n");
printf("[1] Add Nodes\n");
printf("[2] Remove Node from rear\n");
printf("[3] Add Node in front\n");
printf("[4] Remove Node from front\n");
printf("[5] Add Node at index\n");
printf("[6] Remove Node from index\n");
printf("[7] Display the Linked list\n");
printf("[8] Exit\n");
printf("Enter the option :");
}
int main() {
head = NULL;
int option, index, size, data;
do {
Menu();
scanf("%d", &option);
switch (option) {
case 1:
printf("Enter the number of elements :");
scanf("%d", &size);
for (int i = 0; i < size; i++) {
printf("\nEnter the #%d data :", i + 1);
scanf("%d", &data);
addNode(data);
}
break;
case 2:
removeNodeFront();
break;
case 3:
printf("\nEnter the front data :");
scanf("%d", &data);
addNodeFront(data);
break;
case 4:
removeNodeFront();
break;
case 5:
printf("\nEnter the position where you want to insert data :");
scanf("%d", &index);
printf("Enter the data :");
scanf("%d", &data);
addNodeAt(index, data);
break;
case 6:
printf("Enter the index of the element to be removed :");
scanf("%d", &index);
removeNodeAt(index);
break;
case 7:
printLinkedlist();
break;
case 8:
printf("Exting the program...");
break;
default:
printf("Invalid option.");
break;
}
} while (option != 8);
return 0;
}