fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int reg;
  5. string name;
  6. double cg;
  7. Node*next;
  8. };
  9. Node*InsertAtBegin(Node*root,int reg,string name,double cg)
  10. {
  11. Node*newnode=new Node();
  12. newnode->reg=reg;
  13. newnode->name=name;
  14. newnode->cg=cg;
  15. newnode->next=NULL;
  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,double 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->reg=reg;
  38. newnode->name=name;
  39. newnode->cg=cg;
  40. newnode->next=NULL;
  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,double cg)
  53. {
  54. Node*newnode=new Node();
  55. newnode->reg=reg;
  56. newnode->name=name;
  57. newnode->cg=cg;
  58. newnode->next=NULL;
  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. Node*SortedInsert(Node*root,int reg,string name,double cg)
  74. {
  75. Node*newnode=new Node();
  76. newnode->reg=reg;
  77. newnode->name=name;
  78. newnode->cg=cg;
  79. newnode->next=NULL;
  80. Node*currnode,*prevnode;
  81. currnode=root;
  82. prevnode=NULL;
  83. if(root==NULL)
  84. {
  85. root=newnode;
  86. return root;
  87. }
  88. if(reg<root->reg || name<root->name || cg<root->cg)
  89. {
  90. newnode->next=root;
  91. root=newnode;
  92. return root;
  93. }
  94. while(currnode!=NULL)
  95. {
  96. if(currnode->reg<reg)
  97. {
  98. prevnode=currnode;
  99. currnode=currnode->next;
  100. }
  101. else
  102. {
  103. prevnode->next=newnode;
  104. newnode->next=currnode;
  105. return root;
  106. }
  107. }
  108. prevnode->next=newnode;
  109. newnode->next=NULL;
  110. return root;
  111. }
  112. int Search(Node*root,int reg,string name,double cg)
  113. {
  114. int pos=0;
  115. Node*currnode;
  116. currnode=root;
  117. while(currnode!=NULL)
  118. {
  119. if(currnode->reg==reg && currnode->name==name && currnode->cg==cg)
  120. {
  121. return pos;
  122. }
  123. else
  124. {
  125. currnode=currnode->next;
  126. pos++;
  127. }
  128. }
  129. return -1;
  130. }
  131. Node*Delete(Node*root,int reg,string name,double cg)
  132. {
  133. Node*currnode,*prevnode;
  134. currnode=root;
  135. prevnode=NULL;
  136. while(currnode!=NULL)
  137. {
  138. if(currnode->reg!=reg && currnode->name!=name && currnode->cg!=cg)
  139. {
  140. prevnode=currnode;
  141. currnode=currnode->next;
  142. }
  143. if(currnode==root)
  144. {
  145. root=root->next;
  146. delete(currnode);
  147. }
  148. else
  149. {
  150. prevnode->next=currnode->next;
  151. delete(currnode);
  152. }
  153. break;
  154. }
  155. return root;
  156. }
  157. void Print(Node*root)
  158. {
  159. Node*currnode;
  160. currnode=root;
  161. while(currnode!=NULL)
  162. {
  163. cout<<"reg:"<<currnode->reg<<" "<<"name:"<<currnode->name<<" "<<"cg;"<<currnode->cg<<" "<<endl;
  164. currnode=currnode->next;
  165. }
  166. cout<<endl;
  167. }
  168. int main()
  169. {
  170. Node*root=NULL;
  171. root=InsertAtBegin(root,101,"tu",4.00);
  172. root=InsertAtBegin(root,108,"ku",3.90);
  173. root=InsertAtBegin(root,102,"lu",3.91);
  174. Print(root);
  175. root=InsertAtPos(root,100,"mu",3.98,0);
  176. root=InsertAtPos(root,104,"nu",3.55,3);
  177. Print(root);
  178. root=InsertAtEnd(root,109,"su",3.24);
  179. root=InsertAtEnd(root,122,"cu",3.17);
  180. Print(root);
  181. cout<<"Position of 102,lu,3.91:"<<Search(root,102,"lu",3.91)<<endl;
  182. cout<<"Position of 123,ou,1.89:"<<Search(root,123,"ou",1.89)<<endl;
  183. root=Delete(root,104,"nu",3.55);
  184. root=Delete(root,135,"au",2.3);
  185. Print(root);
  186. }
  187.  
  188.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
reg:102 name:lu cg;3.91 
reg:108 name:ku cg;3.9 
reg:101 name:tu cg;4 

reg:100 name:mu cg;3.98 
reg:102 name:lu cg;3.91 
reg:108 name:ku cg;3.9 
reg:104 name:nu cg;3.55 
reg:101 name:tu cg;4 

reg:100 name:mu cg;3.98 
reg:102 name:lu cg;3.91 
reg:108 name:ku cg;3.9 
reg:104 name:nu cg;3.55 
reg:101 name:tu cg;4 
reg:109 name:su cg;3.24 
reg:122 name:cu cg;3.17 

Position of 102,lu,3.91:1
Position of 123,ou,1.89:-1
reg:100 name:mu cg;3.98 
reg:104 name:nu cg;3.55 
reg:101 name:tu cg;4 
reg:109 name:su cg;3.24 
reg:122 name:cu cg;3.17