-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingly.c
291 lines (286 loc) · 5.56 KB
/
singly.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node * head=NULL, *nw, *ptr, *temp,*last;
void create() // creates a linked list by askiing user for number of nodes
{
int i,n;
printf("Enter the no of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
nw = (struct node *)malloc(sizeof(struct node));
printf("Enter the node data");
scanf("%d",&nw->data);
nw->next=NULL;
if(head ==NULL)
{
head = ptr=nw;
ptr->next = head;
}
else
{
ptr-> next = nw;
ptr= ptr-> next;
ptr->next = head;
}
}
}
void display() // used for displaying the linked list
{
ptr= head;
printf("\n Link list elements are \n");
while(ptr-> next!= head)
{
printf("\t %d \t",ptr-> data);
ptr= ptr->next;
}
}
void insert() // used to insert new node
{
int pos,count=1;
ptr = head;
last = head;
while(last->next!=head)
{
last= last->next;
}
printf("\nEnter the position \t");
scanf("%d",&pos);
nw=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the node data\t");
scanf("%d",&nw->data);
nw-> next =NULL;
if(pos==1)
{
nw->next=head;
head=nw;
last->next=head;
}
else
{
while(count!= pos-1)
{
ptr = ptr-> next;
count++;
}
nw-> next = ptr->next ;
ptr->next=nw;
}
}
void delete() // used to delete any node
{
int pos,count=1;
ptr=head;
last = head;
while(last->next!=head)
{
last= last->next;
}
printf("\nEnter the position\t");
scanf("%d",&pos);
if(pos==1)
{
temp=head;
head = head->next;
free(temp);
last->next = head;
}
else
{
while(count!= pos-1)
{
ptr=ptr->next;
count++;
}
temp = ptr-> next;
ptr->next=temp->next;
free(temp);
}
}
void search() //used to search any element in list
{
int ele,flag=0;
ptr= head;
printf("\nEnter element to be serched\t");
scanf("%d",&ele);
while(ptr-> next != head)
{
if(ptr-> data == ele)
{
flag =1;
break;
}
else
{
ptr = ptr->next;
}
if(ptr->data == ele)
{
flag = 1;
break;
}
}
if(flag==1)
{
printf("\nElement Found\n");
}
else
{
printf("\nElement Not Found\n");
}
}
void delete_val() //deletes a node by asking the values
{
int ele, flag =0;
ptr=temp=head;
printf("\nEnter the element\t");
scanf("%d",&ele);
while(temp!=NULL)
{
if(temp->data==ele)
{
flag =1;
if(temp==head)
{
head = head->next;
free(temp);
}
else
{
while(ptr->next != temp)
{
ptr= ptr->next;
}
ptr->next=temp->next;
free(temp);
}
}
else
{
temp=temp->next;
}
}
if(flag==1)
{
printf("\nElement found and deleted\n");
}
else
{
printf("\nElement not found \n");
}
}
void reverse() // used to reverse the list
{
struct node *p,*q,*r;
p=q=r=head;
p=q->next->next;
q=q->next;
r->next=NULL;
q->next=r;
while(p!=NULL)
{
r=q;
q=p;
p=p->next;
q->next=r;
}
head =q;
printf("\nLink List element\n");
display();
}
void insert1() // takes values one by one without asking for the number of nodes.
{
ptr = head;
nw = (struct node *)malloc(sizeof(struct node));
printf("Enter the node data");
scanf("%d",&nw->data);
nw->next=NULL;
if(head==NULL)
{
head=nw;
}
else if(head->data >= nw->data)
{
nw->next= head;
head = nw;
}
else
{
ptr = head;
while(ptr-> next!= NULL && ptr-> data)
{
ptr= ptr-> next;
nw->next= ptr->next;
ptr->next=nw;
}
}
}
void search1() //used to search any element in list
{
int ele,flag=0;
ptr= head;
printf("\nEnter element to be serched\t");
scanf("%d",&ele);
while(ptr!=NULL)
{
if(ptr-> data == ele)
{
flag =1;
break;
}
else if(ptr->data >ele)
{
flag =0;
break;
}
else
{
ptr = ptr-> next;
}
}
if(flag==1)
{
printf("\nElement Found\n");
}
else
{
printf("\nElement Not Found\n");
}
}
void main()
{
int ch;
do
{
printf("\n1: CREATE");
printf("\n2: DISPLAY");
printf("\n3: INSERT");
printf("\n4: DELETE");
printf("\n5: SEARCH");
printf("\n6: EXIT");
printf("\nEnter your choice\t");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: delete();
break;
case 5: search();
break;
break;
case 6: exit(0);
default: printf("Invalid Choice");
}
}
while(ch!=7);
}