-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.c
211 lines (203 loc) · 4.43 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Write a program to implement linked list operation(creation,insertion,deletion,reversing)
#include <stdio.h>
#include <stdlib.h>
typedef struct ListItem // node definition
{
int data;
struct ListItem *link;
} ListItem;
typedef struct List
{
ListItem *root;
} List;
ListItem *createNode(int data)
{
ListItem *node;
node = malloc(sizeof(ListItem));
node->data = data;
node->link = NULL;
return node;
}
void inserItem(List *l1, int item)
{
ListItem *temp, *traverse;
temp = createNode(item);
if (temp == NULL)
{
printf("\n\nmemory error");
return;
}
if (l1->root == NULL)
{
l1->root = temp;
return;
}
else
{
traverse = l1->root;
while (traverse->link != NULL)
traverse = traverse->link;
traverse->link = temp;
}
}
void insertAt(List *l1, int pos, int item)
{
ListItem *temp, *traverse;
int i = 1;
temp = createNode(item);
if (temp == NULL)
{
printf("\n\nMemory error");
return;
}
if (pos < 1)
{
printf("\n\nInvalid position");
}
else if (pos == 1)
{
temp->link = l1->root;
l1->root = temp;
}
else
{
traverse = l1->root;
while (traverse->link != NULL && i < pos - 1)
{
traverse = traverse->link;
i++;
}
if (i != pos - 1)
{
printf("\n\ninvalid position ");
return;
}
temp->link = traverse->link;
traverse->link = temp;
}
}
void deletionItem(List *l1)
{
ListItem *temp, *traverse;
if (l1->root == NULL)
{
printf("\n\nEMPTY LIST");
return;
}
traverse = l1->root;
if (traverse->link == NULL)
{
temp = traverse;
printf("\n\n Deleted Item: %d ", temp->data);
l1->root = NULL;
free(temp);
return;
}
while (traverse->link->link != NULL)
{
traverse = traverse->link;
temp = traverse->link;
traverse->link = NULL;
printf("\n\ndeleted items: %d", temp->data);
free(temp);
}
}
void display(List *l1)
{
ListItem *traverse;
traverse = l1->root;
printf("\n\nROOT");
while (traverse != NULL)
{
printf("%d ", traverse->data);
traverse = traverse->link;
}
printf("END");
}
void reversePrint(List *l1)
{
ListItem *traverse = NULL, *temp = NULL;
temp = l1->root;
while (temp != traverse)
{
while (temp->link != traverse)
temp = temp->link;
printf("%d", temp->data);
traverse = temp;
temp = l1->root;
}
}
void reverseList(List *l1)
{
ListItem *trav, *temp, *newroot;
if (l1->root == NULL || l1->root->link == NULL)
{
return;
}
trav = l1->root;
while (trav->link != NULL) // find the new root
trav = trav->link;
newroot = trav;
while (trav != l1->root)
{
temp = l1->root;
while (temp->link != trav)
{
temp = temp->link;
}
trav->link = temp;
trav = temp;
}
trav->link = NULL;
l1->root = newroot;
printf("\n\n\n%d", newroot->data);
}
int main()
{
List l1;
int opt;
int x, pos;
l1.root = NULL;
while (1)
{
printf("\n\n\t\t\tLINKED LIST OPERATIONS\n\n");
printf("\n\n\t\t\t1.ADD ITEM");
printf("\n\n\t\t\t2.DELETE ITEM");
printf("\n\n\t\t\t3.REVERSE ITEM");
printf("\n\n\t\t\t4.SHOW ITEMS");
printf("\n\n\t\t\t5.REVERSE PRINT");
printf("\n\n\t\t\t6.INSERT ITEMS");
printf("\n\n\t\t\t7.EXIT");
printf("\n\n select the operation:");
scanf("%d", &opt);
switch (opt)
{
case 1:
printf("\\n\n enter the element to insert into list:");
scanf("%d", &x);
inserItem(&l1, x);
break;
case 2:
deletionItem(&l1);
break;
case 3:
reverseList(&l1);
break;
case 4:
display(&l1);
break;
case 5:
reversePrint(&l1);
break;
case 6:
printf("\n\n enter the element to insert into list:");
scanf("%d", &x);
printf("\n\n enter the position:");
scanf("%d", &pos);
insertAt(&l1, pos, x);
break;
case 7:
return 0;
}
}
}