Posted by MindBreaker at 8:52 AM
Read our previous post
Object:
Source code:
#include <iostream>
using namespace std;
const int MAX=5;
class stack
{
int top;
int stack_arr[MAX];
public:
stack()
{
top=-1;
}
void push()
{
int pitem;////////////// push item
if(top == (MAX-1))
cout<<"Stack Overflow\n";
else
{
cout<<"Enter the item to be pushed in stack : ";
cin>>pitem;
top=top+1;
stack_arr[top] = pitem;
}
}/*End of push()*/
void pop()
{
if(top == -1)
cout<<"Stack Underflow\n";
else
{
cout<<"Popped element is : \n"<<stack_arr[top];
top=top-1;
}
}/*End of pop()*/
void display()
{
int chk;
if(top == -1)
cout<<"Stack is empty\n";
else
{
cout<<"Stack elements :\n";
for(chk = top; chk >=0; chk--)
cout<<stack_arr[chk];
}
}///////////////////////End of display()*/
};
void main()
{
char press;
stack skt;
int choice;
do
{
cout<<"Enter 1 for push"<<endl;
cout<<"Enter 2 for pop"<<endl;
cout<<"Enter 3 for Display Stack\n\n"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1 :
skt.push();
break;
case 2:
skt.pop();
break;
case 3:
skt.display();
break;
default:
cout<<"Worng Choice"<<endl;
}
cout<<"Continue press y"<<endl;
cin>>press;
}while(press=='y');
}
Output:
In computer science, stack is a lifo (last in first out)
data structure. It can store data of any type as an element. There are 2 basic
operations that can be performed on stack. The push function adds a new item to
the top of the stack. Or initializing the stack if it is empty; but if the
stack is full and does not contain more space to accept the new item it is
considered as an overflow state. The pop operation removes an item from the top
of the stack. A pop either reveals previously concealed item or results in an
empty stack but if the stack is empty, then it goes into an underflow state.
Implement the class stack that models the said data structure with the ability
to store 10 integer elements. Also add an exception class that stack overflow
and underflow triggers the exception reporting the function that caused the
exception.
Source code:
#include <iostream>
using namespace std;
const int MAX=5;
class stack
{
int top;
int stack_arr[MAX];
public:
stack()
{
top=-1;
}
void push()
{
int pitem;////////////// push item
if(top == (MAX-1))
cout<<"Stack Overflow\n";
else
{
cout<<"Enter the item to be pushed in stack : ";
cin>>pitem;
top=top+1;
stack_arr[top] = pitem;
}
}/*End of push()*/
void pop()
{
if(top == -1)
cout<<"Stack Underflow\n";
else
{
cout<<"Popped element is : \n"<<stack_arr[top];
top=top-1;
}
}/*End of pop()*/
void display()
{
int chk;
if(top == -1)
cout<<"Stack is empty\n";
else
{
cout<<"Stack elements :\n";
for(chk = top; chk >=0; chk--)
cout<<stack_arr[chk];
}
}///////////////////////End of display()*/
};
void main()
{
char press;
stack skt;
int choice;
do
{
cout<<"Enter 1 for push"<<endl;
cout<<"Enter 2 for pop"<<endl;
cout<<"Enter 3 for Display Stack\n\n"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1 :
skt.push();
break;
case 2:
skt.pop();
break;
case 3:
skt.display();
break;
default:
cout<<"Worng Choice"<<endl;
}
cout<<"Continue press y"<<endl;
cin>>press;
}while(press=='y');
}
Output:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.