bipo143

link list full

Apr 15th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. #include<stdlib.h>
  2. struct node
  3. {
  4. int a;
  5. struct node *p;
  6. };
  7. int count(struct node*head);
  8. void insertion_specific_position(struct node*head,int pp,int vv);
  9. void Insertion_Last(struct node *head, int value);
  10. void Display(struct node *head);
  11. void deletion(struct node * head,int pp);
  12. void linear_search(struct node*head,int vv);
  13.  
  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("\nEnter 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.  
  53. case 5:printf("\n Choice=Total node count \n");
  54. n=count(list);
  55. printf("\n Total node =%d \n",n);
  56. break;
  57.  
  58. case 2:printf("\n choice=insert at specific position \n");
  59. n=count(list);
  60. if(n!=0)
  61. {
  62. M:printf("\n Enter position between %d to %d\n",1,n+1);
  63. scanf("%d",&pos);
  64. if(pos>=1 && pos<=n+1)
  65. {
  66. printf("\n Enter the value \n");
  67. scanf("%d",&v);
  68. insertion_specific_position(list,pos,v);
  69.  
  70. }
  71. else
  72. {
  73. printf("\n Invalied position \n");
  74. goto M;
  75. }
  76. }
  77. else
  78. printf("\n The list is empty\n");
  79. break;
  80.  
  81. case 4:printf("\n choice_deletion from specific position\n");
  82. n=count(list);
  83. if(n !=0)
  84. {
  85. k:printf("\nEnter position between %d to %d \n",1,n);
  86. scanf("%d",& pos);
  87. if (pos>=1 && pos <=n)
  88. deletion(list,pos);
  89. else
  90. {
  91. printf("\n Wrong position");
  92. goto k;
  93. }
  94. }
  95. else
  96. printf("\n No data to delete \n\n");
  97. break;
  98.  
  99. case 6:printf("\n Choice= Linear Search\n");
  100. n=count(list);
  101. if(n!=0)
  102. {
  103. printf("\n Enter value for searching \n");
  104. scanf("%d",&v);
  105. linear_search(list,v);
  106. }
  107. else
  108. printf("\n The list is empty \n");
  109. break;
  110. default: printf("\n\n Wrong position\n");
  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.  
  138. int count(struct node*head)
  139. {
  140. int t=0;
  141. while(head->p!=NULL)
  142. {
  143. t=t+1;
  144. head=head->p;
  145. }
  146. return t;
  147. }
  148.  
  149. void insertion_specific_position(struct node*head,int pp,int vv)
  150. {
  151. int t=0;
  152. struct node*temp;
  153. while(head->p!=NULL)
  154. {
  155. if(t==pp-1)
  156. break;
  157. head=head->p;
  158. t=t+1;
  159. }
  160. temp=(struct node*)malloc(1*sizeof(struct node));
  161. temp ->a=vv;
  162. temp ->p=head->p;
  163. head ->p=temp;
  164.  
  165. }
  166. void deletion(struct node * head,int pp)
  167. {
  168. struct node * temp;
  169. int i=0;
  170. while(head->p!=NULL)
  171. {
  172. if(pp-1==i)
  173. break;
  174. head=head->p;
  175. i=i+1;
  176. }
  177. temp=head->p;
  178. head->p=temp->p;
  179. free(temp);
  180. }
  181. void linear_search(struct node*head,int vv)
  182. {
  183. int c=0,i=0;
  184. while(head->p!=NULL)
  185. {
  186. if(vv==head->p->a)
  187. {
  188. printf("\n Found at position=%d",i+1);
  189. c=1;
  190. head=head->p;
  191. i=i+1;
  192.  
  193. }
  194. else
  195. {
  196. head=head->p;
  197. i=i+1;
  198. }
  199. }
  200. if(c==0)
  201. printf("\n\n Not Found\n\n");
  202. }
Advertisement
Add Comment
Please, Sign In to add comment