Advertisement
AparnaDebnath

singly data

Feb 15th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct data
  5. {
  6. int a;
  7. struct data *next;
  8. }data;
  9.  
  10. data *head=NULL;
  11.  
  12. void insert_at_first(int x)
  13. {
  14. data *newnode = (*data)malloc(sizeof(data));
  15. newnode ->a = x;
  16. newnode ->next = NULL;
  17. if(head==NULL)
  18. {
  19. head=newnode;
  20. return;
  21. }
  22. newnode -> next = head;
  23. newnode = head;
  24. }
  25.  
  26. void insert_at_end(int x)
  27. {
  28. data *node = (*data)malloc(sizeof(data));
  29. node -> a = x;
  30. node -> next = NULL;
  31. data *temp = head;
  32. if(head==NULL)
  33. {
  34. head = node;
  35. return;
  36. }
  37. while(temp -> next!=NULL)
  38. {
  39. temp = temp->next;
  40. }
  41. temp -> next = node;
  42. }
  43.  
  44. void insert_at_ntn(int n, int x)
  45. {
  46. data *new = (*data)malloc(sizeof(data));
  47. new -> a= x;
  48. new -> next = NULL;
  49. data *temp = head;
  50. if(head==NULL || n==1)
  51. {
  52. new -> next =head;
  53. head = new;
  54. return;
  55. }
  56. n = n-2;
  57. while(n-- && temp -> next != NULL)
  58. {
  59. temp = temp -> next;
  60. }
  61. new -> next = temp ->next;
  62. temp -> next = new;
  63. }
  64.  
  65. void delete_by_pos(int n)
  66. {
  67. data *temp = head;
  68. if(head==NULL)
  69. {
  70. printf("not found\n");
  71. return;
  72. }
  73. if(n==1)
  74. {
  75. head = temp -> next;
  76. free (temp);
  77. return;
  78. }
  79. n=n-2;
  80. while(n-- && temp ->next!=NULL)
  81. {
  82. temp = temp -> next;
  83. }
  84. data *del = temp -> next;
  85. if(del==NULL)
  86. {
  87. return;
  88. }
  89. temp -> next = del -> next;
  90. free (del);
  91. }
  92.  
  93. void delete_by_value(int n)
  94. {
  95. data *temp = head;
  96. if(temp ->a==n)
  97. {
  98. head = temp -> next;
  99. free (temp);
  100. }
  101. while(temp -> next != NULL)
  102. {
  103. if(temp ->next ->a==n)
  104. {
  105. data *del = temp ->next;
  106. temp ->next = temp ->next ->next;
  107. free (del);
  108.  
  109. }
  110. temp = temp ->next;
  111. }
  112. }
  113.  
  114. void search(int x)
  115. {
  116. data *temp = head;
  117. int c=1;
  118. while(temp!=NULL)
  119. {
  120. if(temp ->a ==x)
  121. {
  122. printf("%d Found!\n",x);
  123. return;
  124. }
  125. else
  126. {
  127. c++;
  128. temp = temp -> next;
  129. }
  130. }
  131. if(c>1)
  132. {
  133. printf("%d not found\n",x);
  134. return;
  135. }
  136. }
  137.  
  138. int count()
  139. {
  140. int count = 0;
  141. data *temp = head;
  142. while(temp!=NULL)
  143. {
  144. count ++;
  145. temp = temp -> next;
  146. }
  147. printf("total element no: %d\n",count);
  148. return count;
  149. }
  150.  
  151. int sum()
  152. {
  153. int add = 0;
  154. data *temp = head;
  155. while(temp!=NULL)
  156. {
  157.  
  158. add=add+temp->a;
  159. temp=temp->next;
  160. }
  161. printf("Sum of our total element: %5d\n",add);
  162. return add;
  163. }
  164.  
  165. void avg(float a, float b)
  166. {
  167. float avg;
  168. avg = a/b;
  169. printf("Avg: %.2f\n",avg);
  170. }
  171. void print()
  172. {
  173. data *temp = head;
  174. while(temp!=NULL)
  175. {
  176. printf("%5d",temp->a);
  177. temp=temp->next;
  178. }
  179. printf("\n");
  180. }
  181.  
  182. int main()
  183.  
  184. {
  185. int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;
  186. float c,s;
  187. printf("insert any data at first : ");
  188. scanf("%d", &a1);
  189. insert_at_first(a1);
  190. print();
  191.  
  192. printf("Insert any data at first: ");
  193. scanf("%d",&a2);
  194. insert_at_first(a2);
  195. print();
  196.  
  197. printf("Insert any data at first: ");
  198.  
  199. printf("Insert any data at end: ");
  200. scanf("%d",&a4);
  201. insert_at_first(a4);
  202. print();
  203.  
  204. printf("Insert any data at end: ");
  205. scanf("%d",&a5);
  206. insert_at_first(a5);
  207. print();
  208.  
  209. printf("Insert any data at nth_position: ");
  210. scanf("%d",&a6);
  211. insert_at_first(a6);
  212. print();
  213.  
  214. printf("Insert any data at nth_position: ");
  215. scanf("%d",&a7);
  216. insert_at_first(a7);
  217. print();
  218.  
  219. printf("Insert any data at nth_position: ");
  220. scanf("%d",&a8);
  221. insert_at_first(a8);
  222. print();
  223.  
  224. printf("Enter any value which you want to delete: ");
  225. scanf("%d",&a9);
  226. insert_at_first(a9);
  227. print();
  228.  
  229. printf("Which value you want to search!: ");
  230. scanf("%d",&a10);
  231. search(a10);
  232. print();
  233.  
  234. c=count();
  235. s=sum();
  236. avg(s,c);
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement