forked from gdscpce/hacktober-codesnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsll.c
233 lines (229 loc) · 4.7 KB
/
sll.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void InsertAtLast(struct node **head, int data)
{
struct node *n, *t;
n = (struct node *)malloc(sizeof(struct node));
n->data = data;
n->next = NULL;
if (*head == NULL)
{
*head = n;
}
else
{
t = *head;
while (t->next != NULL)
{
t = t->next;
}
t->next = n;
}
}
void InsertAtBeginning(struct node **head, int data)
{
struct node *n, *t;
n = (struct node *)malloc(sizeof(struct node));
n->data = data;
n->next = NULL;
if (*head == NULL)
{
*head = n;
}
else
{
n->next = (*head);
(*head) = n;
}
}
void InsertAtPosition(struct node **head, int pos, int data)
{
struct node *n, *t, *bck;
int count = 0;
n = (struct node *)malloc(sizeof(struct node));
n->data = data;
n->next = NULL;
if (*head == NULL && pos == 1)
{
*head = n;
}
else
{
t = *head;
bck = NULL;
while (t->next != NULL && count < pos - 1)
{
bck = t;
t = t->next;
count += 1;
}
if (count + 1 == pos - 1 && t->next == NULL)
{
t->next = n;
}
else if (t->next == NULL && count > pos)
{
printf("\nInvalid Position");
return;
}
else
{
n->next = bck->next;
bck->next = n;
}
}
}
void ShowList(struct node **head)
{
struct node *t;
t = *head;
if (*head == NULL)
{
printf("\nList is Empty");
return;
}
while (t->next != NULL)
{
printf(" %d ", t->data);
t = t->next;
}
printf(" %d ", t->data);
}
void DeleteAtFirst(struct node **head)
{
struct node *t, *r;
t = *head;
if (*head == NULL)
{
printf("\nList is Empty !\t");
return;
}
else
{
r = (*head);
(*head) = (*head)->next;
free(r);
}
}
void DeleteAtLast(struct node **head)
{
struct node *t, *bck, *r;
if (*head == NULL)
{
printf("\nList is Empty !\t");
return;
}
else if ((*head)->next == NULL)
{
r = (*head);
(*head) = (*head)->next;
free(r);
}
else
{
t = *head;
while (t->next != NULL)
{
bck = t;
t = t->next;
}
r = t;
bck->next = NULL;
free(r);
}
}
void DeleteAtPosition(struct node **head, int pos)
{
struct node *t, *r, *bck;
int count = 0;
if (*head == NULL)
{
printf("\nList is Empty !\t");
return;
}
else if ((*head)->next == NULL && pos == 1)
{
r = (*head);
(*head) = (*head)->next;
free(r);
}
else
{
t = *head;
while (t->next != NULL && count < pos - 1)
{
bck = t;
t = t->next;
count += 1;
}
if (t->next == NULL && count > pos - 1)
{
printf("\nInvalid Position");
return;
}
else
{
r = bck->next;
bck->next = t->next;
free(r);
}
}
}
int main()
{
struct node *head = NULL;
int data, choice, pos;
while (1)
{
system("CLS");
printf("1-InsertFirst \n 2-InsertLast \n 3-Insert At Position \n 4-DeleteFirst \n 5-Delete at Last \n 6-Delete at pos \n 7-View List");
printf("\nEnter Your Choice:\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nInsert Data :");
scanf("%d", &data);
InsertAtBeginning(&head, data);
break;
case 2:
printf("\nInsert Data :");
scanf("%d", &data);
InsertAtLast(&head, data);
break;
case 3:
printf("\nInsert Data:");
scanf("%d", &data);
printf("\nEnter position at which you want to insert data:");
scanf("%d", &pos);
InsertAtPosition(&head, pos, data);
break;
case 4:
DeleteAtFirst(&head);
printf("\nValue Deleted");
break;
case 5:
DeleteAtLast(&head);
printf("\nValue Deleted");
break;
case 6:
printf("\nEnter position to delete:");
scanf("%d", &pos);
DeleteAtPosition(&head, pos);
break;
case 7:
ShowList(&head);
break;
default:
printf("Invalid Choice");
break;
}
system("PAUSE");
}
}