fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int val;
  5. Node*next;
  6. };
  7. Node*InsertAtBegin(Node*root,int x)
  8. {
  9. Node*newnode=new Node();
  10. newnode->next=NULL;
  11. newnode->val=x;
  12. if(root==NULL)
  13. {
  14. root=newnode;
  15. return root;
  16. }
  17. else
  18. {
  19. newnode->next=root;
  20. root=newnode;
  21. return root;
  22. }
  23. }
  24. void Print(Node*root)
  25. {
  26. Node*currnode;
  27. currnode=root;
  28. while(currnode!=NULL)
  29. {
  30. cout<<currnode->val<<" ";
  31. currnode=currnode->next;
  32. }
  33. cout<<endl;
  34. }
  35. int main()
  36. {
  37. Node*root=NULL;
  38. root=InsertAtBegin(root,3);
  39. root=InsertAtBegin(root,1);
  40. root=InsertAtBegin(root,9);
  41. Print(root);
  42. }
  43.  
  44.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
9 1 3