Advertisement
muftY

Singly

Feb 8th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.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. 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.  
  47. if(n==1 || head==NULL)
  48. {
  49. if(head==NULL)
  50. {
  51. printf("!!! Caution:Given Position is not avavilable .\nThat's why your input was placed at the 1st Position\n\n");
  52. }
  53. new->next=head;
  54. head=new;
  55. return;
  56. }
  57. data *temp=head;
  58. n=n-2;
  59. while(n-- && temp->next!=NULL)
  60. {
  61. temp=temp->next;
  62. }
  63. new->next=temp->next;
  64. temp->next=new;
  65. }
  66. void dbp(int n)
  67. {
  68.  
  69. data *del=NULL;
  70. data *temp=head;
  71. if(head==NULL)
  72. {
  73. printf("\nSorry, nothing to delete\n\n");
  74. return;
  75. }
  76. if(n==1)
  77. {
  78. del=head;
  79. head=head->next;
  80. free(del);
  81. }
  82.  
  83. n=n-2;
  84.  
  85. while(n-- && temp->next!=NULL)
  86. {
  87. temp=temp->next;
  88. if(temp->next==NULL)
  89. {
  90. return;
  91. }
  92.  
  93. }
  94. if(temp->next==NULL)
  95. {
  96. return;
  97. }
  98. del=temp->next;
  99. temp->next=del->next;
  100. free(del);
  101.  
  102. }
  103.  
  104.  
  105. void dbv(int x)
  106. {
  107.  
  108. data *del=NULL;
  109. data *temp=head;
  110. if(head==NULL)
  111. {
  112. return;
  113. }
  114. if(head->a==x)
  115. {
  116. del=head;
  117. head=del->next;
  118. free(del);
  119. return;
  120. }
  121. while(temp->next->a!=x)
  122. {
  123. temp=temp->next;
  124. if(temp->next==NULL)
  125. {
  126. return;
  127. }
  128. }
  129. del=temp->next;
  130. temp->next=del->next;
  131. free(del);
  132.  
  133.  
  134.  
  135.  
  136. }
  137.  
  138. int sum()
  139. {
  140. int s=0;
  141. data *temp=head;
  142. while(temp!=NULL)
  143. {
  144. s+=temp->a;
  145. temp=temp->next;
  146.  
  147. }
  148. return s;
  149. }
  150. double avrg()
  151. {
  152. double s=0;
  153. int count=0;
  154. data *temp=head;
  155. while(temp!=NULL)
  156. {
  157. s+=temp->a;
  158. count++;
  159. temp=temp->next;
  160.  
  161. }
  162. if(count==0)
  163. {
  164. printf("\nYou have not Input Any Value YET.\n");
  165. printf("***Please Chose Another option...***\n");
  166. return 0;
  167. }
  168. return s/count;
  169. }
  170.  
  171.  
  172. void print()
  173. {
  174. data *temp=head;
  175.  
  176. while(temp!=NULL)
  177. {
  178. printf("%d ",temp->a);
  179. temp=temp->next;
  180. }
  181. printf("\n");
  182.  
  183. }
  184.  
  185. int main()
  186. {
  187. /*end(1212);
  188. fst(9);
  189. end(10);
  190. fst(8);
  191. end(11);
  192. print();
  193.  
  194. nth(1,111);4
  195. print();
  196.  
  197. printf("Sum=%d\n",sum());
  198. printf("Averaxe=%.2lf \n",avrg());
  199.  
  200. dbp(7);
  201. print();
  202.  
  203. dbv(111);
  204. print();
  205.  
  206. dbv(8);
  207. print();
  208.  
  209. dbv(9);
  210. print();
  211.  
  212.  
  213. printf("Sum=%d \n",sum());
  214.  
  215. printf("Averaxe=%.2lf \n",avrg());*/
  216. printf("\nHey ,ASSALAMUALAIKUM...\n");
  217. while(1)
  218. {
  219. int n, m,y;
  220. char p;
  221. printf("\n\nWhat u wanna do?\n\n");
  222. printf("1.WANNA INSERT AT FIRST?\n");
  223. printf("2.WANNA INSERT AT END?\n");
  224. printf("3.WANNA INSERT AT ANY POSITION?\n");
  225. printf("4.WANNA DELETE BY POSITION ?\n");
  226. printf("5.WANNA DELETE BY VALUE?\n");
  227. printf("6.Wanna Print Sum Of These?\n");
  228. printf("7.Wanna Print Average Of These All?\n");
  229. printf("8.Wanna Print These All?\n\n");
  230.  
  231. printf("##SELECT ANY OPTION FROM 1 to 8.##\n\n");
  232. printf("###OR Enter 0(zero) TO QUIT !!!###\n");
  233. scanf("%d",&m);
  234. if(m==1 || m==2 || m==5)
  235. {
  236. printf("Please Enter a Number->");
  237.  
  238. if(m==1)
  239. {
  240. scanf("%d",&y);
  241. fst(y);
  242.  
  243. }
  244. else if(m==2)
  245. {
  246. scanf("%d",&y);
  247. end(y);
  248.  
  249. }
  250.  
  251. else if(m==5)
  252. {
  253. scanf("%d",&y);
  254. dbv(y);
  255.  
  256. }
  257.  
  258. }
  259. else if(m==3)
  260. {
  261.  
  262. printf("Enter the specific position=> ");
  263.  
  264. scanf("%d",&n);
  265. printf("Enter a Number What you wanna insert=> ");
  266. scanf("%d",&y);
  267. nth(n,y);
  268. }
  269. else if(m==4)
  270. {
  271. printf("Please Enter a Position-> ");
  272. scanf("%d",&y);
  273. dbp(y);
  274.  
  275. }
  276.  
  277. else if(m==6)
  278. {
  279. printf("Sum is =%d\n",sum());
  280. }
  281. else if(m==7)
  282. {
  283. printf("Average is=%.2lf\n",avrg());
  284. }
  285. else if(m==8)
  286. {
  287. printf("Here is the result=> ");
  288. print();
  289. }
  290. else if(m==0)
  291. {
  292. printf("Thank You For Staying With Mufty :> \n\n\n");
  293. break;
  294. }
  295.  
  296.  
  297.  
  298. }
  299.  
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement