Lab02Answer

advertisement
CS120 Lab 2 Answers
Due by Mon, February 23, 2014
1. Create a class to implement the data structure STACK
Write a program to implement the data structure STACK as class and write a constructor to initialize
the TOP of the STACK to 0. Write a member function PUSH () to insert an element and member
function POP () to delete an element and LIST () to display all elements in the STACK. Check for
overflow and under flow condition.
#include <iostream>
#include<conio.h>
#include <Process.h>
using namespace std;
class STACK
{
int tos,s[10],i;
public:
STACK();
void PUSH();
void POP();
void LIST();
};
STACK::STACK()
{
tos=0;
}
void STACK::PUSH()
{
if(tos>=10)
cout<<"The stack is full \n ";
else
{
cout <<"Enter the value
";
cin>>s[tos];
tos ++;
}
}
void STACK :: POP()
{
if (tos <=0)
{
cout <<"stack is empty";
}
else
{
cout <<"The deleting number is
"<<s[tos-1]<<"\n";
tos--;
}
}
void STACK :: LIST()
{
if (tos <=0)
{
cout <<"stack is empty\n";
}
else
{
cout<<" The listed elements are"<<"\n";
for (i=0; i<tos; i++)
{
cout<<s[i]<<"\n";
}
}
}
void main()
{
char ch ;
int n;
STACK O;
cout <<"stack operations\n";
cout <<"**************\n";
ch='y';
while((ch=='Y')||(ch=='y'))
{
cout<<"MENUS";
cout<<" 1.Push";
cout<<" 2.Pop";
cout <<" 3.List" ;
cout<< " 4.Exit\n";
cout << "Enter Your choice";
cin>>n;
switch(n)
{
case 1:
O.PUSH();
break;
case 2:
O.POP();
break ;
case 3:
O.LIST();
break;
case 4:
exit(0);
break;
}
cout<<"DO YOU WANT TO CONTINUE
cin>>ch;
}
cout<<"GOOD BY";
getch();
}
BEST WISHES ☺☺
NISREEN ALZAHRANI
Y/N";
Download