Sie sind auf Seite 1von 7

#include <stdio.h>#include <malloc.

h>
1.#define ISEMPTY printf("\nEMPTY LIST:");
2./*
3. * Node Declaration
4. */
5.struct node
6.{
7. int value;
8. struct node *next;
9.};
10.  
11. snode* create_node(int);
12. void insert_node_first();
13. void insert_node_last();
14. void insert_node_pos();
15. void sorted_ascend();
16. void delete_pos();
17. void search();
18. void update_val();
19. void display();
20. void rev_display(snode *);
21.  
22. typedef struct node snode;
23. snode *newnode, *ptr, *prev, *temp;
24. snode *first = NULL, *last = NULL;
25.  
26. /*
27.  * Main :contains menu
28.  */
29.  
30. int main()
31. {
32. int ch;
33. char ans = 'Y';
34.  
35. while (ans == 'Y'||ans == 'y')
36. {
37. printf("\n---------------------------------\n");
38. printf("\nOperations on singly linked list\n");
39. printf("\n---------------------------------\n");
40. printf("\n1.Insert node at first");
41. printf("\n2.Insert node at last");
42. printf("\n3.Insert node at position");
43. printf("\n4.Sorted Linked List in Ascending Order");
44. printf("\n5.Delete Node from any Position");
45. printf("\n6.Update Node Value");
46. printf("\n7.Search Element in the linked list");
47. printf("\n8.Display List from Beginning to end");
48. printf("\n9.Display List from end using Recursion");
49. printf("\n10.Exit\n");
50. printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
51. printf("\nEnter your choice");
52. scanf("%d", &ch);
53.  
54. switch (ch)
55. {
56. case 1:
57. printf("\n...Inserting node at first...\n");
58. insert_node_first();
59. break;
60. case 2:
61. printf("\n...Inserting node at last...\n");
62. insert_node_last();
63. break;
64. case 3:
65. printf("\n...Inserting node at position...\n");
66. insert_node_pos();
67. break;
68. case 4:
69. printf("\n...Sorted Linked List in Ascending
Order...\n");
70. sorted_ascend();
71. break;
72. case 5:
73. printf("\n...Deleting Node from any
Position...\n");
74. delete_pos();
75. break;
76. case 6:
77. printf("\n...Updating Node Value...\n");
78. update_val();
79. break;
80. case 7:
81. printf("\n...Searching Element in the List...\n");
82. search();
83. break;
84. case 8:
85. printf("\n...Displaying List From Beginning to
End...\n");
86. display();
87. break;
88. case 9:
89. printf("\n...Displaying List From End using
Recursion...\n");
90. rev_display(first);
91. break;
92. case 10:
93. printf("\n...Exiting...\n");
94. return 0;
95. break;
96. default:
97. printf("\n...Invalid Choice...\n");
98. break;
99. }
100. printf("\nYOU WANT TO CONTINUE (Y/N)");
101. scanf(" %c", &ans);
102. }
103. return 0;
104. }
105.  
106. /*
107.  * Creating Node
108.  */
109. snode* create_node(int val)
110. {
111. newnode = (snode *)malloc(sizeof(snode));
112. if (newnode == NULL)
113. {
114. printf("\nMemory was not allocated");
115. return 0;
116. }
117. else
118. {
119. newnode->value = val;
120. newnode->next = NULL;
121. return newnode;
122. }
123. }
124.  
125. /*
126.  * Inserting Node at First
127.  */
128. void insert_node_first()
129. {
130. int val;
131.  
132. printf("\nEnter the value for the node:");
133. scanf("%d", &val);
134. newnode = create_node(val);
135. if (first == last && first == NULL)
136. {
137. first = last = newnode;
138. first->next = NULL;
139. last->next = NULL;
140. }
141. else
142. {
143. temp = first;
144. first = newnode;
145. first->next = temp;
146. }
147. printf("\n----INSERTED----");
148. }
149.  
150. /*
151.  * Inserting Node at Last
152.  */
153. void insert_node_last()
154. {
155. int val;
156.  
157. printf("\nEnter the value for the Node:");
158. scanf("%d", &val);
159. newnode = create_node(val);
160. if (first == last && last == NULL)
161. {
162. first = last = newnode;
163. first->next = NULL;
164. last->next = NULL;
165. }
166. else
167. {
168. last->next = newnode;
169. last = newnode;
170. last->next = NULL;
171. }
172. printf("\n----INSERTED----");
173. }
174.  
175. /*
176.  * Inserting Node at position
177.  */
178. void insert_node_pos()
179. {
180. int pos, val, cnt = 0, i;
181.  
182. printf("\nEnter the value for the Node:");
183. scanf("%d", &val);
184. newnode = create_node(val);
185. printf("\nEnter the position ");
186. scanf("%d", &pos);
187. ptr = first;
188. while (ptr != NULL)
189. {
190. ptr = ptr->next;
191. cnt++;
192. }
193. if (pos == 1)
194. {
195. if (first == last && first == NULL)
196. {
197. first = last = newnode;
198. first->next = NULL;
199. last->next = NULL;
200. }
201. else
202. {
203. temp = first;
204. first = newnode;
205. first->next = temp;
206. }
207. printf("\nInserted");
208. }
209. else if (pos>1 && pos<=cnt)
210. {
211. ptr = first;
212. for (i = 1;i < pos;i++)
213. {
214. prev = ptr;
215. ptr = ptr->next;
216. }
217. prev->next = newnode;
218. newnode->next = ptr;
219. printf("\n----INSERTED----");
220. }
221. else
222. {
223. printf("Position is out of range");
224. }
225. }
226.  
227. /*
228.  * Sorted Linked List
229.  */
230. void sorted_ascend()
231. {
232. snode *nxt;
233. int t;
234.  
235. if (first == NULL)
236. {
237. ISEMPTY;
238. printf(":No elements to sort\n");
239. }
240. else
241. {
242. for (ptr = first;ptr != NULL;ptr = ptr->next)
243. {
244. for (nxt = ptr->next;nxt != NULL;nxt = nxt->next)
245. {
246. if (ptr->value > nxt->value)
247. {
248. t = ptr->value;
249. ptr->value = nxt->value;
250. nxt->value = t;
251. }
252. }
253. }
254. printf("\n---Sorted List---");
255. for (ptr = first;ptr != NULL;ptr = ptr->next)
256. {
257. printf("%d\t", ptr->value);
258. }
259. }
260. }
261.  
262. /*
263.  * Delete Node from specified position in a non-empty list
264.  */
265. void delete_pos()
266. {
267. int pos, cnt = 0, i;
268.  
269. if (first == NULL)
270. {
271. ISEMPTY;
272. printf(":No node to delete\n");
273. }
274. else
275. {
276. printf("\nEnter the position of value to be
deleted:");
277. scanf(" %d", &pos);
278. ptr = first;
279. if (pos == 1)
280. {
281. first = ptr->next;
282. printf("\nElement deleted");
283. }
284. else
285. {
286. while (ptr != NULL)
287. {
288. ptr = ptr->next;
289. cnt = cnt + 1;
290. }
291. if (pos > 0 && pos <= cnt)
292. {
293. ptr = first;
294. for (i = 1;i < pos;i++)
295. {
296. prev = ptr;
297. ptr = ptr->next;
298. }
299. prev->next = ptr->next;
300. }
301. else
302. {
303. printf("Position is out of range");
304. }
305. free(ptr);
306. printf("\nElement deleted");
307. }
308. }
309. }
310. /*
311.  * Updating Node value in a non-empty list
312.  */
313. void update_val()
314. {
315. int oldval, newval, flag = 0;
316.  
317. if (first == NULL)
318. {
319. ISEMPTY;
320. printf(":No nodes in the list to update\n");
321. }
322. else
323. {
324. printf("\nEnter the value to be updated:");
325. scanf("%d", &oldval);
326. printf("\nEnter the newvalue:");
327. scanf("%d", &newval);
328. for (ptr = first;ptr != NULL;ptr = ptr->next)
329. {
330. if (ptr->value == oldval)
331. {
332. ptr->value = newval;
333. flag = 1;
334. break;
335. }
336. }
337. if (flag == 1)
338. {
339. printf("\nUpdated Successfully");
340. }
341. else
342. {
343. printf("\nValue not found in List");
344. }
345. }
346. }
347.  
348. /*
349.  * searching an element in a non-empty list
350.  */
351. void search()
352. {
353. int flag = 0, key, pos = 0;
354.  
355. if (first == NULL)
356. {
357. ISEMPTY;
358. printf(":No nodes in the list\n");
359. }
360. else
361. {
362. printf("\nEnter the value to search");
363. scanf("%d", &key);
364.

Das könnte Ihnen auch gefallen