-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList
218 lines (203 loc) · 4.81 KB
/
LinkedList
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
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
};
struct Node *head = NULL;
void insertFront(int data){
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
if (head == NULL){
newNode->next = NULL;
head = newNode;
}
else{
newNode->next = head;
head = newNode;
}
}
void insertBack(int data){
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL){
head = newNode;
}
else{
struct Node *ptr = head;
while(ptr->next!=NULL){
ptr = ptr->next;
}
ptr->next = newNode;
}
}
void insertAt(int pos, int data){
int found = 0;
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (head == NULL){
found = 0;
}
else{
int count=0;
struct Node *ptr = head;
while(ptr!=NULL){
count++;
if (count== pos-1){
found = 1;
struct Node *temp = ptr->next;
newNode->data = data;
ptr->next = newNode;
newNode->next = temp;
break;
}
else
ptr = ptr->next;
}
}
if (found == 0){
printf("\nNo such position available");
}
}
void deleteFront(){
struct Node *temp;
if (head == NULL)
printf("Empty list\n");
else{
temp = head;
printf("Deleted: %d",temp->data);
head = head->next;
free(temp);
}
}
void deleteBack(){
struct Node *temp, *ptr;
if (head == NULL)
printf("List empty\n");
else{
ptr = head;
while (ptr->next!=NULL){
temp = ptr;
ptr = ptr->next;
}
temp->next = NULL;
free(ptr);
}
}
void deleteAt(int pos){
struct Node *temp, *ptr;
int found = 0, count = 0;
ptr = head;
if (head == NULL)
found = 0;
else{
if (pos == 1){
found = 1;
temp = head;
head = head->next;
printf("\nDeleted: %d",temp->data);
free(temp);
}
else{
count = 1;
while (ptr!=NULL && count<pos){
temp = ptr;
ptr = ptr->next;
count++;
}
if (ptr == NULL){
found = 0;
}
else{
found = 1;
temp->next = ptr->next;
printf("Deleted: %d",ptr->data);
free(ptr);
}
}
}
if (found == 0)
printf("\nNo such position found!");
}
void display(){
struct Node *ptr = head;
while(ptr!=NULL){
printf("%d->",ptr->data);
ptr = ptr->next;
}
printf("X\n");
}
struct Node* reverse(){
struct Node *next = NULL, *prev = NULL;
struct Node *curr = head;
while(curr!=NULL){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
int main()
{
int ch, element, pos;
struct Node *newHead, *curr;
do
{
printf("\n\nChoose operation");
printf("\n1.Insert at beginning");
printf("\n2.Insert at end");
printf("\n3.Insert at position");
printf("\n4.Delete from front");
printf("\n5.Delete from back");
printf("\n6.Delete from a position");
printf("\n7.Display linked list");
printf("\n8.Reverse");
printf("\n\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter element to insert ");
scanf("%d", &element);
insertFront(element);
break;
case 2:
printf("\nEnter element to insert ");
scanf("%d", &element);
insertBack(element);
break;
case 3:
printf("\nEnter the position after which node will be inserted ");
scanf("%d", &pos);
printf("\nEnter element to insert ");
scanf("%d", &element);
insertAt(pos, element);
break;
case 4:
deleteFront();
break;
case 5:
deleteBack();
break;
case 6:
printf("\nEnter position of element to delete ");
scanf("%d", &pos);
deleteAt(pos);
break;
case 7:
display();
break;
case 8:
newHead = reverse();
curr = newHead;
while(curr!=NULL){
printf("%d->",curr->data);
curr = curr->next;
}
printf("X\n");
break;
}
} while (ch < 9);
return 0;
}