Sunday, November 29, 2009

Source code for Stack using Array method

#include “iostream.h”
#include “process.h”
class stack
{
private : int *s, sp,max;
public :
void push (); // to push item into stack
void pop ( ); // to delete an item
void display ( ); // to display the contents of stack
stack (int); // constructor
int isfull();
int isempty();
};


//The following is the member function for the push operation.
void stack :: push ()
{if (isfull())
cout << “stack is full “ << endl;
else
{cout << ”enter element \n”;
cin >> s[sp++]; }
}
//The following is the member function for the pop operation
void stack :: pop ( )
{ if (isempty)
cout << "stack is empty";
else
cout << s [sp--];
}
int stack::isfull()
{
if (sp== max-1)
return 1;
else
return 0;
}
int stack::isempty()
{
if (sp == -1)
return 1;
else
return 0;
}
// the following is the display member function
void stack :: display ( )
{
for (int i=sp; i>0; i --)
cout << s[ i ] << endl;
}
stack :: stack ( int n)
{
sp=-1;
max = n;
s = new int[max];
}

void main ( )
{
int n;
cout << “enter size of stack “ << endl;
cin >> n;
stack s(n);
int choice;
do
{
cout << " 1. psuh\n 2. pop\n 3. Display/n 4. exit\n";
cout << "enter choice";
cin >> choice;
switch (choice)
{
case 1: s.push () ; break ;
case 2 : s.pop ( ); break;
case 3 : s.display ( ); break;
case 4 : exit(0);
}
} while (choice!=4) ;
}

0 Comments:

 
^ Scroll to Top