0

in display function: both 0 and NULL giving the same answer when using in 'while loop' why?

when printing temp it shows 0 after completion of while loop that's why I use 0 as termination condition but everyone is using NULL as termination condition

Please anyone can explain ?

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

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

    Node(int val){
        data=val;
        next=NULL;
    }
};

void insert_at_first(Node* &head,int val){
    Node* n=new Node(val);
    if (head==NULL){
        head=n;
        return;
    }
    Node* temp=head;
    while(temp->next != NULL){
        temp=temp->next;
    }
    temp->next=n;
}

void display(Node* head){
    Node* temp=head;
    while(temp!=0){
        cout<<temp->data<<"->";

        temp=temp->next;
    }

    cout<<"NULL"<<endl;
}

int main(){
    Node* head=NULL;
    insert_at_first(head,4);
    display(head);
    insert_at_first(head,2);
    display(head);
    insert_at_first(head,8);
    display(head);
}

0 Answers0