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