#include<iostream.h>
#include<conio.h>
#define maxSize 5
#include<stdlib.h>
class stack
{
public:
stack(); // constructor call the display function
void pop(); //used to pop the value as per user demand
void push(); //used for push the value as per user demand
int empty(); //used to check the stack
int full(); //used to check the stack whether it is full as max limit is 80
void display(); //used to display menu for operations
void stackDisplay(); //used to display whole stack items
void operation(); //used to enter the user choice
private:
int top;
int item[maxSize];
};
stack::stack()
{
top = 0;
display();
}
void stack :: display()
{
cout << "STACK MAIN MENU GIVEN BELOW" << endl;
cout << "PRESS 1 FOR PUSH THE ITEM ON STACK" << endl;
cout << "PRESS 2 FOR POP THE ITEM ON STACK" << endl;
cout << "PRESS 3 FOR DISPLAY THE WHOLE STACK" << endl;
cout << "PRESS 4 FOR EXIT THE PROGRAM" << endl;
cout<<"ENTER YOUR CHOICE" << endl;
operation();
}
void stack :: operation()
{
int choice;
cin >> choice;
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
stackDisplay();
break;
case 4:
exit(4);
default:
cout << "ERROR: PLEASE ENTER VALID NUMBER!" << endl;
operation();
}
}
int stack::empty()
{
if( top == 0 )
return 0;
else
return 1;
}
int stack::full()
{
if( top == maxSize )
return 0;
else
return 1;
}
void stack::push()
{
if( full() == 0 )
{
cout<<"STACK IS FULL "<< endl;
cin.get();
}
else
{
char ch;
int x;
top = top + 1;
cout<<"ENTER THE ELEMENT TO BE INSERTED" << endl;
cin>>x;
item[top] = x;
cout << "THE ITEM " << x << " HAS BEEN INSERTED" << endl;
cout<<"WANT TO ENTER ANOTHER ITEM ON STACK y/n "<< endl;
cin>>ch;
if(ch=='y')
push();
cin.get();
}
clrscr();
cout<<"PRESS ENTER FOR MAIN MENU " << endl;
cin.get();
clrscr();
display();
}
void stack::pop()
{
if(empty() == 0)
{
cout<<"THE STACK IS EMPTY " << endl;
cin.get();
}
else
{
char ch;
cout<<"AS THE LAST ELEMENT IN STACK IS "<<item[top] << endl;
top = top-1;
cout<<"WANT TO POP OUT ANOTHER ELEMENT (y/n)" << endl;
cin>>ch;
if(ch=='y')
pop();
cin.get();
}
cout<<"PRESS ENTER TO GO TO MAIN MENU " << endl;
cin.get();
clrscr();
display();
}
void stack::stackDisplay()
{
cout<<"THE ELEMENTS IN STACK IS"<<endl;
for(int i = 1;i <= top; i++)
{
cout << item[i] << endl;
}
cin.get();
cout<<"PRESS ENTER FOR MAIN MENU " << endl;
cin.get();
clrscr();
display();
}
void main()
{
clrscr();
gotoxy(15,10);
cout<<"**WELCOME TO THE PROGRAM OF STACK CREATED BY CHRISTOTECH**" << endl;
gotoxy(15,11);
cout<<"______________________________________________" << endl;
cin.get();
clrscr();
stack obj;
getch();
}
{
public:
stack(); // constructor call the display function
void pop(); //used to pop the value as per user demand
void push(); //used for push the value as per user demand
int empty(); //used to check the stack
int full(); //used to check the stack whether it is full as max limit is 80
void display(); //used to display menu for operations
void stackDisplay(); //used to display whole stack items
void operation(); //used to enter the user choice
private:
int top;
int item[maxSize];
};
stack::stack()
{
top = 0;
display();
}
void stack :: display()
{
cout << "STACK MAIN MENU GIVEN BELOW" << endl;
cout << "PRESS 1 FOR PUSH THE ITEM ON STACK" << endl;
cout << "PRESS 2 FOR POP THE ITEM ON STACK" << endl;
cout << "PRESS 3 FOR DISPLAY THE WHOLE STACK" << endl;
cout << "PRESS 4 FOR EXIT THE PROGRAM" << endl;
cout<<"ENTER YOUR CHOICE" << endl;
operation();
}
void stack :: operation()
{
int choice;
cin >> choice;
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
stackDisplay();
break;
case 4:
exit(4);
default:
cout << "ERROR: PLEASE ENTER VALID NUMBER!" << endl;
operation();
}
}
int stack::empty()
{
if( top == 0 )
return 0;
else
return 1;
}
int stack::full()
{
if( top == maxSize )
return 0;
else
return 1;
}
void stack::push()
{
if( full() == 0 )
{
cout<<"STACK IS FULL "<< endl;
cin.get();
}
else
{
char ch;
int x;
top = top + 1;
cout<<"ENTER THE ELEMENT TO BE INSERTED" << endl;
cin>>x;
item[top] = x;
cout << "THE ITEM " << x << " HAS BEEN INSERTED" << endl;
cout<<"WANT TO ENTER ANOTHER ITEM ON STACK y/n "<< endl;
cin>>ch;
if(ch=='y')
push();
cin.get();
}
clrscr();
cout<<"PRESS ENTER FOR MAIN MENU " << endl;
cin.get();
clrscr();
display();
}
void stack::pop()
{
if(empty() == 0)
{
cout<<"THE STACK IS EMPTY " << endl;
cin.get();
}
else
{
char ch;
cout<<"AS THE LAST ELEMENT IN STACK IS "<<item[top] << endl;
top = top-1;
cout<<"WANT TO POP OUT ANOTHER ELEMENT (y/n)" << endl;
cin>>ch;
if(ch=='y')
pop();
cin.get();
}
cout<<"PRESS ENTER TO GO TO MAIN MENU " << endl;
cin.get();
clrscr();
display();
}
void stack::stackDisplay()
{
cout<<"THE ELEMENTS IN STACK IS"<<endl;
for(int i = 1;i <= top; i++)
{
cout << item[i] << endl;
}
cin.get();
cout<<"PRESS ENTER FOR MAIN MENU " << endl;
cin.get();
clrscr();
display();
}
void main()
{
clrscr();
gotoxy(15,10);
cout<<"**WELCOME TO THE PROGRAM OF STACK CREATED BY CHRISTOTECH**" << endl;
gotoxy(15,11);
cout<<"______________________________________________" << endl;
cin.get();
clrscr();
stack obj;
getch();
}