Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to create a stack using static memory allocation

Posted By: Easy Tutor     Category: C++ Programming     Views: 6510

Write a program to create a stack using static memory allocation

Code for Program to create a stack using static memory allocation in C++ Programming

# include <iostream.h>
# include <conio.h>
# include <process.h>

class stack
{
 int arr[50];
 int top;
 public:
    int get_top();
    stack()
    {
     top=-1;
    }
    void push(int);
    int pop();
    void peep();
};
int display_menu();
int stack :: get_top()
{
 return(top);
}
void stack :: push(int no)
{
  arr[top+1]=no;
  top=top+1;
}

int stack :: pop()
{
  int no;
  if(top<0)
  {
   return(NULL);
  }
  else
  {
  no=arr[top];
  top=top-1;
  return(no);
  }
}

void stack :: peep()
{
  if(top>=0)
  {
    for(int i=top;i>=0;i--)
    {
        cout<<"Stack ["<<i<<"] :"<<arr[i]<<endl;
    }
  }







  else
  {
     cout<<"Empty Stack !!!"<<endl;
  }
}

void main()
{
 stack s1;
 while(1)
 {
  switch(display_menu())
  {
   case 1: cout<<"Enter Number to push :";
       int no;
       cin>>no;
       s1.push(no);
       s1.peep();
       getch();
       break;
   case 2:
       no=s1.pop();
       if(no!=NULL)
       {
        cout<<"Stack ["<<s1.get_top()+1<<"] :"<<no;
       }
       else
       {
        cout<<"Empty Stack !!!"<<endl;
       }
       getch();
       break;
   case 3: s1.peep();
       getch();
       break;
   case 4: exit(1);

  }
 }
}

int display_menu()
{
 clrscr();
 int ch;
 cout<<endl;
 cout<<"\t\t\t| 1 | : PUSH"<<endl;
 cout<<"\t\t\t| 2 | : POP"<<endl;
 cout<<"\t\t\t| 3 | : PEEP"<<endl;
 cout<<"\t\t\t| 4 | : Exit"<<endl;
 cout<<"\t\t\tEnter Your Choice :";
 cin>>ch;
 return(ch);
}
  
Share: 


Didn't find what you were looking for? Find more on Program to create a stack using static memory allocation Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to create a stack using static memory allocation is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!