Tuesday, 23 September 2014

Stack Using Array

$$$         STACK IMPLEMENTATION USING ARRAY              $$$

PROGRAM:

#include<iostream>
#include<stdlib.h>
#define size 5
using namespace std;
int i,s[size],top=-1,item;
class stack
{
public:
void push()
{
                if(top==size-1)
                {
                                cout<<"Stack is full";
                }
                else
                {
                                cout<<"Enter data:";
                                cin>>item;
                                top=top+1;
                                s[top]=item;
                }
}
void pop()
{
                if(top==-1)
                {
                                cout<<"Stack is empty";
                }
                else
                {
                                cout<<"poped element is:"<<s[top];
                                top--;
                }
}
void display()
{
                if(top==-1)
                {
                                cout<<"Stack is empty";
                }
                else
                {
                                cout<<"The elements are:\n";
                                for(i=top;i>=0;i--)
                                {
                                                cout<<s[i];
                                                cout<<"\n";
                                }
                }
}
};

void main()
{
                stack s;
                cout<<"$$$ STACK IMPLEMENTATION USING ARRAY $$$\n";
                while(1)
                {
                                int n;
                                cout<<"\n\n1.Push\n2.Pop\n3.Display\n4.Exit\n";
                                cout<<"Enter the choice:";
                                cin>>n;
                                switch(n)
                                {
                                                case 1:s.push();break;
                                                case 2:s.pop();break;
                                                case 3:s.display();break;
                                                case 4:exit(0);
                                                default:cout<<"Enter an valid option";break;
                                }
                }
}


OUTPUT:

$$$ STACK IMPLEMENTATION USING ARRAY $$$

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be pushed: 11

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be pushed: 22

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be pushed: 33

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      3
Content of the stack is:
33
22
11

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      2
The deleted element is: 33

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      3
Content of the stack is:
22
11

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      4

No comments:

Post a Comment