Advertisement
RAJIBRAJU

linklist

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