Advertisement
muftY

link list HW

Feb 7th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 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. data *head=NULL;
  10. void fst(int x)
  11. {
  12. data *new =(data*)malloc(sizeof(data));
  13. new->a=x;
  14. new->next=NULL;
  15. if(head==NULL)
  16. {
  17. head=new;
  18. return;
  19. }
  20. new->next=head;
  21. head=new;
  22. }
  23. void end(int x)
  24. {
  25. data *new =(data*)malloc(sizeof(data));
  26. new->a=x;
  27. new->next=NULL;
  28. if(head==NULL)
  29. {
  30. head=new;
  31. return;
  32. }
  33. data *temp=head;
  34.  
  35. while(temp->next!=NULL)
  36. {
  37. temp=temp->next;
  38. }
  39. temp->next=new;
  40. }
  41. void nth(int n,int x)
  42. {
  43. data *new =(data*)malloc(sizeof(data));
  44. new->a=x;
  45. new->next=NULL;
  46. if(n==1)
  47. {
  48. new->next=head;
  49. head=new;
  50. return;
  51. }
  52. data *temp=head;
  53. n=n-2;
  54. while(n-- && temp->next!=NULL)
  55. {
  56. temp=temp->next;
  57. }
  58. new->next=temp->next;
  59. temp->next=new;
  60. }
  61. void dbp(int n)
  62. {
  63. int count=0;
  64. data *del=NULL;
  65. data *temp=head;
  66. if(n==1)
  67. {
  68. del=head;
  69. head=head->next;
  70. free(del);
  71. }
  72. n=n-2;
  73. while(n-- && temp->next!=NULL)
  74. {
  75. temp=temp->next;
  76. count++;
  77. }
  78. if(temp->next==NULL)
  79. {
  80.  
  81. return;
  82. }
  83.  
  84. del=temp->next;
  85. temp->next=del->next;
  86. free(del);
  87.  
  88. }
  89.  
  90.  
  91. void dbv(int x)
  92. {
  93.  
  94. data *del=NULL;
  95. data *temp=head;
  96. if(head->a==x)
  97. {
  98. del=head;
  99. head=del->next;
  100. free(del);
  101. return;
  102. }
  103.  
  104. else if(head->a!=x && temp->next==NULL)
  105. {
  106. while(temp->next->a!=x)
  107. {
  108. temp=temp->next;
  109. }
  110. del=temp->next;
  111. temp->next=del->next;
  112. free(del);
  113.  
  114. }
  115. else
  116. return;
  117.  
  118. }
  119.  
  120. int sum()
  121. {
  122. int s=0;
  123. data *temp=head;
  124. while(temp!=NULL)
  125. {
  126. s+=temp->a;
  127. temp=temp->next;
  128.  
  129. }
  130. return s;
  131. }
  132. double avrg()
  133. {
  134. int s=0;
  135. int count=0;
  136. data *temp=head;
  137. while(temp!=NULL)
  138. {
  139. s+=temp->a;
  140. count++;
  141. temp=temp->next;
  142.  
  143. }
  144. return s/count;
  145. }
  146.  
  147.  
  148. void print()
  149. {
  150. data *temp=head;
  151.  
  152.  
  153. while(temp!=NULL)
  154. {
  155. printf("%d ",temp->a);
  156. temp=temp->next;
  157. }
  158. printf("\n");
  159.  
  160. }
  161.  
  162. int main()
  163. {
  164. /*end(1212);
  165. fst(9);
  166. end(10);
  167. fst(8);
  168. end(11);
  169. print();
  170.  
  171. nth(1,111);
  172. print();
  173.  
  174. printf("Sum=%d\n",sum());
  175. printf("Averaxe=%.2lf \n",avrg());
  176.  
  177. dbp(7);
  178. print();
  179.  
  180. dbv(111);
  181. print();
  182.  
  183. dbv(8);
  184. print();
  185.  
  186. dbv(9);
  187. print();
  188.  
  189.  
  190. printf("Sum=%d \n",sum());
  191.  
  192. printf("Averaxe=%.2lf \n",avrg());*/
  193. while(1)
  194. {
  195. int n, m,y;
  196. char p;
  197. printf("ASSALAMUALAIKUM...\nWhat u wanna do ?\n1.WANNA INSERT AT FIRST?\n2.WANNA INSERT AT END?\n");
  198. printf("3.WANNA INSERT AT ANY POSITION ?\n4.WANNA DELETE BY POSITION ?\n5.WANNA DELETE BY VALUE?\n6.Wanna Print These All?\n");
  199. printf("SELECT ANY OPTION FROM 1 to 6 OR 0(zero) TO QUIT .\n");
  200. scanf("%d",&m);
  201. if(m==1 || m==2 || m==4 || m==5)
  202. {
  203. printf("Please Enter a Number->");
  204.  
  205. if(m==1)
  206. {
  207. scanf("%d",&y);
  208. fst(y);
  209.  
  210. }
  211. else if(m==2)
  212. {
  213. scanf("%d",&y);
  214. end(y);
  215.  
  216. }
  217. else if(m==4)
  218. {
  219. scanf("%d",&y);
  220. dbp(y);
  221.  
  222. }
  223. else if(m==5)
  224. {
  225. scanf("%d",&y);
  226. dbv(y);
  227.  
  228. }
  229.  
  230. }
  231. else if(m==3)
  232. {
  233.  
  234. printf("Enter the specific position=>");
  235.  
  236. scanf("%d",&n);
  237. printf("Enter a Number What you wanna insert=>");
  238. scanf("%d",&y);
  239. nth(n,y);
  240. }
  241. else if(m==6)
  242. {
  243. print();
  244. }
  245. else if(m==0)
  246. {
  247. printf("Thank You For Staying With Mufty :> \n\n\n");
  248. break;
  249. }
  250.  
  251.  
  252.  
  253. }
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement