fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int reg;
  5. string name;
  6. float cg;
  7. Node*next;
  8. };
  9.  
  10.  
  11. Node*SortedInsert(Node*root,int reg,string name,float cg)
  12. {
  13. Node*newnode=new Node();
  14. newnode->next=NULL;
  15. newnode->reg=reg;
  16. newnode->name=name;
  17. newnode->cg=cg;
  18. Node*currnode,*prevnode;
  19. currnode=root;
  20. prevnode=NULL;
  21. if(root==NULL)
  22. {
  23. root=newnode;
  24. return root;
  25. }
  26. if(reg<currnode->reg || name<currnode->name || cg<currnode->cg)
  27. {
  28. newnode->next=root;
  29. root=newnode;
  30. return root;
  31. }
  32. while(currnode!=NULL)
  33. {
  34. if(currnode->reg<reg || currnode->name<name || currnode->cg<cg)
  35. {
  36. prevnode=currnode;
  37. currnode=currnode->next;
  38. }
  39. else
  40. {
  41. prevnode->next=newnode;
  42. newnode->next=currnode;
  43. return root;
  44. }
  45. }
  46. prevnode->next=newnode;
  47. newnode->next=NULL;
  48. return root;
  49. }
  50.  
  51. void Print(Node*root)
  52. {
  53. Node*currnode;
  54. currnode=root;
  55. while(currnode!=NULL)
  56. {
  57. cout<<"reg:"<<currnode->reg<<"name:"<<currnode->name<<"cg:"<<currnode->cg<<endl;
  58. currnode=currnode->next;
  59. }
  60. cout<<endl;
  61. }
  62. int main()
  63. {
  64. Node*root=NULL;
  65.  
  66. root=SortedInsert(root,98,"vu",3.45);
  67. root=SortedInsert(root,110,"Xu",2.78);
  68. Print(root);
  69.  
  70. }
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
reg:110name:Xucg:2.78
reg:98name:vucg:3.45