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

reg:67cg:3.91
reg:98cg:3.7
reg:43cg:3.95
reg:34cg:3.57
reg:48cg:3.96

reg:67cg:3.91
reg:98cg:3.7
reg:43cg:3.95
reg:34cg:3.57
reg:48cg:3.96
reg:95cg:3.24
reg:100cg:3.12

reg:34cg:3.97
reg:45cg:3.98
reg:56cg:4
reg:67cg:3.91
reg:98cg:3.7
reg:43cg:3.95
reg:34cg:3.57
reg:48cg:3.96
reg:95cg:3.24
reg:100cg:3.12

Positions of 56,4.00:2
Positions of 2,3.98:-1
reg:45cg:3.98
reg:67cg:3.91
reg:98cg:3.7
reg:43cg:3.95
reg:34cg:3.57
reg:48cg:3.96
reg:95cg:3.24
reg:100cg:3.12