SHARE
TWEET

Stack Codez

a guest Sep 17th, 2019 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Lab Stack
  2.  
  3. public class MyStack
  4. {
  5.    private int maxSize;
  6.    private Object[] stackArray;
  7.    private int top;
  8.    public MyStack(int s)
  9.    {
  10.       maxSize = s;
  11.       stackArray = new Object[maxSize];
  12.       top = -1;
  13.    }
  14.    
  15.    public void push(Object j)
  16.    {
  17.       stackArray[++top] = j;
  18.    }
  19.    
  20.    public Object pop()
  21.    {
  22.       return stackArray[top--];
  23.    }
  24.    
  25.    public Object peek()
  26.    {
  27.       return stackArray[top];
  28.    }
  29.    
  30.    public boolean empty()
  31.    {
  32.       return (top == -1);
  33.    }
  34.    
  35.    public boolean isFull()
  36.    {
  37.       return (top == maxSize - 1);
  38.    }
  39.    
  40.    public Object search(int i)
  41.    {
  42.        return stackArray[i];
  43.     }
  44.    
  45.    public static void main(String[] args)
  46.    {
  47.       MyStack theStack = new MyStack(10);
  48.       theStack.push(10);
  49.       theStack.push(20);
  50.       theStack.push(30);
  51.       theStack.push(40);
  52.       theStack.push(50);
  53.       while (!theStack.empty())
  54.       {
  55.          Object value = theStack.pop();
  56.          System.out.print(value);
  57.          System.out.print(" ");
  58.       }
  59.       System.out.println("");
  60.       System.out.println("Find obj on index 2:" +theStack.search(2));
  61.    }
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top