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

Operations on singly linked list

---------------------------------

1.Insert node at first
2.Insert node at last
3.Insert node before element
4.Insert node after element
5.Delete Node at first
6.Delete Node at last
7.Delete Node before any Position
8.Delete Node after any Position
9.Display List from Beginning to end
10.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice
...Displaying List From Beginning to End...
:No nodes in the list to display

YOU WANT TO CONTINUE (Y/N)