Advertisement
muftY

link list Full by muftY

Feb 7th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 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. printf("Hey ,ASSALAMUALAIKUM...\n");
  194. while(1)
  195. {
  196. int n, m,y;
  197. char p;
  198. printf("What u wanna do?\n\n");
  199. printf("1.WANNA INSERT AT FIRST?\n");
  200. printf("2.WANNA INSERT AT END?\n");
  201. printf("3.WANNA INSERT AT ANY POSITION?\n");
  202. printf("4.WANNA DELETE BY POSITION ?\n");
  203. printf("5.WANNA DELETE BY VALUE?\n");
  204. printf("6.Wanna Print Sum Of These?\n");
  205. printf("7.Wanna Print Average Of These All?\n");
  206. printf("8.Wanna Print These All?\n\n");
  207.  
  208. printf("##SELECT ANY OPTION FROM 1 to 8.##\n\n");
  209. printf("###OR Enter 0(zero) TO QUIT !!!###\n");
  210. scanf("%d",&m);
  211. if(m==1 || m==2 || m==4 || m==5)
  212. {
  213. printf("Please Enter a Number->");
  214.  
  215. if(m==1)
  216. {
  217. scanf("%d",&y);
  218. fst(y);
  219.  
  220. }
  221. else if(m==2)
  222. {
  223. scanf("%d",&y);
  224. end(y);
  225.  
  226. }
  227. else if(m==4)
  228. {
  229. scanf("%d",&y);
  230. dbp(y);
  231.  
  232. }
  233. else if(m==5)
  234. {
  235. scanf("%d",&y);
  236. dbv(y);
  237.  
  238. }
  239.  
  240. }
  241. else if(m==3)
  242. {
  243.  
  244. printf("Enter the specific position=>");
  245.  
  246. scanf("%d",&n);
  247. printf("Enter a Number What you wanna insert=>");
  248. scanf("%d",&y);
  249. nth(n,y);
  250. }
  251.  
  252. else if(m==6)
  253. {
  254. printf("Sum is =%d\n",sum());
  255. }
  256. else if(m==7)
  257. {
  258. printf("Average is=%.2lf\n",avrg());
  259. }
  260. else if(m==8)
  261. {
  262. printf("See__> ");
  263. print();
  264. }
  265. else if(m==0)
  266. {
  267. printf("Thank You For Staying With Mufty :> \n\n\n");
  268. break;
  269. }
  270.  
  271.  
  272.  
  273. }
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement