bipo143

datalab full 25

Mar 25th, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. #include<stdlib.h>
  2. struct node
  3. {
  4. int a;
  5. struct node *p;
  6. };
  7.  
  8. void Insertion_Last(struct node *head, int value);
  9. void Display(struct node *head);
  10. int count(struct node *head);
  11. void insertion_specific_position(struct node *head,int pp,int vv);
  12. void Deletion (struct node *head, int pp);
  13. void linear_search(struct node *head,int vv);
  14.  
  15. int main()
  16. {
  17. int n,i,c,v,pos,q;
  18. struct node *list;
  19. list=(struct node *) malloc(1*sizeof(struct node));
  20. list->a=0;
  21. list->p=NULL;
  22. while(1)
  23. {
  24. printf("\n----------Menu-----------\n");
  25. printf("\nPress 0 for quit\n");
  26. printf("\nPress 1 for insertion at last position\n");
  27. printf("\nPress 2 for insertion at specific position\n");
  28. printf("\nPress 3 for display\n");
  29. printf("\nPress 4 for deletion at specific position\n");
  30. printf("\nPress 5 for counting total node\n");
  31. printf("\nPress 6 for linear search\n");
  32.  
  33. printf("\n Enter your choice:\n");
  34. scanf("%d",&c);
  35. switch(c)
  36. {
  37. case 0: exit(0);
  38. break;
  39.  
  40. case 1: printf("\nchoice = Insertion at last position\n");
  41. printf("\n Enter new value \n");
  42. scanf("%d",&v);
  43. Insertion_Last(list,v);
  44. break;
  45.  
  46. case 3: printf("\nchoice = Display\n");
  47. if(list->p!=NULL)
  48. Display(list);
  49. else
  50. printf("\nThe list is empty\n");
  51. break;
  52. case 5: printf("\n choice=Total node count\n");
  53. n=count(list);
  54. printf("\n Total node =%d\n",n);
  55. break;
  56. case 2: printf("\n choice=Insertion at specific position\n");
  57. n=count(list);
  58. if (n!=0)
  59. {
  60. M:printf("\n Enter position between %d to %d\n",1,n+1);
  61. scanf("%d",&pos);
  62. if(pos>=1 && pos<=n+1)
  63. {
  64. printf("\n Enter Value \n");
  65. scanf("%d",&v);
  66. insertion_specific_position(list,pos,v);
  67. }
  68. else
  69. {
  70. printf("\n Invalid position \n");
  71. goto M;
  72. }
  73. }
  74. else
  75. printf("\n The List is Empty\n");
  76. break;
  77.  
  78. case 4: printf("\n choice=Deletion from specific position\n");
  79. n=count(list);
  80. if(n!=0)
  81. {
  82.  
  83. K:printf("\n Enter position between %d to %d \n", 1,n);
  84. scanf("%d",&pos);
  85. if(pos>=1 && pos<=n)
  86. Deletion(list,pos);
  87. else{
  88. printf("\n Wrong position");
  89. goto K;
  90. }
  91. }
  92. else
  93. printf("\n No data to delete \n\n");
  94. break;
  95.  
  96. case 6: printf ("\n choice =liner search\n");
  97. n=count(list);
  98. if(n!=0)
  99. {
  100. printf("\n Enter value for searching \n");
  101. scanf("%d",&v);
  102. linear_search(list,v);
  103. }
  104. else
  105. printf("\n The list is Empty\n");
  106. break;
  107.  
  108.  
  109. default: printf("\n\n Wrong position\n");
  110.  
  111. }
  112.  
  113. }
  114. }
  115.  
  116.  
  117.  
  118. void Insertion_Last(struct node *head, int value)
  119. {
  120. struct node *temp;
  121. while(head->p!=NULL)
  122. head=head->p;
  123.  
  124. temp=(struct node *) malloc(1*sizeof(struct node));
  125. temp->a=value;
  126. temp->p=NULL;
  127. head->p=temp;
  128. }
  129. void Display(struct node *head)
  130. {
  131. while(head->p!=NULL)
  132. {
  133. printf("\t->%d->",head->p->a);
  134. head=head->p;
  135. }
  136. }
  137. int count(struct node *head)
  138. {
  139. int t=0;
  140. while(head->p!=NULL)
  141. {
  142. t=t+1;
  143. head=head->p;
  144. }
  145. return t;
  146. }
  147. void insertion_specific_position(struct node *head,int pp,int vv)
  148. {
  149. int t=0;
  150. struct node *temp;
  151. while(head->p!=NULL)
  152. {
  153. if(t==pp-1)
  154. break;
  155. head=head->p;
  156. t=t+1;
  157. }
  158. temp=(struct node*)malloc(1*sizeof(struct node));
  159.  
  160. temp->a=vv;
  161. temp->p=head->p;
  162. head->p=temp;
  163. }
  164.  
  165. void Deletion (struct node *head, int pp)
  166. {
  167. struct node *temp;
  168. int i=0;
  169. while(head->p!=NULL)
  170. {
  171. if(pp-1==i)
  172. break;
  173.  
  174. else{
  175. head=head->p;
  176. i=i+1;
  177. }
  178. }
  179. temp=head->p;
  180. head->p=temp->p;
  181. free(temp);
  182. }
  183. void linear_search(struct node *head,int vv)
  184. {
  185. int c=0, i=0;
  186. while(head->p!=NULL)
  187. {
  188. if(vv==head->p->a)
  189. {
  190. printf("\n Found at position =%d",i+1);
  191. c=1;
  192. head=head->p;
  193. i=i+1;
  194. }
  195. else
  196. {
  197. head=head->p;
  198. i=i+1;
  199. }
  200. }
  201. if(c==0)
  202. printf("\n\n Not Fount \n\n");
  203. }
Add Comment
Please, Sign In to add comment