Advertisement
ariestamirra

stack

May 25th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package Tugas_2;
  2.  
  3. /**
  4.  *
  5.  * @author Mirra Ariesta
  6.  */
  7. public class Stack {
  8.  
  9.     char[] stack;
  10.     int stackSize, size = 0;
  11.  
  12.     public boolean isFull() {
  13.         return stackSize == size;
  14.     }
  15.  
  16.     public boolean isEmpty() {
  17.         return size == 0;
  18.     }
  19.  
  20.     public Stack(int stackSize) {
  21.         this.stackSize = stackSize;
  22.         stack = new char[stackSize];
  23.     }
  24.  
  25.     public int getTop() {
  26.         return size - 1;
  27.     }
  28.  
  29.     public int getSize() {
  30.         return size;
  31.     }
  32.  
  33.     public String allItem() {
  34.         String result = "";
  35.         for (int i = 0; i < size; i++) {
  36.             result += ((i + 1) + "." + stack[i] + "\n");
  37.         }
  38.         return result;
  39.     }
  40.  
  41.     public Object pop() {
  42.         if (isEmpty()) {
  43.             throw new EmptyStackException();
  44.         } else {
  45.             Object temp;
  46.             temp = stack[size - 1];
  47.             stack[size - 1] = 0;
  48.             size--;
  49.             return temp;
  50.         }
  51.     }
  52.  
  53.     public void push(char item) {
  54.         if (isFull()) {
  55.             throw new FullStackException();
  56.         } else {
  57.             stack[size] = item;
  58.             size++;
  59.  
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement