I'm facing weird problem that the only 1st element of the list is get printed. I have writing linked list program after long time. thanks for the help. Is there something wrong with printAll function or add function in list class. I have tried printing previous elements while adding new one & it works. So, I'm not getting why only 1st element .ie. head is getting printed & head->next seems to be null.
#include<iostream>
using namespace std;
class Node{
public: int data;
public: Node *next;
public: Node(int data){
this->data = data;
this->next = NULL;
}
};
class List{
Node *head, *trav;
public: List(){
this->head = NULL;
this->trav = NULL;
};
void add(int data){
if(this->head==NULL && this->trav==NULL){
cout<<"inside the if block"<<endl;
this->head = new Node(data);
this->trav = this->head->next;
}
else{
cout <<"inside the else block"<<endl;
this->trav = new Node(data);
this->trav = this->trav->next;
}
}
void printAll(){
this->trav = this->head;
while(this->trav!=NULL){
cout<<this->trav->data<<endl;
this->trav = this->trav->next;
}
}
};
int main(){
List list;
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.printAll();
cout<<sizeof(list);
}