Advertisement
Guest User

Untitled

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