-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab4_1.c
201 lines (195 loc) · 5.92 KB
/
lab4_1.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
/*Implement the following operations on a single linked list using C.
Create
traverse
Insert
Delete
reverse
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
} * first;
void create(int a)
{
struct node *temp, *ptr;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = a;
temp->next = NULL;
if (first == NULL)
{
first = temp;
}
else
{
ptr = first;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
void display()
{
struct node *ptr;
if (first == NULL)
{
printf("Linked list is empty \n");
return;
}
else
{
ptr = first;
printf("Elements: \n");
while (ptr != NULL)
{
printf("%d \n", ptr->data);
ptr = ptr->next;
}
}
}
void insert(int a, int pos, int n)
{
struct node *ptr, *temp;
ptr = first;
temp = (struct node *)malloc(sizeof(struct node));
if (first == NULL)
{
printf("List is already empty \n");
return;
}
else if (pos == 1)
{
temp->data = a;
temp->next = first;
first = temp;
}
else if (pos == n)
{
while (ptr->next != NULL)
ptr = ptr->next;
temp->data = a;
temp->next = NULL;
ptr->next = temp;
}
else
{
for (int i = 1; i < (pos - 1); i++)
{
ptr = ptr->next;
}
temp->data = a;
temp->next = ptr->next;
ptr->next = temp;
}
printf("Elements after insertion: \n");
display();
}
void delete (int pos, int n)
{
struct node *ptr, *temp;
ptr = first;
if (first == NULL)
{
printf("List is already empty \n");
return;
}
else if (pos == 1)
{
temp = first;
first = first->next; //first moves to the next element
free(temp); //we delete the first element
temp = NULL; //delete the address of the deleted element of LL
}
else if (pos == n)
{
while (ptr->next != NULL)
{
temp = ptr;
ptr = ptr->next;
}
temp->next = NULL;
free(ptr);
ptr = NULL;
}
else
{
temp = first;
for (int i = 0; i < (pos - 1); i++)
{
temp = ptr;
ptr = ptr->next;
}
temp->next = ptr->next;
free(ptr);
ptr = NULL;
}
printf("Elements after deletion");
display();
}
void reverse()
{
struct node *temp, *temp1;
temp = NULL;
while (first != NULL)
{
temp1 = first->next;
first->next = temp;
temp = first;
first = temp1;
}
first = temp;
printf("Reversed linkedlist \n");
display();
}
int main()
{
int a, c = 0, n, y, v, s, p;
struct node *first = NULL;
while (c < 6)
{
printf("Enter 1 to create a single linked list \n Enter 2 to traverse a single linked list \n Enter 3 to insert in a single linked list \n Enter 4 to delete a single linked list \n Enter 5 to reverse a single linked list \n");
printf(" \n Press 6 to Exit \n");
printf("Enter your choice: \n");
scanf("%d", &a);
switch (a)
{
case 1:
printf("Enter the number of nodes to be created \n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter data \n");
scanf("%d", &v);
create(v);
}
break;
case 2:
display();
break;
case 3:
printf("Enter data \n");
scanf("%d", &s);
printf("Enter position \n");
scanf("%d", &p);
insert(s, p, n);
break;
case 4:
printf("Enter the position to be deleted \n");
scanf("%d", &y);
delete (y, n);
break;
case 5:
reverse();
break;
case 6:
exit(1);
default:
printf("Wrong choice!Try again \n");
}
if (a > 0 && a < 7)
c++;
}
return 0;
}