forked from gihantha/-HactoberFest2020-For_All_Beginers-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleLinked_List
247 lines (245 loc) · 4.58 KB
/
SingleLinked_List
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*********single linked list with various operations*********/
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next; //self referential structure
}*head; //global pointer contains address of first node
typedef struct node node;
void addbeg(int num)
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
newnode->data=num;
newnode->next=NULL;
if(head==NULL) // first node to be implemented
{
head=newnode;
return;
}
else
{ newnode->next=head;
head=newnode;
}
}
void append(int num)
{
node *newnode, *cur;
newnode=(node*)malloc(sizeof(node));
newnode->data=num;
newnode->next=NULL;
if(head==NULL) // first node
{
head=newnode;
return;
}
else
{
cur=head; //head is fixed
while(cur->next!=NULL) //traversing
{
cur=cur->next;
}
cur->next=newnode;
}
}
void addbet(int loc,int num)
{
node *newnode,*cur=head;
newnode=(node*)malloc(sizeof(node));
newnode->data=num;
node *prev;
for(int i=1;i<loc;i++)
{
prev=cur;
cur=cur->next;
}
newnode->next=cur;
prev->next=newnode;
}
int c()
{ int p=0;
node *cur=head;
while(cur!=NULL)
{
p++;
cur=cur->next;
}
return p;
}
void display()
{
node *cur=head;
while(cur!=NULL)
{
printf("%d ",cur->data);
cur=cur->next;
}
}
void search()
{ int num;
node*cur=head;
if(head==NULL)
{
printf("ERROR 404");
return;
}
else
{
printf("enter the number you want to search");
scanf("%d",&num);
}
while(cur!= NULL)
{
if(cur->data==num)
{
printf("Element found %d",num);
return;
}
cur=cur->next; //increment
}
printf(" %d is the Element not found in the list",num);
}
void delnode(int num) //delete the nodes
{
node *cur=head,*prev;
while(cur!=NULL)
{
if(cur->data ==num)
{
if(cur==head)
{
head=cur->next;
free(cur);
printf("Element deleted\n");
return;
}
else
{
prev->next=cur->next;
free(cur);
printf("Element deleted\n");
return;
}
}
else
{
prev=cur;
cur=cur->next;
}
}
printf("Element %d not found",num);
}
void delloc(int loc)
{
int i;
node *prev,*cur;
cur=head;
if(loc>c() ||loc<=0 || head==NULL)
{
printf("\n Deletion is not possible :");
return;
}
if(loc==1)
{
head=cur->next;
printf("%d deleted",cur->data);
free(cur);
return;
}
else
{
for(i=1;i<loc;i++)
{
prev=cur;
cur=cur->next;
}
prev->next=cur->next;
free(cur);
return;
}
}
void reverse()
{
node *left,*right,*cur;
cur=head;
right=NULL;
while(cur!=NULL)
{
left=right;
right=cur;
cur=cur->next;
right->next=left;
}
head=right;
}
int main()
{
int ch,num,loc,k,data;
node *head=NULL;
do
{
printf("\n Options displayed:\n1)Insert at beginning\n2)Insert at last\n3)Insert at any particular location\n4)Count the nodes\n5)Reverse the nodes\n6)Search any element\n7)Delete the node\n8)Delete the node at any particular location\n9)Display\n");
printf("Enter your choice:\n ");
scanf("%d",&ch);
switch(ch)
{
case 1: { printf("Enter the number:");
scanf("%d",&num);
addbeg(num);
break;
}
case 2: { printf("Enter the number:");
scanf("%d",&num);
append(num);
break;
}
case 3:
{ printf("Enter the location");
scanf("%d",&loc);
if(loc<1 || (loc>c()+1))
printf("\n Invalid loc!");
else
{
printf("Enter the number");
scanf("%d",&num);
if(loc==1)
addbeg(num);
else if (loc ==c()+1)
append(num);
else
addbet(loc,num);
}
}
case 4: { k=c();
printf("No. of elements in the list is %d",k);
break;
}
case 5: { reverse();
printf("list is reversed");
break;
}
case 6: {
search();
break;
}
case 7: {printf("Enter the data to be deleted");
scanf("%d",data);
delnode(data);
break;
}
case 8: { printf("Enter the location");
scanf("%d",&loc);
delloc(loc);
break;
}
case 9: display();
break;
default : printf("INVALID CHOICE!");
break;
}
}
while(ch>0 &&ch<10);
}