Wednesday 12 October 2011

IMPLEMENTATION OF STACK USING EXCEPTION HANDLING


IMPLEMENTATION OF STACK USING EXCEPTION HANDLING
AIM:
To implement a Stack using exception handling using C++.

ALGORITHM:
Step 1: Start the process.
Step 2: Create a Class Stack.
Step 3: Initialize the top pointer of the stack.
Step 4: Create class full and empty.
Step 5: Create the members push, pop and display.
Step 6: Inside the push function check the top of the stack for overflow. If the top is not equal to the size (n-1) then push the elements onto the stack and increment the top pointer. If the top reaches the maximum size of n then throw the elements on to the stack overflow.
Step 7: Inside the pop function check the stack underflow condition. If the stack is empty, top is equal to zero then deletion cannot be performed, throws a stack underflow exception. Otherwise pop the elements and decrement the pointer.
Step 8: Display the stack elements.
Step 9: Stop the process.

PROGRAM:
#include<iostream.h>
#include<iomanip.h>
class stack
{
private:
int *s; int max; int top;
public:
class full{};
class empty{};
stack(int);
void push(int);
int pop(void);void display(void);
};
stack::stack(int m)
{
s=new int[m];
top = -1;
max=m;
}
void stack::push(int item)
{
if(top<max-1)
s[++top]=item;
else throw full();
}
int stack::pop(void)
{
if(top>=0)
return s[top--];
else
throw empty();
}
void stack::display(void)
{
if(top>=0)
for(int i = top; i>=0;i--)
cout<<"\n \t |"<<setw(4)<<s[i]<<"|\n\t-----";
else
throw empty();
}
int main()
{
int item,size;
int ch = 1;
cout<<"\n Enter the size of the Stack:";
cin>>size;
stack s1(size);
cout<<"\n Stack with exception handling";
cout<<"\n \n MENU \n 1.PUSH \n 2.POP \n3.SHOW THE STACK \n4.EXIT";
cout<<"\n Enter your choice:";
cin>>ch;
do
{
switch(ch)
{
case 1:
cout<<"\n Enter the item to push:";
cin>>item;
try
{
s1.push(item);
}
catch(stack::full)
{
cout<<"\n Stack Overflow";
}
break;
case 2:
try
{
cout<<"The Popped element is:";
cout<<s1.pop();
}
catch(stack::empty)
{
cout<<"\n Stack is empty";
}
break;
case 3:
cout<<"The stack is....";
try
{
s1.display();
}
catch(stack::empty)
{
{
cout<<"\n Stack empty";
}
break;
case 4:
exit(0);
}
cout<<"\n Enter your choice:\t";
cin>>ch;
}while(ch<5);
return 0;
}

OUTPUT:
[ex@localhost ~]$ vi stack.cpp
[ex@localhost ~]$ g++ stack.cpp
[ex@localhost ~]$ ./a.out
 Enter the size of the Stack:2
 Stack with exception handling
 MENU
 1.PUSH
 2.POP
3.SHOW THE STACK
4.EXIT
 Enter your choice:1
 Enter the item to push:12
 Enter your choice:     1
 Enter the item to push:34
 Enter your choice:     1
 Enter the item to push:23
 Stack Overflow
 Enter your choice:     2
The Popped element is:34
 Enter your choice:     3
The stack is....
         |  12|
         -----
Enter your choice:     4

RESULT:
Thus the program to implement the stack using exception handling was executed.

2 comments:

Shady said...

the program cant be copied please do something

Unknown said...
This comment has been removed by the author.

Post a Comment