#include<bits/stdc++.h>
using namespace std;
///------------ CREATE NODE CLASS---------
class Node{
public:
int data;
Node *next;
Node(int x=0){
this->data = x;
this->next = nullptr; /// NULL
}
};
///------------ CREATE LINKED LIST CLASS ---------
class LinkedList{
private:
Node *HEAD;
public:
LinkedList(){
HEAD = nullptr;
}
/// insert a new element at the first position of the linked list
void insertAtFirst(int x){
Node *newNode = new Node(x);
newNode->next = HEAD;
HEAD = newNode;
}
/// display the elements in the linked list
void display(){
if(HEAD==nullptr){
cout<<"The list is empty"<<endl;
return;
}
Node *currentNode = HEAD;
while(currentNode!=nullptr){
cout<< currentNode->data <<"->";
currentNode = currentNode->next;
}
cout<< "NULL" << endl;
}
///-------Insert a new node as the last node---------
void insertAtLast( int x ) {
Node *newNode = new Node (x); /// create new node
if (HEAD==nullptr) {
HEAD = newNode ;
return;
}
Node*currentNode = HEAD ;
while (currentNode->next != nullptr) {
currentNode = currentNode->next;
}
currentNode->next = newNode;
}
};
///-------------- MAIN FUNCTION ------------
int main()
{
LinkedList myList;
myList.insertAtLast(30);
myList.insertAtLast(50);
myList.insertAtLast(100);
myList.display();
return 0;
}