0
#include <iostream>
using namespace std;

struct Node{
    int data;
    Node* next=NULL;
};
class list{
    Node *head,*tail;
    public:
        void insert(int data){
            Node *node =new Node;
            node->data=data;
            node->next=NULL;
            if(head==NULL){
                head=node;
                tail=node;
            }else{
                tail->next=node;
                tail=tail->next;
            }
        }
        void show(){
            Node *n=head;
            while(n->next!=NULL){
                cout<<n->data<<" ";
                n=n->next;
            }
            cout<<n->data<<endl;
        }
};

int main(){
    list x;
    int n;
    for(int i=0;i<10;i++){
        cin>>n;
        x.insert(n);
    }
    x.show();

    return 0;
}

the program is compiling perfectly but while running it stops and its not working if i put the insert function in loop only then the problem arises but otherwise it is running fine

Souvik De
  • 58
  • 1
  • 8

1 Answers1

1

When you declare a pointer, it doesn't have a default value of NULL necessarily.

related question

Initialize head and tail with NULL and the program will be run successfully.

mrazimi
  • 337
  • 1
  • 3
  • 12