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