fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int reg;
  5. string s;
  6. float cg;
  7. Node*next;
  8. };
  9. Node*InsertAtBegin(Node*root,int reg,string s,float cg)
  10. {
  11. Node*newnode=new Node();
  12. newnode->next=NULL;
  13. newnode->reg=reg;
  14. newnode->s=s;
  15. newnode->cg=cg;
  16. if(root==NULL)
  17. {
  18. root=newnode;
  19. return root;
  20. }
  21. else
  22. {
  23. newnode->next=root;
  24. root=newnode;
  25. return root;
  26. }
  27. }
  28. Node*InsertAtPos(Node*root,int reg,string s,float cg,int pos)
  29. {
  30. if(pos==0)
  31. {
  32. root=InsertAtBegin(root,reg,s,cg);
  33. }
  34. else
  35. {
  36. Node*newnode=new Node();
  37. newnode->next=NULL;
  38. newnode->reg=reg;
  39. newnode->s=s;
  40. newnode->cg=cg;
  41. Node*currnode;
  42. currnode=root;
  43. for(int i=1;i<pos;i++)
  44. {
  45. currnode=currnode->next;
  46. }
  47. newnode->next=currnode->next;
  48. currnode->next=newnode;
  49. }
  50. return root;
  51. }
  52. Node*InsertAtEnd(Node*root,int reg,string s,float cg)
  53. {
  54. Node*newnode=new Node();
  55. newnode->next=NULL;
  56. newnode->reg=reg;
  57. newnode->s=s;
  58. newnode->cg=cg;
  59. if(root==NULL)
  60. {
  61. root=newnode;
  62. return root;
  63. }
  64. Node*currnode;
  65. currnode=root;
  66. while(currnode->next!=NULL)
  67. {
  68. currnode=currnode->next;
  69. }
  70. currnode->next=newnode;
  71. return root;
  72. }
  73.  
  74. Node*SortedInsert(Node*root,int reg,string s,float cg)
  75. {
  76. Node*newnode=new Node();
  77. newnode->reg=reg;
  78. newnode->s=s;
  79. newnode->cg=cg;
  80. newnode->next=NULL;
  81. Node*currnode,*prevnode;
  82. currnode=root;
  83. prevnode=NULL;
  84. if(root==NULL)
  85. {
  86. root=newnode;
  87. return root;
  88. }
  89. if(reg<root->reg || s<root->s || cg<root->cg)
  90. {
  91. newnode->next=root;
  92. root=newnode;
  93. return root;
  94. }
  95. while(currnode!=NULL)
  96. {
  97. if(currnode->reg<reg || currnode->s<s || currnode->cg<cg)
  98. {
  99. prevnode=currnode;
  100. currnode=currnode->next;
  101. }
  102. else
  103. {
  104. prevnode->next=newnode;
  105. newnode->next=currnode;
  106. return root;
  107. }
  108. }
  109. prevnode->next=newnode;
  110. newnode->next=NULL;
  111. return root;
  112. }
  113. int Search(Node*root,int reg,string s,float cg)
  114. {
  115. int pos=0;
  116. Node*currnode;
  117. currnode=root;
  118. while(currnode!=NULL)
  119. {
  120. if(currnode->reg==reg && currnode->s==s && currnode->cg==cg)
  121. {
  122. return pos;
  123. }
  124. else
  125. {
  126. currnode=currnode->next;
  127. pos++;
  128. }
  129. }
  130. return -1;
  131. }
  132. Node*Delete(Node*root,int reg,string s,float cg)
  133. {
  134. Node*currnode,*prevnode;
  135. currnode=root;
  136. prevnode=NULL;
  137. while(currnode!=NULL)
  138. {
  139. if(currnode->reg!=reg && currnode->s!=s && currnode->cg!=cg)
  140. {
  141. prevnode=currnode;
  142. currnode=currnode->next;
  143. }
  144. else
  145. {
  146. if(currnode==root)
  147. {
  148. root=root->next;
  149. delete(currnode);
  150. }
  151. else
  152. {
  153. prevnode->next=currnode->next;
  154. delete(currnode);
  155. }
  156. break;
  157. }
  158. }
  159. return root;
  160. }
  161. void Print(Node*root)
  162. {
  163. Node*currnode;
  164. currnode=root;
  165. while(currnode!=NULL)
  166. {
  167. cout<<"reg:"<<currnode->reg<<"s:"<<currnode->s<<"cg:"<<currnode->cg<<endl;
  168. currnode=currnode->next;
  169. }
  170. cout<<endl;
  171. }
  172. int main()
  173. {
  174. Node*root=NULL;
  175.  
  176. root=InsertAtBegin(root,48,"tu",3.96);
  177. root=InsertAtBegin(root,43,"Cu",3.95);
  178. root=InsertAtBegin(root,98,"ku",3.70);
  179. Print(root);
  180. root=InsertAtPos(root,67,"Mu",3.91,0);
  181. root=InsertAtPos(root,34,"lu",3.57,3);
  182. Print(root);
  183. root=InsertAtEnd(root,95,"au",3.24);
  184. root=InsertAtEnd(root,100,"bu",3.12);
  185. Print(root);
  186. root=SortedInsert(root,56,"fu",4.00);
  187. root=SortedInsert(root,45,"zu",3.98);
  188. root=SortedInsert(root,37,"xu",3.97);
  189. Print(root);
  190. cout<<"Positions of 56,fu,4.00:"<<Search(root,56,"fu",4.00)<<endl;
  191. cout<<"Positions of 2,lu,3.98:"<<Search(root,2,"lu",3.98)<<endl;
  192. root=Delete(root,56,"fu",4.00);
  193. root=Delete(root,105,"ru",3.65);
  194. Print(root);
  195. }
  196.  
Success #stdin #stdout 0s 5260KB
stdin
Standard input is empty
stdout
reg:98s:kucg:3.7
reg:43s:Cucg:3.95
reg:48s:tucg:3.96

reg:67s:Mucg:3.91
reg:98s:kucg:3.7
reg:43s:Cucg:3.95
reg:34s:lucg:3.57
reg:48s:tucg:3.96

reg:67s:Mucg:3.91
reg:98s:kucg:3.7
reg:43s:Cucg:3.95
reg:34s:lucg:3.57
reg:48s:tucg:3.96
reg:95s:aucg:3.24
reg:100s:bucg:3.12

reg:37s:xucg:3.97
reg:45s:zucg:3.98
reg:56s:fucg:4
reg:67s:Mucg:3.91
reg:98s:kucg:3.7
reg:43s:Cucg:3.95
reg:34s:lucg:3.57
reg:48s:tucg:3.96
reg:95s:aucg:3.24
reg:100s:bucg:3.12

Positions of 56,fu,4.00:2
Positions of 2,lu,3.98:-1
reg:37s:xucg:3.97
reg:45s:zucg:3.98
reg:67s:Mucg:3.91
reg:98s:kucg:3.7
reg:43s:Cucg:3.95
reg:34s:lucg:3.57
reg:48s:tucg:3.96
reg:95s:aucg:3.24
reg:100s:bucg:3.12