asiffff

Dublink

Feb 9th, 2019
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int id;
  6. struct node *next;
  7. struct node *prev;
  8. }*start=NULL,*end=NULL;
  9. void create()
  10. {
  11. int n,i;
  12. struct node *new_node,*current;
  13. printf("Enter the number of student: \n");
  14. scanf("%d",&n);
  15. for(i=0; i<n; i++)
  16. {
  17. new_node=(struct node*)malloc(sizeof(struct node));
  18.  
  19. printf("Enter the id of student no %d: \n",i+1);
  20. scanf("%d",&new_node->id);
  21. new_node->next=NULL;
  22. new_node->prev=NULL;
  23. if(start==NULL&&end==NULL)
  24. {
  25. start=new_node;
  26. end=new_node;
  27. current=new_node;
  28. }
  29. else
  30. {
  31. current->next=new_node;
  32. new_node->prev=current;
  33. current=new_node;
  34. end=new_node;
  35. }
  36. }
  37. }
  38.  
  39. void f_insert()
  40. {
  41. struct node *new_node,*current;
  42. new_node=(struct node*)malloc(sizeof(struct node));
  43. printf("\nEnter the first id of student no : \n");
  44. scanf("%d",&new_node->id);
  45. new_node->next=NULL;
  46. new_node->prev=NULL;
  47. current=start;
  48. start=new_node;
  49. new_node->next=current;
  50. current->prev=new_node;
  51. }
  52.  
  53. void l_insert()
  54. {
  55. struct node *new_node,*current;
  56. new_node=(struct node*)malloc(sizeof(struct node));
  57. printf("\nEnter the last id of student no : \n");
  58. scanf("%d",&new_node->id);
  59. new_node->next=NULL;
  60. new_node->prev=NULL;
  61. current=end;
  62. current->next=new_node;
  63. new_node->prev=current;
  64. end=new_node;
  65. }
  66. int search(int s_key)
  67. {
  68. int pos=0;
  69. struct node *current;
  70. current=start;
  71. while(current!=NULL)
  72. {
  73. pos++;
  74. if(current->id==s_key)
  75. {
  76. return pos;
  77. }
  78. current=current->next;
  79. }
  80. return -1;
  81. }
  82. void mb_insert()
  83. {
  84. int key,i,pos;
  85. struct node *temp,*current,*new_node;
  86. new_node=(struct node*)malloc(sizeof(struct node));
  87. printf("\nEnter the middle(before) id of student no : \n");
  88. scanf("%d",&new_node->id);
  89. new_node->next=NULL;
  90. new_node->prev=NULL;
  91. printf("\nEnter the id before the new node insert:\n");
  92. scanf("%d",&key);
  93. pos=search(key);
  94. current=start;
  95. for(i=1;i<=pos-1;i++)
  96. {
  97. current=current->next;
  98. }
  99. temp=current->prev;
  100. temp->next=new_node;
  101. new_node->prev=temp;
  102. new_node->next=current;
  103. current->prev=new_node;
  104.  
  105. }
  106. void ma_insert()
  107. {
  108. int key,i,pos;
  109. struct node *temp,*current,*new_node;
  110. new_node=(struct node*)malloc(sizeof(struct node));
  111. printf("\nEnter the middle(after) id of student no : \n");
  112. scanf("%d",&new_node->id);
  113. new_node->next=NULL;
  114. new_node->prev=NULL;
  115. printf("\nEnter the id after the new node insert:\n");
  116. scanf("%d",&key);
  117. pos=search(key);
  118. current=start;
  119. for(i=1;i<=pos-1;i++)
  120. {
  121. current=current->next;
  122. }
  123. //printf("\nCurrent=%d",current->id);
  124. temp=current->next;
  125. current->next=new_node;
  126. new_node->prev=current;
  127. new_node->next=temp;
  128. temp->prev=new_node;
  129.  
  130. }
  131. void delete_ll()
  132. {
  133. int dkey,pos,i;
  134. struct node *current,*temp1,*temp2;
  135. printf("\nEnter the value for deleting:\n");
  136. scanf("%d",&dkey);
  137. pos=search(dkey);
  138. current=start;
  139. for(i=1;i<=pos-1;i++)
  140. {
  141. current=current->next;
  142. }
  143. if(current==start)
  144. {
  145. temp1=current->next;
  146. temp1->prev=NULL;
  147. start=temp1;
  148. }
  149. else if(current==end)
  150. {
  151. temp1=current->prev;
  152. temp1->next=NULL;
  153. end=temp1;
  154. }
  155. else
  156. {
  157. temp1=current->prev;
  158. temp2=current->next;
  159. temp1->next=temp2;
  160. temp2->prev=temp1;
  161. }
  162. printf("\nDelete Successfully.");
  163. }
  164. void f_display()
  165. {
  166. struct node *current;
  167. current=start;
  168. printf("\nThe Link List from start is:\n");
  169. while(current!=NULL)
  170. {
  171. printf("%d-->",current->id);
  172. current=current->next;
  173. }
  174. printf("NULL\n");
  175. }
  176. void b_display()
  177. {
  178. struct node *current;
  179. current=end;
  180. printf("\nThe Link List from last is:\n");
  181. while(current!=NULL)
  182. {
  183. printf("%d-->",current->id);
  184. current=current->prev;
  185. }
  186. printf("NULL\n");
  187. }
  188. void max()
  189. {
  190. int max=-100000;
  191. struct node *current;
  192. current=start;
  193. while(current!=NULL)
  194. {
  195. if(current->id>max)
  196. {
  197. max=current->id;
  198. }
  199. current=current->next;
  200. }
  201. printf("\nThe maximum value is:%d.",max);
  202. }
  203. void sum_avg()
  204. {
  205. int sum=0,c=0;
  206. float avg;
  207. struct node *current;
  208. current=start;
  209. while(current!=NULL)
  210. {
  211. sum=sum+current->id;
  212. current=current->next;
  213. }
  214. printf("\nThe summation value is:%d.",sum);
  215. }
  216. void count()
  217. {
  218. int c=0;
  219.  
  220. struct node *current;
  221. current=start;
  222. while(current!=NULL)
  223. {
  224. c++;
  225. current=current->next;
  226. }
  227. printf("\nThe num of node in the LL is:%d.",c);
  228. }
  229. void even_freq()
  230. {
  231. int even=0;
  232. struct node *current;
  233. current=start;
  234. while(current!=NULL)
  235. {
  236. if(current->id%2==0)
  237. {
  238. even++;
  239. }
  240. current=current->next;
  241. }
  242. printf("\nThe total even num is:%d.",even);
  243. }
  244.  
  245. int main()
  246. {
  247. int s_key,pos;
  248. create();
  249. f_display();
  250. b_display();
  251. /*f_insert();
  252. f_display();
  253. b_display();
  254. l_insert();
  255. f_display();
  256. b_display();
  257. printf("\nEnter the id for searching:");
  258. scanf("%d",&s_key);
  259. pos=search(s_key);
  260. if(pos==-1)
  261. {
  262. printf("\nNOT FOUND");
  263. }
  264. else
  265. {
  266. printf("\n FOUND at %d",pos);
  267. }
  268. mb_insert();
  269. f_display();
  270. b_display();
  271. ma_insert();
  272. f_display();
  273. b_display();*/
  274. delete_ll();
  275. f_display();
  276. b_display();
  277. max();
  278. sum_avg();
  279. count();
  280. even_freq();
  281. return 0;
  282.  
  283.  
  284. }
Advertisement
Add Comment
Please, Sign In to add comment