Skip to main content

Menu Driven Singly Linked List

Menu Driven Singly Linked List



 CODE

👇

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node 
  4. {
  5. int info;
  6. struct node *next;
  7. }NODE;

  8. NODE* createlist(NODE *list); 
  9. void Display (NODE *list);
  10. void search (NODE *list);
  11. NODE* insertbeg(NODE *list);
  12. NODE* insertbetween(NODE *list);
  13. NODE* insertlast(NODE *list);
  14. NODE* Delpos(NODE *list);
  15. NODE* Delvalue(NODE *list);

  16. //MAIN FUNCTION
  17. void main ()
  18. {
  19.     printf(">>Singly Linked List<<");
  20. NODE*list=NULL, *temp;
  21.     int ch,n,pos;
  22. do
  23. {
  24. printf("\n1. Create:");
  25. printf("\n2. Display");
  26. printf("\n3.Insert AtFirst");
  27. printf("\n4.Insert AtMiddle");
  28. printf("\n5.Insert AtLast");
  29. printf("\n6. Delete by position");
  30. printf("\n7. Delete by value");
  31. printf("\n8. Search");
  32. printf("\n9.Reverse");
  33. printf("\n10.Count");
  34. printf("\n11. Exit\n");
  35. printf("\n Enter your choice:");
  36. scanf("%d", &ch);
  37. switch(ch)
  38. {
  39. case 1:
  40. list=createlist (list);
  41. break;
  42. case 2:
  43. Display (list);
  44. break;
  45. case 3:
  46. list=insertbeg (list);
  47. break;
  48. case 4:
  49. list=insertbetween (list);
  50. break;
  51. case 5:
  52. list=insertlast (list);
  53. break;
  54. case 6:
  55. break;
  56. case 7:
  57. break;
  58. case 8:search (list);
  59. break;
  60. case 9:
  61. break;
  62. case 10:
  63. break;
  64. case 11:printf("_END_");
  65. break;
  66. }while(ch != 11);
  67. }


  68. //CREATE FUNCTION
  69. NODE* createlist (NODE *list)
  70. {
  71.  int n,count;
  72. NODE *temp, *newnode;
  73.  printf ("\nHow many nodes you want to enter ? \n");
  74.  scanf("%d" ,&n);
  75.  for(count=1 ; count<=n; count++)
  76.   {
  77.   newnode=(NODE*)malloc(sizeof(NODE));
  78.  newnode->next=NULL;
  79.   printf( "Enter the node data:-  " );
  80. scanf ("%d",&newnode->info);
  81.  if (list==NULL)
  82.   {
  83.     list=temp=newnode;
  84.    }
  85.   else 
  86.  { 
  87.    temp->next=newnode;
  88.    temp=newnode;
  89.   }
  90. }
  91.   return list;
  92. }
  93.   void Display (NODE *list)
  94.  {
  95.   NODE *temp=list;
  96.    while(temp!=NULL)
  97.   {
  98.      printf ("%d",temp->info);
  99.      printf("-->");
  100.     temp=temp->next ;
  101.  }
  102.    printf("NULL \n");
  103. }


  104. //Insert AT BEGINING
  105. NODE * insertbeg(NODE *list)
  106. {
  107. int n;
  108. printf("Enter the node you want to insert at first position:");
  109. scanf("%d", &n);
  110. NODE *newnode;
  111. newnode=(NODE*)malloc(sizeof(NODE));
  112. newnode->info=n;
  113. newnode->next=list;
  114. list=newnode;
  115. return list;
  116. }

  117. //INSERT IN BETWEEN
  118. NODE * insertbetween(NODE *list)
  119. {
  120. NODE *newnode, *temp=list;
  121. int n,i,pos;
  122. printf("Enter the  node data and position you want to insert between the node: \t");
  123. scanf("\n%d", &n);
  124. scanf("\n%d", &pos);
  125. newnode=(NODE*)malloc(sizeof(NODE));
  126. newnode->next=NULL;
  127. newnode->info=n;

  128. for(i=1; i<pos-1&&temp->next!=NULL; i++)
  129. temp=temp->next;
  130. newnode->next=temp->next;
  131. temp->next=newnode;

  132. return list;
  133. }


  134. //INSERT AT LAST
  135. NODE * insertlast(NODE *list)
  136. {
  137. int n;
  138. printf("Enter the  node data you want to insert at last position: \t");
  139. scanf("%d", &n);
  140. NODE *newnode, *temp;
  141. newnode=(NODE*)malloc(sizeof(NODE));
  142. newnode->info=n;
  143. newnode->next=NULL;

  144. for(temp=list; temp->next!=NULL; temp=temp->next);

  145. temp->next=newnode;
  146. return list;
  147. }


  148. //SEARCH
  149. void search (NODE *list)
  150. {
  151. int num, flag=0;
  152. NODE *temp;
  153. printf("Enter the element to be search:");
  154. scanf("%d", &num);
  155. for(temp=list; temp!=NULL; temp=temp->next)
  156. {
  157. if(temp->info==num)
  158. {
  159. printf(">>%d is Found<<\n",num);
  160. flag=1;
  161. exit;
  162. }
  163. }
  164. if(flag==0)
  165. printf(">>%d is not found<<\n", num);
  166. }


  167. //DELETE BY POSITION
  168. NODE * Delpos(NODE *list)
  169. {
  170. NODE *temp=list, *temp1;
  171. int i, pos;
  172. printf("Enter node position to delete the node: \t");
  173. scanf("%d", &pos);
  174. if(pos==1)
  175. {
  176. list=temp->next;
  177. free (temp);
  178. return list;
  179. }
  180. for(i=1, temp=list; i<=pos-1 && temp!=NULL; i++)
  181. temp=temp->next;
  182. if(temp==NULL)
  183. {
  184. printf(">>position is out of range<<");
  185. return list;
  186. }
  187. temp1=temp->next;
  188. temp->next=temp1->next;
  189. free (temp1);
  190. return list;
  191. }

  192. //DELETE BY VALUE
  193. NODE* Delvalue(NODE *list)
  194. {
  195. NODE *temp=list, *temp1;
  196. int num;
  197. printf("Enter node data to delete that node \t");
  198. scanf("%d", &num);
  199. if(list->info==num)
  200. {
  201. list=list->next;
  202. free (temp);
  203. return list;
  204. exit;
  205. }
  206. for(temp=list; temp->next!=NULL; temp=temp->next)
  207. if(temp->next->info==num)
  208. {
  209. temp1=temp->next;
  210. temp->next=temp1->next;
  211. free (temp1);
  212. return list;
  213. exit;
  214. }
  215. printf(">>Element is not found<<\n");
  216. return list;
  217. }


Comments