Advertisement
Guest User

Untitled

a guest
May 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. public class MyStackImpl {
  2.  
  3.     private int stackSize;
  4.     private int[] stackArr;
  5.     private int top;
  6.  
  7.     /**
  8.      * constructor to create stack with size
  9.      * @param size
  10.      */
  11.     public MyStackImpl(int size) {
  12.         this.stackSize = size;
  13.         this.stackArr = new int[stackSize];
  14.         this.top = -1;
  15.     }
  16.  
  17.     /**
  18.      * This method adds new entry to the top
  19.      * of the stack
  20.      * @param entry
  21.      * @throws Exception
  22.      */
  23.     public void push(int entry) throws Exception {
  24.         if(this.isStackFull()){
  25.             throw new Exception("Stack is already full. Can not add element.");
  26.         }
  27.         System.out.println("Adding: "+entry);
  28.         this.stackArr[++top] = entry;
  29.     }
  30.  
  31.     /**
  32.      * This method removes an entry from the
  33.      * top of the stack.
  34.      * @return
  35.      * @throws Exception
  36.      */
  37.     public int pop() throws Exception {
  38.         if(this.isStackEmpty()){
  39.             throw new Exception("Stack is empty. Can not remove element.");
  40.         }
  41.         int entry = this.stackArr[top--];
  42.         System.out.println("Removed entry: "+entry);
  43.         return entry;
  44.     }
  45.      
  46.     /**
  47.      * This method returns top of the stack
  48.      * without removing it.
  49.      * @return
  50.      */
  51.     public int peek() {
  52.         return stackArr[top];
  53.     }
  54.  
  55.     /**
  56.      * This method returns true if the stack is
  57.      * empty
  58.      * @return
  59.      */
  60.     public boolean isStackEmpty() {
  61.         return (top == -1);
  62.     }
  63.  
  64.     /**
  65.      * This method returns true if the stack is full
  66.      * @return
  67.      */
  68.     public boolean isStackFull() {
  69.         return (top == stackSize - 1);
  70.     }
  71.  
  72.     public static void main(String[] args) {
  73.         MyStackImpl stack = new MyStackImpl(5);
  74.         try {
  75.             stack.push(4);
  76.             stack.push(8);
  77.             stack.push(3);
  78.             stack.push(89);
  79.             stack.pop();
  80.             stack.push(34);
  81.             stack.push(45);
  82.             stack.push(78);
  83.         } catch (Exception e) {
  84.             System.out.println(e.getMessage());
  85.         }
  86.         try {
  87.             stack.pop();
  88.             stack.pop();
  89.             stack.pop();
  90.             stack.pop();
  91.             stack.pop();
  92.             stack.pop();
  93.         } catch (Exception e) {
  94.             System.out.println(e.getMessage());
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement