Advertisement
muftY

Singly Updated

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