Saturday, 27 August 2011

stackusing Arr


#include<iostream.h>
#include<conio.h>
class StackUsingArr
{private: int s[10];
 int top;
 public:
      StackUsingArr();
      void push(int item);
      int pop();
      int peak();
      void display();
}x;

StackUsingArr::StackUsingArr() { top=-1;}

void StackUsingArr::push(int item)
{ if (top==9) cout<<"\n Stack is Full\n"<<endl;
  else
    { top++;
      s[top]=item;
    }
}

int StackUsingArr::pop()
{int n=-9999;
 if (top == -1) cout<<"The Stack is Empty\n";
 else
 {n= s[top];
 top--;}
 return n;
}

int StackUsingArr::peak()
{ int n=-9999;
  if (top ==-1) cout<<"\n The stack is Empty\n";
  else n=s[top];
  return n;
}

void StackUsingArr::display()
{ for(int i=0;i<=top;i++)
  {cout.width(5);
   cout<<s[i];
  }
  cout<< " <-- top "<<endl;
}

void main()
{
 clrscr();
 x.push(8);
 x.push(7);
 x.push(5);
 x.push(2);
 cout<<"\nThe elements of the stack\n";
 x.display();
 int m=x.peak();
 cout<<"\nThe topmost element is"<<m<<endl;
 cout<<"\n After calling peak the stack is\n";
 x.display();
 m=x.pop();
 cout<<"\nAfter deleting a topmost element "<<m<<"  the stack is..\n";
 x.display();
 getch();
}

No comments:

Post a Comment