This blog consists of C and C++ programs. C and C++ programming is the basics to learn any programming language. Most of the programs are provided with their respective outputs. This blog also contains Data Structures programs using Object-Oriented Programming (C++). Some of the best books for learning C and C++ programming are also mentioned.

Tuesday 13 August 2013

C++ program to implement Stack ADT using Array

#include<iostream.h>
#include<conio.h>
#define maxsize 10
class stack
{
private:
int a[maxsize];
int top;
public:
stack();
void push(int i);
void pop();
void display();
}
stack::stack()
{
top=0;
}
void stack::push(int i)
{
if(top==maxsize)
cout<<"\nStack is full";
else
{
top++;
a[top]=i;
}
}
void stack::pop()
{
if(top==0)
cout<<"\nStack is empty";
else
{
int i=a[top];
top--;
}
}
void stack::display()
{
if(top==0)
cout<<"Stack is empty";
else
{
cout<<"\nThe elements in the stack are: ";
for(int i=0; i<=top;i++)
cout<<a[i]<<'\t';
}
}
int main()
{
stack s;
int x,ch;
do
{
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the element to be pushed";
cin>>x;
s.push(x);
cout<<"\nThe element is inserted";
break;
case 2:
s.pop();
break;
case 3:
s.display();
case 4:
break;
}
}while(ch!=4);
getch();
}

4 comments: