Tuesday 24 April 2012

1 st Experiment OPERATION ON STACK ADT(abstract)


                                                                Algorithm for STACK ADT:
--------------------------------------------------------------------------------------------------------------------------------------

Step-1: Start
Step-2: Create an object for the class stack.
Step-3: do the following:
a)      Print the options
b)     read the choice
c)      i) If case is ‘push’, the value to be pushed and use the object of the class to call the member function push() and display()
ii) If case is ‘pop’, use the object to call member function pop() and display().
iii) If case is ‘quit’, display ‘bye’.
Step-4: while the case is not quit, repeat the step-3.
Step-5: Stop.

--------------------------------------------------------------------------------------------------------------------------------------
                                                               FLOW CHARTs
-------------------------------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
-------------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#define max 50
class stack
{
private:
int sarr[max];
int top;
public:
stack()
{
top=-1;
}
void push(int i)
{
if(top==max-1)
{
cout<<"\n Stack is full"<<endl;
getch();
}
top++;
sarr[top]=i;
}
int pop()
{
int d;
if(top==-1)
{
cout<<"\n Stack is empty"<<endl;
getch();
return NULL;
}
else
{
d=sarr[top];
top--;
}
return d;
}
void display()
{
cout<<endl<<"\nThe elements in the stack\n"<<endl;
for(int i=top;i>=0;i--)
{
cout<<sarr[i]<<endl;
}
}
};
void main()
{
clrscr();
stack s1;
int ch,item;
do
{
cout<<"1. Push"<<endl;
cout<<"2. Pop"<<endl;
cout<<"3. exit"<<endl;
cout<<"\n Enter the choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Enter the value:";
cin>>item;
s1.push(item);
s1.display();
getch();
break;
case 2:
item=s1.pop();
cout<<"\n Popped item="<<item;
s1.display();
getch();
break;
case 3:
cout<<"\n Bye";
getch();
break;
}}
while(ch!=3);
}

OUTPUT:

1.Push
2.Pop
3.Exit
Enter your choice:
1
Enter the element: 1
The elements in the stack
1

1.Push
2.Pop
3.Exit
Enter your choice:
1
Enter the element: 2
The elements in the stack
2
1

1.Push
2.Pop
3.Exit
Enter your choice:
2
Popped item=2
The elements in the stack
1

1.Push
2.Pop
3.Exit
Enter your choice:
3
Bye




No comments:

Post a Comment