Advertisement
Abir_Ahsan

Untitled

Jul 16th, 2018
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int count=0;
  5.  
  6. struct node
  7. {
  8. int value;
  9. struct node *next;
  10. };
  11.  
  12. struct node *head;
  13.  
  14. void menu()
  15. {
  16. printf("\nEnter your desired option\n");
  17. printf("==========================\n");
  18. printf("1. Insert first\n");
  19. printf("2. Insert last\n");
  20. printf("3. Insert middle\n");
  21. printf("4. Display\n");
  22. printf("5. Exit\n");
  23. printf("6. Count Elements\n");
  24. printf("7. SOrt Elements \n");
  25. printf("Enter your choice: ");
  26. }
  27.  
  28. void insertFirst(int num)
  29. {
  30. count++;
  31.  
  32. struct node *newItem=(struct node *)malloc(sizeof (struct node));
  33. newItem->value=num;
  34. newItem->next=NULL;
  35.  
  36. if (head==NULL)
  37. head=newItem;
  38. else
  39. {
  40. newItem->next=head;
  41. head=newItem;
  42. }
  43. }
  44.  
  45.  
  46. void insertLast(int num)
  47. {
  48. count++;
  49.  
  50. struct node *new_node=(struct node *)malloc(sizeof (struct node));
  51. new_node->value=num;
  52. new_node->next=NULL;
  53.  
  54. struct node *temp=head;
  55.  
  56. if (head==NULL)
  57. head=new_node;
  58. else
  59. {
  60. while (temp->next!=NULL)
  61. temp=temp->next;
  62. temp->next=new_node;
  63. }
  64. }
  65.  
  66. void insertMiddle(int num,int loc)
  67. {
  68. count++;
  69.  
  70. struct node *new_node=(struct node *)malloc(sizeof (struct node));
  71.  
  72. struct node* temp=new_node;
  73.  
  74. temp->value=num;
  75. temp->next=NULL;
  76.  
  77. int count=0;
  78. struct node* pt=head;
  79.  
  80. while(pt->next!=NULL)
  81. {
  82. if(count==loc)
  83. {
  84. temp->next=pt->next;
  85. pt->next=temp;
  86. }
  87. pt=pt->next;
  88. count++;
  89. }
  90.  
  91.  
  92. }
  93.  
  94. void toSort()
  95. {
  96. int sort=1, temp;
  97. struct node *new_head, *tail=NULL;
  98.  
  99. while(sort)
  100. {
  101. new_head=head;
  102. sort=0;
  103.  
  104. while(new_head->next!=tail)
  105. {
  106. if (new_head->value>new_head->next->value)
  107. {
  108. temp=new_head->value;
  109. new_head->value=new_head->next->value;
  110. new_head->next->value=temp;
  111. sort=1;
  112. }
  113. new_head=new_head->next;
  114. }
  115. tail=new_head;
  116. }
  117. }
  118. void toCount(void)
  119. {
  120. printf("Total Elements %d ",count);
  121.  
  122. }
  123. void display()
  124. {
  125. if (head==NULL)
  126. {
  127. printf("List Empty\n");
  128. return;
  129. }
  130.  
  131. struct node *cur=head;
  132.  
  133. while (cur!=NULL)
  134. {
  135.  
  136. printf("%i\t", cur->value);
  137. cur=cur->next;
  138.  
  139. }
  140. }
  141.  
  142. int main( )
  143. {
  144. int i, n,x,y,z;
  145. head=NULL;
  146.  
  147. while (1)
  148. {
  149. menu();
  150. scanf("%i", &i);
  151.  
  152. switch(i)
  153. {
  154. case 1:
  155. scanf("%i", &n);
  156. insertFirst(n);
  157. break;
  158. case 2:
  159. scanf("%d", &x);
  160. printf("\n");
  161. insertLast(x);
  162. break;
  163. case 3:
  164. scanf("%d%d", &y,&z);
  165.  
  166. printf("\n");
  167. insertMiddle(y,z);
  168. break;
  169. case 4:
  170. display();
  171. break;
  172. case 5:
  173.  
  174. return 0;
  175. case 6:
  176. printf("\n");
  177. toCount();
  178.  
  179. break;
  180.  
  181. case 7:
  182. printf("\n");
  183. toSort();
  184. break;
  185.  
  186. }
  187.  
  188. }
  189.  
  190.  
  191. return 0;
  192. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement