Advertisement
S_h_u_v_r_o

Linked List Full

Mar 3rd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct x
  5. {
  6. int a;
  7. struct x *ptr;
  8. }x;
  9. x *a,*head=NULL,*temp,*temp1;
  10.  
  11. void print()
  12. {
  13. temp=head;
  14. while(temp!=NULL)
  15. {
  16. printf("%d ",temp->a);
  17. temp=temp->ptr;
  18. }
  19. printf("\n");
  20. }
  21.  
  22. void search()
  23. {
  24. printf("Input Value U Want To Search ");
  25. int b=0,c=0,d;
  26. scanf("%d",&d);
  27. printf("\n");
  28.  
  29. temp=head;
  30. while(temp!=NULL)
  31. {
  32. c++;
  33. if(temp->a==d)
  34. {
  35. b=1;
  36. break;
  37. }
  38. temp=temp->ptr;
  39. }
  40. if(b=0)
  41. {
  42. printf("Not Found\n");
  43. }
  44. else
  45. {
  46. printf("%d Found At Index %d\n",temp->a,c);
  47. }
  48. }
  49.  
  50. void topinsert()
  51. {
  52. printf("Top Insert\n");
  53. temp=head;
  54. a=(x*)malloc(sizeof(x));
  55. scanf("%d",&a->a);
  56. a->ptr=temp;
  57. head=a;
  58. }
  59.  
  60. void insert()
  61. {
  62. printf("Insert Anywhere\n");
  63. printf("Position & Value\n");
  64. int n,b,c;
  65. a=(x*)malloc(sizeof(x));
  66. scanf("%d %d",&n,&a->a);
  67. a->ptr=NULL;
  68. temp=head;
  69. if(n==1)
  70. {
  71. a->ptr=temp;
  72. head=a;
  73. }
  74. else
  75. {
  76. for(b=1;b<n-1;b++)
  77. {
  78. temp=temp->ptr;
  79. }
  80. a->ptr=temp->ptr;
  81. temp->ptr=a;
  82. }
  83. }
  84.  
  85. void deletehead()
  86. {
  87. printf("Head Deleted\n");
  88. temp=head;
  89. head=head->ptr;
  90. free(temp);
  91. }
  92.  
  93. void deletee()
  94. {
  95. printf("Input Position U Want To Delete ");
  96. int b,d;
  97. scanf("%d",&d);
  98. printf("\n");
  99.  
  100. temp=head;
  101. if(d==1)
  102. {
  103. head=head->ptr;
  104. free(temp);
  105. }
  106. else
  107. {
  108. for(b=1;b<d;b++)
  109. {
  110. temp1=temp;
  111. temp=temp->ptr;
  112. }
  113. temp1->ptr=temp->ptr;
  114. free(temp);
  115. }
  116. }
  117.  
  118. void deleteee()
  119. {
  120. printf("Input Value U want To Delete ");
  121. int b,d;
  122. scanf("%d",&d);
  123. printf("\n");
  124.  
  125.  
  126. temp=head;
  127. if(head->a==d)
  128. {
  129. head=head->ptr;
  130. free(temp);
  131. }
  132. else
  133. {
  134. while(temp->a!=d)
  135. {
  136. temp1=temp;
  137. temp=temp->ptr;
  138. }
  139. temp1->ptr=temp->ptr;
  140. free(temp);
  141. }
  142.  
  143. }
  144.  
  145. int main()
  146. {
  147. printf("Input The Size Of Array ");
  148. int n,b,c,d,e=0;
  149. scanf("%d",&n);
  150.  
  151. printf("Input Value\n");
  152. for(b=0;b<n;b++)
  153. {
  154. a=(x*)malloc(sizeof(x));
  155. scanf("%d",&a->a);
  156. a->ptr=NULL;
  157.  
  158. if(head==NULL)
  159. {
  160. head=a;
  161. }
  162. else
  163. {
  164. temp=head;
  165. while(temp->ptr!=NULL)
  166. {
  167. temp=temp->ptr;
  168. }
  169. temp->ptr=a;
  170. }
  171. }
  172. printf("Stored\n");
  173.  
  174. print();
  175. search();
  176. topinsert();
  177. print();
  178. insert();
  179. print();
  180. deletehead();
  181. print();
  182. deletee();
  183. print();
  184. deleteee();
  185. print();
  186.  
  187. printf("\nR Kichu??\n");
  188.  
  189.  
  190. return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement