Coding Lab - TechOnTechnology

Program in C++ to demonstrate Stack Using Arrays.





#include<stdio.h>
#include<iostream.h>
#include<conio.h>
class Stack
{
int top;
int arr[50];
public:
    Stack(){top=-1;}
    void push();
    void pop();
    void view();
    int isEmpty();
    int isFull();
};
int Stack::isEmpty(){return (top==(-1)?1:0);}
int Stack::isFull(){return ( top == 50 ? 1 : 0 );}
void Stack::push()
{
    if(isFull())
    {cout<<"\nSTACK IS FULL { OVERFLOW }";}
    else
    {
        int i;
        cout<<"\nEnter an element :\n";
        cin>>i;++top;arr[top]=i;
        cout<<"\nInsertion successfull.";
    }
}
void Stack::pop()
{
    int num;
    if(isEmpty()){cout<<"\n STACK IS EMPTY [ UNDERFLOW ] ";}
    else
    {
    cout<<"Deleted item is : "<<arr[top];top--;
    }
}
void Stack::view()
{
    if(isEmpty()){cout<<"\n STACK IS EMPTY [ UNDERFLOW ] ";}
    else
    {
    cout<<"\nSTACK :\n";
    for(int i=top;i>=0;i--){cout<<arr[i]<<"\n";}
    }
}
void main()
{
Stack s;
int ch;
clrscr();
ch=0;
while(ch!=3)
{
cout<<"\nMENU : \n";
cout<<"0. PUSH\n";
cout<<"1. POP\n";
cout<<"2. VIEW\n";
cout<<"3. QUIT\n";
cin>>ch;
switch(ch)
{
case 0:s.push();break;
case 1:s.pop();break;
case 2:s.view();break;
case 3:ch=3;cout<<"Press any key .. ";break;
default:cout<<"INVALID CHOICE  !! \n";break;
}
}
getch();
}

Name

Email *

Message *