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. } 
  67. }while(ch != 11);
  68. }


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


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

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

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

  133. return list;
  134. }


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

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

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


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


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

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


Comments

Another posts