bipo143

dada lab 25 tarik

Mar 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 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.  
  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 2 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. k:printf("\n enter position between %d to %d\n",1,n);
  83. scanf("%d",&pos);
  84. if(pos>=1&&pos<=n)
  85. Deletion(list,pos);
  86. else
  87. {
  88. printf("\n wrong position\n");
  89. goto k;
  90. }
  91. }
  92. else
  93. printf("\n no data to delete\n\n");
  94. break;
  95.  
  96.  
  97. default: printf("\n\n Wrong position\n");
  98.  
  99. }
  100.  
  101. }
  102. }
  103.  
  104.  
  105. void Insertion_Last(struct node *head, int value)
  106. {
  107. struct node *temp;
  108. while(head->p!=NULL)
  109. head=head->p;
  110.  
  111. temp=(struct node *) malloc(1*sizeof(struct node));
  112. temp->a=value;
  113. temp->p=NULL;
  114. head->p=temp;
  115. }
  116. void Display(struct node *head)
  117. {
  118. while(head->p!=NULL)
  119. {
  120. printf("\t->%d->",head->p->a);
  121. head=head->p;
  122. }
  123. }
  124. int count(struct node *head)
  125. {
  126. int t=0;
  127. while(head->p!=NULL)
  128. {
  129. t=t+1;
  130. head=head->p;
  131. }
  132. return t;
  133. }
  134. void insertion_specific_position(struct node *head,int pp,int vv)
  135. {
  136. int t=0;
  137. struct node *temp;
  138. while(head->p!=NULL)
  139. {
  140. if(t==pp-1)
  141. break;
  142. head=head->p;
  143. t=t+1;
  144. }
  145. temp=(struct node*)malloc(1*sizeof(struct node));
  146.  
  147. temp->a=vv;
  148. temp->p=head->p;
  149. head->p=temp;
  150. }
  151. void Deletion(struct node*head,int pp)
  152. {
  153.  
  154.  
  155. struct node *temp;
  156. int i=0;
  157. while(head->p!=NULL)
  158. {
  159. if(pp-1==i)
  160. break;
  161. else
  162. head=head->p;
  163. i=i+1;
  164. }
  165. temp=head->p;
  166. head->p=temp->p;
  167. free(temp);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment