fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct Node
  8. {
  9. int data;
  10. struct Node* next;
  11. };
  12.  
  13. Node* Insert(Node* head, int data);
  14. Node* print(Node* head);
  15. void ReverseIterative();
  16.  
  17.  
  18. Node* Insert(Node* head, int data)
  19. {
  20. Node* newNode = new Node();
  21. newNode->data = data;
  22. newNode->next = NULL;
  23.  
  24. if(head == NULL)
  25. {
  26. return newNode;
  27. }
  28.  
  29. Node* curr=head;
  30. while(curr->next!=NULL)
  31. {
  32. curr=curr->next;
  33. }
  34. curr->next = newNode;
  35. return head;
  36. }
  37.  
  38. Node* printList(Node* head)
  39. {
  40. while(head)
  41. {
  42. cout<<head->data;
  43. head=head->next;
  44. }
  45. cout<<endl;
  46. }
  47.  
  48. int main()
  49. {
  50. struct Node* head = NULL;
  51. head = Insert(head, 2);
  52. head = Insert(head, 4);
  53. printList(head);
  54. return 0;
  55. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
24