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)
  86. {
  87. newnode->next=root;
  88. root=newnode;
  89. return root;
  90. }
  91. while(currnode!=NULL)
  92. {
  93. if(currnode->reg<reg)
  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. void Print(Node*root)
  110. {
  111. Node*currnode;
  112. currnode=root;
  113. while(currnode!=NULL)
  114. {
  115. cout<<"reg:"<<currnode->reg<<"cg:"<<currnode->cg<<endl;
  116. currnode=currnode->next;
  117. }
  118. cout<<endl;
  119. }
  120. int main()
  121. {
  122. Node*root=NULL;
  123.  
  124. root=InsertAtBegin(root,48,3.96);
  125. root=InsertAtBegin(root,43,3.95);
  126. root=InsertAtBegin(root,98,3.70);
  127. Print(root);
  128. root=InsertAtPos(root,67,3.91,0);
  129. root=InsertAtPos(root,34,3.57,3);
  130. Print(root);
  131. root=InsertAtEnd(root,95,3.24);
  132. root=InsertAtEnd(root,100,3.12);
  133. Print(root);
  134. root=SortedInsert(root,56,4.00);
  135. root=SortedInsert(root,45,3.98);
  136. root=SortedInsert(root,34,3.97);
  137. Print(root);
  138. }
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:100cg:3.12

reg:34cg:3.97
reg:45cg:3.98
reg:56cg:4
reg:67cg:3.91
reg:100cg:3.12