Advertisement
S_h_u_v_r_o

Linked List

Feb 23rd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct x
  5. {
  6. int data;
  7. struct x *next;
  8. }x;
  9.  
  10. x *head=NULL,*temp;
  11.  
  12. void search()
  13. {
  14. int n,a=0,b=0;
  15. temp=head;
  16. printf("Input the number u want to search\n");
  17. scanf("%d",&n);
  18. while(1)
  19. {
  20. b++;
  21. if(temp->data==n)
  22. {
  23. printf("%d Found at %d\n",temp->data,b);
  24. a=1;
  25. }
  26. if(temp->next==NULL)
  27. {
  28. break;
  29. }
  30.  
  31. temp=temp->next;
  32.  
  33. }
  34. if(a==0)
  35. {
  36. printf("Not Found");
  37. }
  38. }
  39.  
  40. void dbv()
  41. {
  42. int i,count=1;
  43. x *previous;
  44.  
  45. printf("Input the value to delete:\n");
  46. scanf("%d",&i);
  47.  
  48. temp=head;
  49.  
  50. if(i==head->data)
  51. {
  52. head=head->next;
  53. free(temp);
  54. }
  55. else
  56. {
  57. while(temp!=NULL && temp->data!=i)
  58. {
  59. previous=temp;
  60. temp=temp->next;
  61. }
  62. if(temp==NULL)
  63. {
  64. printf("%d does not exist\n",i);
  65. }
  66. else
  67. {
  68. previous->next=temp->next;
  69. free(temp);
  70.  
  71. }
  72. }
  73. temp=head;
  74. while(temp!=NULL)
  75. {
  76. printf("%d ",temp->data);
  77. temp=temp->next;
  78. }
  79. }
  80.  
  81. void dbp()
  82. {
  83. int i,count=1;
  84. x *previous;
  85.  
  86. printf("Input the position to delete:\n");
  87. scanf("%d",&i);
  88.  
  89. if(i==1)
  90. {
  91. temp=head;
  92. head=head->next;
  93. free(temp);
  94. }
  95. else
  96. {
  97. temp=head;
  98. while(count<i)
  99. {
  100. previous=temp;
  101. temp=temp->next;
  102. count++;
  103. }
  104. previous->next=temp->next;
  105. free(temp);
  106. }
  107.  
  108. temp=head;
  109. while(temp!=NULL)
  110. {
  111. printf("%d ",temp->data);
  112. temp=temp->next;
  113. }
  114.  
  115.  
  116. }
  117.  
  118. int main()
  119. {
  120. int n,b,c=-1;
  121. x *nn;
  122. scanf("%d",&n);
  123. for(b=0;b<n;b++)
  124. {
  125. nn=(x*)malloc(sizeof(x));
  126.  
  127. scanf("%d",&nn->data);
  128. nn->next=NULL;
  129.  
  130. if(head==NULL)
  131. {
  132. head=nn;
  133. }
  134. else
  135. {
  136. temp=head;
  137. while(temp->next!=NULL)
  138. {
  139. temp=temp->next;
  140. }
  141. temp->next=nn;
  142. }
  143. }
  144. temp=head;
  145. while(temp!=NULL)
  146. {
  147. c++;
  148. printf("%d\n",temp->data);
  149. temp=temp->next;
  150. }
  151.  
  152. search();
  153. dbv();
  154. dbp();
  155.  
  156. while(temp!=NULL)
  157. {
  158. printf("%d\n",temp->data);
  159. temp=temp->next;
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement