/* stack using link list */
#include<iostream>
using namespace std;
class node
{
private:
int data;
node *next;
public:
void push();
void pop();
void show();
};
node *head=NULL;
node *tail=NULL;
node *current=NULL;
node *temp=NULL;
int num;
int size=0;
int main()
{
node n;
string x;
while(1)
{
cout<<"\n\n1 to Push"<<endl;
cout<<"\n2 to Pop"<<endl;
cout<<"\n3 to Display"<<endl;
cout<<"\n4 to exit: ";
cin>>x;
if(x=="1")
{
n.push();
}
else if(x=="2")
{
n.pop();
}
else if(x=="3")
{
n.show();
}
else if(x=="4")
{
return 0;
}
else
{
cout<<"\nInvalid Command\n"<<endl;
}
}
}
//push Function
void node::push()
{
cout<<"\nEnter value: ";
cin>>num;
temp=new node;
temp->data=num;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
current=temp;
}
else
{
tail->next=temp;
tail=temp;
current=temp;
}
size++;
cout<<"\n"<<num<<": Pushed Successfully\n"<<endl;
}
//push Function
//POP function
void node::pop()
{
if(size==0)
{
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"\nList is Empty\n"<<endl;
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
}
else
{
current=head;
for(int i=1;i<size;i++)
{
temp=current;
current=current->next;
}
num=current->data;
if(size==1)
{
delete current;
head=NULL;
current=NULL;
tail=NULL;
temp=NULL;
}
if(size>1)
{
delete current;
temp->next=NULL;
tail=temp;
current=temp;
}
size--;
cout<<"\n"<<num<<": POPED Successfully\n"<<endl;
}
}
//POP function
//Display function
void node::show()
{
if(size==0)
{
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"\nList is Empty\n"<<endl;
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
}
else
{
current=head;
cout<<"\n**********************\n"<<endl;
cout<<"Total Nodes: "<<size<<"\n"<<endl;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<"\n\n**********************\n"<<endl;
}
}
//Display function
#include<iostream>
using namespace std;
class node
{
private:
int data;
node *next;
public:
void push();
void pop();
void show();
};
node *head=NULL;
node *tail=NULL;
node *current=NULL;
node *temp=NULL;
int num;
int size=0;
int main()
{
node n;
string x;
while(1)
{
cout<<"\n\n1 to Push"<<endl;
cout<<"\n2 to Pop"<<endl;
cout<<"\n3 to Display"<<endl;
cout<<"\n4 to exit: ";
cin>>x;
if(x=="1")
{
n.push();
}
else if(x=="2")
{
n.pop();
}
else if(x=="3")
{
n.show();
}
else if(x=="4")
{
return 0;
}
else
{
cout<<"\nInvalid Command\n"<<endl;
}
}
}
//push Function
void node::push()
{
cout<<"\nEnter value: ";
cin>>num;
temp=new node;
temp->data=num;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
current=temp;
}
else
{
tail->next=temp;
tail=temp;
current=temp;
}
size++;
cout<<"\n"<<num<<": Pushed Successfully\n"<<endl;
}
//push Function
//POP function
void node::pop()
{
if(size==0)
{
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"\nList is Empty\n"<<endl;
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
}
else
{
current=head;
for(int i=1;i<size;i++)
{
temp=current;
current=current->next;
}
num=current->data;
if(size==1)
{
delete current;
head=NULL;
current=NULL;
tail=NULL;
temp=NULL;
}
if(size>1)
{
delete current;
temp->next=NULL;
tail=temp;
current=temp;
}
size--;
cout<<"\n"<<num<<": POPED Successfully\n"<<endl;
}
}
//POP function
//Display function
void node::show()
{
if(size==0)
{
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"\nList is Empty\n"<<endl;
cout<<"\n^^^^^^^^^^^^^^^^^^^^^^"<<endl;
}
else
{
current=head;
cout<<"\n**********************\n"<<endl;
cout<<"Total Nodes: "<<size<<"\n"<<endl;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<"\n\n**********************\n"<<endl;
}
}
//Display function
Comments
Post a Comment