Advertisement
muftY

dekhoo (doubly , prb:del_by_pos)

Feb 14th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 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*temp1;
  57. data*temp2;
  58. printf("\nEnter the Position : ");
  59. laboni->a=x;
  60. printf("\nEnter the Data : ");
  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. temp1=head;
  77. for(i=0; i<x-2; i++)
  78. {
  79. temp1=temp1->next;
  80.  
  81. }
  82. laboni->next=temp1->next;
  83. laboni->prev=temp1;
  84. temp1->next=laboni;
  85. temp2=laboni->next;
  86. temp2->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("entry the first--");
  213. insert_at_first(3);
  214. insert_at_first(4);
  215. insert_at_first(6);
  216. print();
  217. printf("entry the end--");
  218. insert_at_end(7);
  219. insert_at_end(8);
  220. insert_at_end(9);
  221. print();
  222. InsertAtN(3,3);
  223.  
  224. print();
  225. deletenode(3);
  226.  
  227. print();
  228.  
  229. printf("the sum is %d \n",sum());
  230. printf("the avarage is %.2lf\n",avg());
  231. deletevalue(8);
  232. print();
  233. deletevalue(9);
  234. print();
  235.  
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement