This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p17_2910.c
210 lines (184 loc) · 3.79 KB
/
p17_2910.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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 17/2910
// Circular Singly Linked List
// Dynamic Memory Allocation
// Please review program before execution.
// Incomplete or Error Prone !
#include<stdio.h>
#include<stdlib.h>
void ins_b();
void ins_e();
void ins_p();
void del_b();
void del_e();
void del_p();
void display();
void exit_free();
struct node{
int data;
struct node *link;
};
struct node *start = NULL;
struct node *temp;
void main(){
int ch;
while(1){
printf("\n\n*** CIRCULAR SINGLY LINKED LIST [C - SLL] ***");
printf("\nInsert at >> 1. Beginning 2. End 3. Position");
printf("\nDelete at >> 4. Beginning 5. End 6. Position");
printf("\n >> 7. Display 8. Exit");
printf("\nEnter choice: ");
scanf("%d", &ch);
switch(ch){
case 1: ins_b(); break;
case 2: ins_e(); break;
case 3: ins_p(); break;
case 4: del_b(); break;
case 5: del_e(); break;
case 6: del_p(); break;
case 7: display(); break;
default: exit_free(); return;
}
}
}
// Insertion at beginning
// Choice 1
void ins_b(){
int d;
struct node *a = malloc(sizeof(struct node));
printf("\tEnter data: ");
scanf("%d", &(a->data));
a->link = start;
start = a;
printf("\n\tSuccessfully inserted %d at beginning", a->data);
}
// Insertion at end
// Choice 2
void ins_e(){
struct node *a = malloc(sizeof(struct node));
printf("\tEnter data: ");
scanf("%d", &(a->data));
a->link = NULL;
if(start == NULL){
a->link = start;
start = a;
}
else{
for(temp = start; temp->link != NULL; temp = temp->link);
temp->link = a;
}
printf("\n\tSuccessfully inserted %d at end", a->data);
}
// Insertion at specific valid position
// Choice 3
void ins_p(){
int pos, i = 1;
printf("\tEnter position: ");
scanf("%d", &pos);
if(pos == 1){
ins_b();
return;
}
for(temp = start; temp != NULL && i < (pos - 1); i++){
temp = temp->link;
}
if(temp != NULL){
struct node *a = malloc(sizeof(struct node));
printf("\tEnter data: ");
scanf("%d", &(a->data));
a->link = temp->link;
temp->link = a;
printf("\n\tSuccessfully inserted %d at position %d", a->data, pos);
}
else{
printf("\tError in position!");
}
}
// Deletion of node at start
// Choice 4
void del_b(){
if(start == NULL){
printf("\n\tSLL is Empty");
}
else{
temp = start;
start = start->link;
printf("\n\tSuccessfully deleted %d from the beginning", temp->data);
free(temp);
}
}
// Deletion of node at end
// Choice 5
void del_e(){
if(start == NULL){
printf("\n\tSLL is Empty");
}
else if(start->link == NULL){
del_b();
}
else{
for(temp = start; (temp->link)->link != NULL; temp = temp->link);
printf("\n\tSuccessfully deleted %d from the end", (temp->link)->data);
free(temp->link);
temp->link = NULL;
}
}
// Deletion of node at specific position
// Choice 6
void del_p(){
if (start == NULL){
printf("\n\tSLL is Empty");
return;
}
int pos;
printf("\tEnter position: ");
scanf("%d", &pos);
if(pos <= 0){
printf("\tError in position! Do not enter index");
return;
}
if(pos == 1){
del_b();
return;
}
int i = 1;
for(temp = start; temp != NULL && i < (pos - 1); i++){
temp = temp->link;
}
struct node *loc;
loc = temp->link;
if(temp != NULL && loc != NULL){
temp->link = loc->link;
printf("\n\tSuccessfully deleted %d from position %d", loc->data, pos);
free(loc);
}
else{
printf("\tError in position!");
}
}
// Displaying all nodes - Start to NULL
// Choice 7
void display(){
printf("\n\tCuurent SLL >>>");
for(temp = start; temp != NULL; temp = temp->link){
printf(" %d ->", temp->data);
}
printf(" NULL");
}
// Final de-allocation of SLL
// Choice 8
void exit_free(){
struct node *loc;
if(start != NULL){
temp = start;
loc = temp->link; // Next node
while(loc != NULL){
free(temp);
temp = loc;
loc = loc->link;
}
free(temp);
}
}