Advertisement
asiffff

LinkkHomeworkkkk

Feb 12th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node
  4. {
  5. int id;
  6. double prize;
  7. struct node *next;
  8. }*start=NULL;
  9.  
  10. void cre()
  11. {
  12. struct node *c,*n;
  13. int a,i;
  14. printf("\nEnter book number:\n");
  15. scanf("%d",&a);
  16. for(i=0;i<a;i++)
  17. {
  18. n=(struct node*)malloc(sizeof(struct node));
  19. printf("Enter book id:\n");
  20. scanf("%d",&n->id);
  21. printf("Enter book prize\n");
  22. scanf("%lf",&n->prize);
  23. n->next=NULL;
  24. if(start == NULL)
  25. {
  26. start=n;
  27. c=n;
  28. }
  29. else
  30. {
  31. c->next = n;
  32. c=n;
  33. }
  34.  
  35. }
  36.  
  37. }
  38.  
  39. void dis()
  40. {
  41. struct node *c;
  42. c=start;
  43. while(c!=NULL)
  44. {
  45. printf("Id-->%d-->%.1lf-->",c->id,c->prize);
  46. c=c->next;
  47. }
  48. printf("NULL");
  49. }
  50. int pos(int a)
  51. {
  52. int po=0,i=0;
  53. struct node *c;
  54. c=start;
  55. while(c!=NULL)
  56. {
  57. po++;
  58. if(c->id == a)
  59. {
  60. return po;
  61. }
  62. c=c->next;
  63. }
  64. return -1;
  65. }
  66. void mida()
  67. {
  68. struct node *c,*n,*t;
  69. printf("\nEnter a book details at midA:\n Enter Boook Id:\n");
  70. n=(struct node*)malloc(sizeof(struct node));
  71. scanf("%d",&n->id);
  72. printf("Enter prize:\n");
  73. scanf("%lf",&n->prize);
  74. int k,po=0;
  75. printf("Before Which Id?\n");
  76. scanf("%d",&k);
  77. po=pos(k);
  78. int i;
  79. c=start;
  80. for(i=0;i<po-1;i++)
  81. {
  82. c=c->next;
  83. }
  84. t=c->next;
  85. n->next=t;
  86. c->next=n;
  87.  
  88. }
  89.  
  90. void f()
  91. {
  92. struct node *c,*n;
  93. printf("\nEnter a book details at the top:\n Enter Boook Id:\n");
  94. n=(struct node*)malloc(sizeof(struct node));
  95. scanf("%d",&n->id);
  96. printf("Enter prize:\n");
  97. scanf("%lf",&n->prize);
  98. n->next=NULL;
  99. c=start;
  100. n->next=c;
  101. start=n;
  102.  
  103. }
  104. void last()
  105. {
  106. struct node *c,*n;
  107. printf("\nEnter a book details at the last:\n Enter Boook Id:\n");
  108. n=(struct node*)malloc(sizeof(struct node));
  109. scanf("%d",&n->id);
  110. printf("Enter prize:\n");
  111. scanf("%lf",&n->prize);
  112. c=start;
  113. while(c->next != NULL)
  114. {
  115. c=c->next;
  116. }
  117. c->next=n;
  118. n->next=NULL;
  119. }
  120.  
  121. void midb()
  122. {
  123. struct node *c,*n,*t;
  124. printf("\nEnter a book details at midB:\n Enter Boook Id:\n");
  125. n=(struct node*)malloc(sizeof(struct node));
  126. scanf("%d",&n->id);
  127. printf("Enter prize:\n");
  128. scanf("%lf",&n->prize);
  129. int k,po=0;
  130. printf("Before Which Id:\n");
  131. scanf("%d",&k);
  132. po=pos(k);
  133. int i;
  134. c=start;
  135. for(i=0;i<po-2;i++)
  136. {
  137. c=c->next;
  138. }
  139. t=c->next;
  140. n->next=t;
  141. c->next=n;
  142.  
  143. }
  144. void max()
  145. {
  146. struct node *c;
  147. c=start;
  148. double max=0;
  149. while(c!=NULL)
  150. {
  151. if(max<c->prize)
  152. {
  153. max=c->prize;
  154. }
  155. c=c->next;
  156. }
  157. printf("\nHighest prize is %.1lf\n",max);
  158. }
  159. void dele()
  160. {
  161. int dkey,po,i;
  162. struct node *current,*temp1,*temp2;
  163. printf("\nEnter the Book ID for deleting:\n");
  164. scanf("%d",&dkey);
  165. po=pos(dkey);
  166. current=start;
  167. for(i=1;i<=po-1;i++)
  168. {
  169. current=current->next;
  170. }
  171. if(current==start)
  172. {
  173. start=current->next;
  174. }
  175. else if(current->next==NULL)
  176. {
  177. current=start;
  178. for(i=1;i<=po-2;i++)
  179. {
  180. current=current->next;
  181. }
  182. current->next=NULL;
  183. }
  184. else
  185. {
  186. temp2=current->next;
  187. temp1=start;
  188. for(i=1;i<=po-2;i++)
  189. {
  190. temp1=temp1->next;
  191. }
  192. temp1->next=temp2;
  193. }
  194. printf("\nDelete Successfully.");
  195. }
  196. int main()
  197. {
  198. cre();
  199. dis();
  200. max();
  201. f();
  202. dis();
  203. last();
  204. dis();
  205. midb();
  206. dis();
  207. mida();
  208. dis();
  209. dele();
  210. dis();
  211. max();
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement