// Online C++ compiler to run C++ program online
include
using namespace std;
class Node{
public:
int data;
Node * next;
Node(int d){
data = d;
next = nullptr;
}
};
class LinkedList{
private:
Node * head;
public:
void insertAB(int data){
Node * newNode = new Node(data);
newNode->next = head;
head = newNode;
return;
}
void display(){
Node * current = head;
while(current != nullptr){
cout<<current->data<<" -> ";
current = current -> next;
}
}
void count(){
int count = 0;
Node * current = head;
while(current != nullptr){
count++;
current = current->next;
}
cout<<"\n\ntotal Node :" <<count<<endl;
}
void clear(){
Node * current = head;
Node * temp;
while(current != nullptr){
temp = current;
current = current ->next;
delete temp;
}
head = nullptr;
}
};
int main() {
LinkedList list;
int x;
//running with for loop is throwing segementation fault, without it running fine.
// for(int i=0;i<10;i++){
// cout<<“enter data:”;
// cin>>x;
// list.insertAB(x);
// }
list.insertAB(100);
list.insertAB(1000);
list.display();
list.count();
list.clear();
list.count();
// list.display();
// list.count();
// list.clear();
return 0;
}