fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Node {
  5. int reg;
  6. float cg;
  7. Node* next;
  8. };
  9.  
  10.  
  11. Node* InsertAtBegin(Node* root, int reg,float cg) {
  12. Node* newnode = new Node();
  13. newnode->reg=reg;
  14. newnode->cg=cg;
  15. newnode->next = root;
  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.  
  29.  
  30.  
  31. void Print(Node* root) {
  32. Node* curr = root;
  33. while (curr != NULL) {
  34. cout <<"reg:"<< curr->reg<<"cg:"<<curr->cg << " "<<endl;
  35. curr = curr->next;
  36. }
  37. cout << endl;
  38. }
  39.  
  40. int main() {
  41. Node* root = NULL;
  42.  
  43.  
  44. root = InsertAtBegin(root,66,4.00);
  45. root = InsertAtBegin(root, 33,3.90);
  46. root = InsertAtBegin(root,45,3.80);
  47. Print(root);
  48.  
  49.  
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
reg:45cg:3.8 
reg:33cg:3.9 
reg:66cg:4