fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int reg;
  5. string name;
  6. float cg;
  7. Node*next;
  8. };
  9. Node*InsertAtBegin(Node*root,int reg,string name,float cg)
  10. {
  11. Node*newnode=new Node();
  12. newnode->next=NULL;
  13. newnode->reg=reg;
  14. newnode->name=name;
  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 name,float cg,int pos)
  29. {
  30. if(pos==0)
  31. {
  32. root=InsertAtBegin(root,reg,name,cg);
  33. }
  34. else
  35. {
  36. Node*newnode=new Node();
  37. newnode->next=NULL;
  38. newnode->reg=reg;
  39. newnode->name=name;
  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 name,float cg)
  53. {
  54. Node*newnode=new Node();
  55. newnode->next=NULL;
  56. newnode->reg=reg;
  57. newnode->name=name;
  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 name,float cg)
  75. {
  76. Node*newnode=new Node();
  77. newnode->next=NULL;
  78. newnode->reg=reg;
  79. newnode->name=name;
  80. newnode->cg=cg;
  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<currnode->reg || name<currnode->name || cg<currnode->cg)
  90. {
  91. newnode->next=root;
  92. root=newnode;
  93. return root;
  94. }
  95. while(currnode!=NULL)
  96. {
  97. if(currnode->reg<reg || currnode->name<name || 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 name,float cg)
  114. {
  115. int pos=0;
  116. Node*currnode;
  117. currnode=root;
  118. while(currnode!=NULL)
  119. {
  120. if(currnode->reg==reg && currnode->name==name && 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 name,float cg)
  133. {
  134. Node*currnode,*prevnode;
  135. currnode=root;
  136. prevnode=NULL;
  137. while(currnode!=NULL)
  138. {
  139. if(currnode->reg!=reg && currnode->name!=name && 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<<"name:"<<currnode->name<<"cg:"<<currnode->cg<<endl;
  168. currnode=currnode->next;
  169. }
  170. cout<<endl;
  171. }
  172. int main()
  173. {
  174. Node*root=NULL;
  175. root=InsertAtBegin(root,101,"tu",3.00);
  176. root=InsertAtBegin(root,106,"lu",3.08);
  177. root=InsertAtBegin(root,100,"Mu",3.56);
  178. Print(root);
  179. root=InsertAtPos(root,99,"Nu",3.30,0);
  180. root=InsertAtPos(root,107,"pu",3.67,3);
  181. Print(root);
  182. root=InsertAtEnd(root,108,"ku",3.29);
  183. root=InsertAtEnd(root,109,"yu",3.60);
  184. Print(root);
  185. root=SortedInsert(root,98,"vu",3.45);
  186. root=SortedInsert(root,110,"Xu",2.78);
  187. Print(root);
  188. cout<<"positions of 107,pu,3.67:"<<Search(root,107,"pu",3.67)<<endl;
  189. cout<<"positions of 115,du,3.66:"<<Search(root,115,"du",3.66)<<endl;
  190. root=Delete(root,99,"Nu",3.30);
  191. Print(root);
  192. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
reg:100name:Mucg:3.56
reg:106name:lucg:3.08
reg:101name:tucg:3

reg:99name:Nucg:3.3
reg:100name:Mucg:3.56
reg:106name:lucg:3.08
reg:107name:pucg:3.67
reg:101name:tucg:3

reg:99name:Nucg:3.3
reg:100name:Mucg:3.56
reg:106name:lucg:3.08
reg:107name:pucg:3.67
reg:101name:tucg:3
reg:108name:kucg:3.29
reg:109name:yucg:3.6

reg:110name:Xucg:2.78
reg:98name:vucg:3.45
reg:99name:Nucg:3.3
reg:100name:Mucg:3.56
reg:106name:lucg:3.08
reg:107name:pucg:3.67
reg:101name:tucg:3
reg:108name:kucg:3.29
reg:109name:yucg:3.6

positions of 107,pu,3.67:5
positions of 115,du,3.66:-1
reg:110name:Xucg:2.78
reg:98name:vucg:3.45
reg:100name:Mucg:3.56
reg:106name:lucg:3.08
reg:107name:pucg:3.67
reg:101name:tucg:3
reg:108name:kucg:3.29
reg:109name:yucg:3.6