Monday, October 19, 2009

Code Snippet to implement stack in Java

class MyStack 
{
private:
 int arr[5];
 int top;

public:
 MyStack()
 {
  int i;
  for(i=0;i<5;i++)
  {
   arr[i]=0;
  }
  top=0;
 }

 void push(int val)
 {
  if(full())
   System.out.println("Stack is Full.........\n");

  this.arr[top]=val;
  this.top=this.top+1;
 }

 int pop()
 {
  if(empty())
   System.out.println("Stack is Empty.........\n");
  this.top=this.top-1;
  return arr.[top];
 }

 boolean empty()
 {
  if(this.top==0)
   return true;
  else
   return false;
 }

 boolean full()
 {
  if(this.top==5)
   return true;
  else
   return false;
 }

 
}
class DemoStack
{
 public static void main(String[] args) 
 {
  
 }
}

No comments:

Post a Comment