fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Node{
  5. public:
  6. int data;
  7. Node* next;
  8.  
  9. //Constructor
  10.  
  11. Node(int data){
  12. this -> data=data;
  13. this -> next=NULL;
  14. }
  15. };
  16.  
  17.  
  18. void insertAtHead(Node* &head, int d){
  19. Node* temp= new Node(d);
  20. temp->next=head;
  21. head=temp;
  22.  
  23. }
  24. void insertAtTail(Node* &tail,int d){
  25. Node *temp = new Node(d);
  26. tail->next = temp;
  27. tail=temp;
  28. }
  29.  
  30. void print(Node* &head){
  31. Node* temp=head;
  32.  
  33. while(temp!=NULL){
  34. cout<<temp->data<<" ";
  35. temp=temp->next;
  36.  
  37.  
  38. }
  39. cout<<endl;
  40.  
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47. int main(){
  48.  
  49. //created a new node
  50.  
  51. Node* node1 = new Node(10);
  52. // cout<< node1 -> data<<endl;
  53. // cout<< node1 -> next<<endl;
  54.  
  55. //head pointed to node1
  56.  
  57. Node* head = node1;
  58. Node *tail = node1;
  59. print(head);
  60.  
  61. // insertAtHead(head,12);
  62. insertAtTail(tail,12);
  63.  
  64. print(head);
  65.  
  66. // insertAtHead(head,25);
  67. insertAtTail(tail,15);
  68. print(head);
  69.  
  70. return 0;
  71.  
  72.  
  73. }
Success #stdin #stdout 0.02s 25568KB
stdin
Standard input is empty
stdout
#include<iostream>
using namespace std;

class Node{
    public:
    int data;
    Node* next;

    //Constructor

    Node(int data){
        this -> data=data;
        this -> next=NULL;
    }
};


void insertAtHead(Node* &head, int d){
        Node* temp= new Node(d);
        temp->next=head;
        head=temp;

}
void insertAtTail(Node* &tail,int d){
    Node *temp = new Node(d);
    tail->next = temp;
    tail=temp;
}

void print(Node* &head){
        Node* temp=head;

        while(temp!=NULL){
            cout<<temp->data<<" ";
            temp=temp->next;


        }
        cout<<endl;

}





int main(){

    //created a new node

    Node* node1 = new Node(10);
    // cout<< node1 -> data<<endl;
    // cout<< node1 -> next<<endl;

    //head pointed to node1

    Node* head = node1;
    Node *tail = node1;
    print(head);

    // insertAtHead(head,12);
    insertAtTail(tail,12);

    print(head);

    // insertAtHead(head,25);
    insertAtTail(tail,15);
    print(head);

    return 0;
    

}