Advertisement
HmHimu

linked list final

Mar 1st, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. struct Node
  4. {
  5. int data;
  6. struct Node *next;
  7. } *temp, *node;
  8.  
  9. struct Head
  10. {
  11. int count;
  12. struct Node *first;
  13. } *head;
  14.  
  15. void Head_create()
  16. {
  17. head = (struct Head*) malloc(sizeof(struct Head));
  18. head->count = 0;
  19. head->first = NULL;
  20. }
  21.  
  22. void Add_value_start(int value)
  23. {
  24. node = (struct Node*) malloc(sizeof(struct Node));
  25. node->data = value;
  26. node->next = head->first;
  27. head->first = node;
  28. (head->count)++;
  29. }
  30.  
  31. void Add_value_end(int value)
  32. {
  33. if(head->count != 0)
  34. {
  35. temp = head->first;
  36. while( temp->next != NULL)
  37. {
  38. temp = temp->next;
  39. }
  40. node = (struct Node*) malloc(sizeof(struct Node));
  41. node->data = value;
  42. node->next = NULL;
  43. temp->next = node;
  44. (head->count)++;
  45. }
  46. else
  47. Add_value_start(value);
  48. }
  49.  
  50. void Add_value_anywhere()
  51. {
  52. int Search_values,v,flag=0;
  53. //struct Node *otherTemp;
  54. printf("\t\tEnter a value after you want to put a new Value: ");
  55. scanf("%d",&Search_values);
  56. node = (struct Node*) malloc (sizeof(struct Node));
  57.  
  58. temp = head->first;
  59. while(temp != NULL)
  60. {
  61. if(temp->data == Search_values)
  62. {
  63. printf("\t\tEnter a Value: ");
  64. scanf("%d",&v);
  65. node->da = v;
  66. /*otherTemp = temp->next;
  67. temp->next = node;
  68. node->next = otherTemp;
  69. flag++;*/
  70. node->next=temp->next;
  71. temp->next=node;
  72. flag=1;
  73. (head->count)++;
  74. break;
  75. }
  76. temp = temp->next;
  77. }
  78. if(flag == 0)
  79. printf("\t\t%d is not found the list.\n",Search_values);
  80. //free(otherTemp);
  81. }
  82.  
  83. void Delete_value_start()
  84. {
  85. if(head->count != 0)
  86. {
  87. temp = head->first;
  88. //head->first = head->first->next;
  89. head->first = temp->next;
  90. free(temp);
  91. (head->count)--;
  92. }
  93. else
  94. printf("\t\tLinkedlist is empty.\n");
  95. }
  96.  
  97. void Delete_value_end()
  98. {
  99. if(head->count != 0)
  100. {
  101. temp = head->first;
  102. while(temp->next != NULL)
  103. {
  104. node = temp;
  105. temp = temp->next;
  106. }
  107. node->next = NULL;
  108. //free(node->next);
  109. free(temp);
  110. (head->count)--;
  111. }
  112. else
  113. printf("\t\tLinkedlist is empty.\n");
  114. }
  115.  
  116. void Delete_value_anywhere()
  117. {
  118. if(head->count != 0)
  119. {
  120. int Search_values,val,flag=0;
  121. struct Node *otherTemp;
  122. printf("\t\tEnter a value after you want to delete: ");
  123. scanf("%d",&Search_values);
  124.  
  125. temp = head->first;
  126. while(temp != NULL)
  127. {
  128. if(temp->data==Search_values&&temp->next==NULL)
  129. {
  130. printf("\t\tData is in last position.\n\t\tUse option 5 to delete this data.\n");
  131. flag=1;
  132. }
  133. else if(temp->data == Search_values)
  134. {
  135. otherTemp = temp->next;
  136. node = otherTemp->next;
  137. temp->next = node;
  138. flag=1;
  139. free(otherTemp);
  140. (head->count)--;
  141. break;
  142. }
  143. temp = temp->next;
  144. }
  145. if(flag == 0)
  146. printf("\t\t%d is not found.\n",Search_values);
  147. }
  148. else
  149. printf("\t\tLinkedlist is empty.\n");
  150. }
  151.  
  152. void Search_values(int value)
  153. {
  154. int flag = 0, i;
  155. for(temp = head->first, i=1; temp != NULL; temp = temp->next,i++)
  156. {
  157. if(temp->data == value)
  158. {
  159. printf("\t\t%d Found in the list in no %d position.\n",temp->data,i);
  160. flag=1;
  161. break;
  162. }
  163.  
  164. }
  165. if(flag == 0)
  166. {
  167. printf("\t\t%d not Found in the list!\n",value);
  168. }
  169. }
  170.  
  171. void Show_values()
  172. {
  173. temp = head->first;
  174. printf("\t\tCurrent Values in the Linked List\n");
  175. while(temp != NULL)
  176. {
  177. printf("\t\t%d\n",temp->data);
  178. temp = temp->next;
  179.  
  180. }
  181. }
  182.  
  183. void Menu_function()
  184. {
  185. printf("\n\n\t\t------------------------------------------------\n");
  186. printf("\t\t: < Linked List > :\n");
  187. printf("\t\t------------------------------------------------\n");
  188. printf("\t\t: Add data :\n");
  189. printf("\t\t: 1. At First 2. At Last 3. At Anywhere :\n");
  190. printf("\t\t------------------------------------------------\n");
  191. printf("\t\t: Delete data :\n");
  192. printf("\t\t: 4. At First 5. At Last 6. At Anywhere :\n");
  193. printf("\t\t------------------------------------------------\n");
  194. printf("\t\t: 7. Search a Data :\n");
  195. printf("\t\t------------------------------------------------\n");
  196. printf("\t\t: 8. Show All Data :\n");
  197. printf("\t\t------------------------------------------------\n");
  198. printf("\t\t: 9. Exit program :\n");
  199. printf("\t\t------------------------------------------------\n");
  200. printf("\t\tEnter choice :");
  201. }
  202.  
  203. int Take_number()
  204. {
  205. int value;
  206. printf("\t\tEnter a value: ");
  207. scanf("%d", &value);
  208. return value;
  209. }
  210.  
  211. int main()
  212. {
  213. int choice;
  214. Head_create();
  215. for( ; ; )
  216. {
  217. Menu_function();
  218. scanf("%d", &choice);
  219. switch (choice)
  220. {
  221. case 1:
  222. Add_value_start(Take_number());
  223. break;
  224. case 2:
  225. Add_value_end(Take_number());
  226. break;
  227. case 3:
  228. Add_value_anywhere();
  229. break;
  230. case 4:
  231. Delete_value_start();
  232. break;
  233. case 5:
  234. Delete_value_end();
  235. break;
  236. case 6:
  237. Delete_value_anywhere();
  238. break;
  239. case 7:
  240. Search_values(Take_number());
  241. break;
  242. case 8:
  243. Show_values();
  244. break;
  245. case 9:
  246. free(temp);
  247. free(node);
  248. free(head);
  249. return EXIT_SUCCESS;
  250. default:
  251. printf("\t\tplease only press 1 to 9\n");
  252. break;
  253. }
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement