-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
singly_link_list_deletion.c
177 lines (175 loc) · 4.17 KB
/
singly_link_list_deletion.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
/*Includes structure for a node which can be use to make new nodes of the Linked
List. It is assumed that the data in nodes will be an integer, though function
can be modified according to the data type, easily. deleteNode deletes a node
when passed with a key of the node.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct node
{
int info;
struct node *link;
};
struct node *start = NULL;
//////////////////////////////////////////////////////////////////
struct node *createnode() // function to create node
{
struct node *t;
t = (struct node *)malloc(sizeof(struct node));
return (t);
}
//////////////////////////////////////////////////////////////////
int insert(int pos, int d)
{
struct node *new;
new = createnode();
new->info = d;
if (pos == 1)
{
new->link = NULL;
if (start == NULL)
{
start = new;
}
else
{
new->link = start;
start = new;
}
}
else
{
struct node *pre = start;
for (int i = 2; i < pos; i++)
{
if (pre == NULL)
{
break;
}
pre = pre->link;
}
if(pre==NULL)
{
printf("Position not found!");
return 0;
}
new->link = pre->link;
pre->link = new;
}
return 0;
}
///////////////////////////////////////////////////////////////////
int deletion(int pos) // function to delete from any position
{
struct node *t;
if (start == NULL)
{
printf("\nlist is empty");
}
else
{
if (pos == 1)
{
struct node *p;
p = start;
start = start->link;
free(p);
}
else
{
struct node *prev = start;
for (int i = 2; i < pos; i++)
{
if (prev == NULL)
{
printf("Position not found!");
return 0;
}
prev = prev->link;
}
struct node *n = prev->link; // n points to required node to be deleted
prev->link = n->link;
free(n);
}
}
return 0;
}
///////////////////////////////////////////////////////////////////
void viewlist() // function to display values
{
struct node *p;
if (start == NULL)
{
printf("\nlist is empty");
}
else
{
p = start;
while (p != NULL)
{
printf("%d ", p->info);
p = p->link;
}
}
}
//////////////////////////////////////////////////////////////////
static void test()
{
insert(1, 39);
assert(start->info == 39);
insert(2, 10);
insert(3, 11);
deletion(1);
assert(start->info != 39);
printf("Self-tests successfully passed!\n");
}
//////////////////////////////////////////////////////////////////
int main()
{
int n = 0, pos = 0, p = 0, num = 0, c = 0;
printf("\n1.self test mode");
printf("\n2.interactive mode");
printf("\nenter your choice:");
scanf("%d", &c);
if (c == 1)
{
test();
}
else if (c == 2)
{
while (1)
{
printf("\n1.add value at the given location");
printf("\n2.delete value at the given location");
printf("\n3.view list");
printf("\nenter your choice :");
scanf("%d", &n);
switch (n)
{
case 1:
printf("enter the position where the element is to be added :");
scanf("%d", &p);
printf("enter the element is to be added :");
scanf("%d", &num);
insert(p, num);
break;
case 2:
printf("enter the position where the element is to be deleted :");
scanf("%d", &pos);
deletion(pos);
break;
case 3:
viewlist();
break;
default:
printf("\ninvalid choice");
}
}
}
else
{
printf("Invalid choice");
}
return 0;
}