fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class node{
  5. public:
  6. int data;
  7. node* next;
  8.  
  9. node(int val){
  10. data = val;
  11. next = NULL;
  12. }
  13. };
  14.  
  15. void insertAtTail(node* &head, int val){
  16.  
  17.  
  18. node* n = new node(val);
  19.  
  20. if(head == NULL){
  21. head = n;
  22. return;
  23. }
  24. node* temp = head;
  25. while(temp->next != NULL){
  26. temp = temp->next;
  27. }
  28. temp->next = n;
  29.  
  30. }
  31.  
  32. void display(node* head){
  33. node* temp=head;
  34. while(temp!=NULL){
  35. cout<<temp->data<<"->";
  36. temp = temp->next;
  37. }
  38. cout<<"NULL"<<endl;
  39. }
  40.  
  41. int main() {
  42.  
  43. // your code goes here
  44. node* head=NULL;
  45. insertAtTail(head,1);
  46. insertAtTail(head,2);
  47. insertAtTail(head,3);
  48. insertAtTail(head,4);
  49. insertAtTail(head,5);
  50. display(head);
  51. return 0;
  52. }
Success #stdin #stdout 0.01s 5340KB
stdin
Standard input is empty
stdout
1->2->3->4->5->NULL