fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Node{
  4. int val;
  5. Node*next;
  6. };
  7. Node*InsertAtEnd(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. Node*currnode;
  24. currnode=root;
  25. while(currnode!=NULL)
  26. {
  27. currnode=currnode->next;
  28. }
  29. currnode->next=newnode;
  30. return root;
  31. }
  32. void Print(Node*root)
  33. {
  34. Node*currnode;
  35. currnode=root;
  36. while(currnode!=NULL)
  37. {
  38. cout<<currnode->val<<" ";
  39. currnode=currnode->next;
  40. }
  41. cout<<endl;
  42. }
  43. int main()
  44. {
  45. Node*root=NULL;
  46. root=InsertAtEnd(root,6);
  47. root=InsertAtEnd(root,8);
  48. root=InsertAtEnd(root,1);
  49. Print(root);
  50. }
  51.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
1 8 6