Pages

Monday, 8 November 2021

Program to illustrate Stack using Arrays in c++

 /*  stack array implementation using class and object in c++ */

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
#define max 20
int i,n,top=-1,a[max],item; /* global declaration*/

class rkstack
{
public:
int push(void); /* push function declaration */
int pop(void);  /* pop function declaration */
int display(void); /* display function declaration */
};


int rkstack::push() /* implementation function */
{
    if(top>=n-1)
    {
      cout<<"\n Stack overflow";
      return top;
    }
    cout<<"\n Enter the item to be added: ";
    cin>>item;
    top++;
    a[top]=item;
    return top;
 }

int rkstack::pop()
 {
    if(top<0)
    {
     cout<<"\n stack underflow";
     return top;
    }
    cout<<"\n The poped item from stack is "<<a[top];
    --top;
    return top;
}

int rkstack::display()
 {
    if(top<0)
     {
     cout<<"\n Stack is empty " ;
     return top;
     }
    cout<<"stack elements are:\n";
    for(i=top;i>=0;i--)
     cout<<a[i]<< "   ";
   cout<<"\n";
   return top;
 }

void main()
{
  int ch;
  rkstack s;
  clrscr();
  cout<<"\nEnter stack size :";
  cin>>n;
  while(1)
  {
    cout<<"\n****  MENU *** \n\n";
    cout<<"\n 1.PUSH  \n";
    cout<<"\n 2.POP  \n";
    cout<<"\n 3.DISPLAY  \n";
    cout<<"\n 4.QUIT  \n";
    cout<<"\n ENTER UR CHOICE: ";
    cin>>ch;
    switch(ch)
    {
       case 1:s.push();break;
       case 2:s.pop();break;
       case 3:s.display();break;
       case 4:exit(1);break;
       default:cout<<"u have entered wrong choice";break;
    }
  }
}

Output: 

/* stack array implementation with out using class and object in c++ */
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
#define max 20
int push(void); /* push function declaration */
int pop(void);  /* pop function declaration */
int display(void); /* display function declaration */
int i,n,top=-1,a[max],item; /* global declaration*/

void main()
{  
  int ch;
  clrscr();
  cout<<"\nEnter stack size :";
  cin>>n;
  while(1)
  {
    cout<<"\n****  MENU *** \n\n";
    cout<<"\n 1.PUSH  \n";
    cout<<"\n 2.POP  \n";
    cout<<"\n 3.DISPLAY  \n";
    cout<<"\n 4.QUIT  \n";
    cout<<"\n ENTER UR CHOICE: ";
    cin>>ch;
    switch(ch)
    {
       case 1:push();break;
       case 2:pop();break;
       case 3:display();break;
       case 4:exit(1);break;
       default:cout<<"u have entered wrong choice";break;
    }
  }
}
int push() /* implementation function */
{
    if(top>=n-1)
    {
      cout<<"\n Stack overflow";
      return top;
    }
    cout<<"\n Enter the item to be added: ";
    cin>>item;
    top++;
    a[top]=item;
    return top;
 }
 
int pop()
 {
    if(top<0)
    {
     cout<<"\n stack underflow";
     return top;
    }
    cout<<"\n The poped item from stack is "<<a[top];
    --top;
    return top;
}

int display()
 {
    if(top<0)
     {
     cout<<"\n Stack is empty " ;
     return top;
     }
    cout<<"stack elements are:\n";
    for(i=top;i>=0;i--)
     cout<<a[i]<< "   ";
   cout<<"\n";
   return top;
 }



ANIMATION VIDEO LINK OF STACK :
https://www.youtube.com/watch?v=-KQpk-dIA8s

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...