fork download
  1.  
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. struct Node{
  6. int reg;
  7. float cg;
  8. Node*next;
  9. };
  10. Node*InsertAtBegin(Node*root,int reg,float cg)
  11. {
  12. Node*newnode=new Node();
  13. newnode->next=NULL;
  14. newnode->reg=reg;
  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,float cg,int pos)
  29. {
  30. if(pos==0)
  31. {
  32. root=InsertAtBegin(root,reg,cg);
  33. }
  34. else
  35. {
  36. Node*newnode=new Node();
  37. newnode->next=NULL;
  38. newnode->reg=reg;
  39. newnode->cg=cg;
  40. Node*currnode;
  41. currnode=root;
  42. for(int i=1;i<pos;i++)
  43. {
  44. currnode=currnode->next;
  45. }
  46. newnode->next=currnode->next;
  47. currnode->next=newnode;
  48. }
  49. return root;
  50. }
  51. Node*InsertAtEnd(Node*root,int reg,float cg)
  52. {
  53. Node*newnode=new Node();
  54. newnode->next=NULL;
  55. newnode->reg=reg;
  56. newnode->cg=cg;
  57. if(root==NULL)
  58. {
  59. root=newnode;
  60. return root;
  61. }
  62. Node*currnode;
  63. currnode=root;
  64. while(currnode->next!=NULL)
  65. {
  66. currnode=currnode->next;
  67. }
  68. currnode->next=newnode;
  69. return root;
  70. }
  71.  
  72. Node*SortedInsert(Node*root,int reg,float cg)
  73. {
  74. Node*newnode=new Node();
  75. newnode->reg=reg;
  76. newnode->cg=cg;
  77. newnode->next=NULL;
  78. Node*currnode,*prevnode;
  79. currnode=root;
  80. prevnode=NULL;
  81. if(root==NULL)
  82. {
  83. root=newnode;
  84. return root;
  85. }
  86. if(reg<root->reg || cg<root->cg)
  87. {
  88. newnode->next=root;
  89. root=newnode;
  90. return root;
  91. }
  92. while(currnode!=NULL)
  93. {
  94. if(currnode->reg<reg || currnode->cg<cg)
  95. {
  96. prevnode=currnode;
  97. currnode=currnode->next;
  98. }
  99. else
  100. {
  101. prevnode->next=newnode;
  102. newnode->next=currnode;
  103. return root;
  104. }
  105. }
  106. prevnode->next=newnode;
  107. newnode->next=NULL;
  108. return root;
  109. }
  110. int Search(Node*root,int reg,float cg)
  111. {
  112. int pos=0;
  113. Node*currnode;
  114. currnode=root;
  115. while(currnode!=NULL)
  116. {
  117. if(currnode->reg==reg || currnode->cg==cg)
  118. {
  119. return pos;
  120. }
  121. else
  122. {
  123. currnode=currnode->next;
  124. pos++;
  125. }
  126. }
  127. return -1;
  128. }
  129. void Print(Node*root)
  130. {
  131. Node*currnode;
  132. currnode=root;
  133. while(currnode!=NULL)
  134. {
  135. cout<<"reg:"<<currnode->reg<<"cg:"<<currnode->cg<<endl;
  136. currnode=currnode->next;
  137. }
  138. cout<<endl;
  139. }
  140. int main()
  141. {
  142. Node*root=NULL;
  143.  
  144. root=InsertAtBegin(root,48,3.96);
  145. root=InsertAtBegin(root,43,3.95);
  146. root=InsertAtBegin(root,98,3.70);
  147. Print(root);
  148. root=InsertAtPos(root,67,3.91,0);
  149. root=InsertAtPos(root,34,3.57,3);
  150. Print(root);
  151. root=InsertAtEnd(root,95,3.24);
  152. root=InsertAtEnd(root,100,3.12);
  153. Print(root);
  154. root=SortedInsert(root,56,4.00);
  155. root=SortedInsert(root,45,3.98);
  156. root=SortedInsert(root,34,3.97);
  157. Print(root);
  158. cout<<"Positions of 56,4.00:"<<Search(root,56,4.00)<<endl;
  159. cout<<"Positions of 2,3.98:"<<Search(root,2,3.98)<<endl;
  160. }
  161.  
Success #stdin #stdout 0s 5296KB
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