Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Tugas_2;
- /**
- *
- * @author Mirra Ariesta
- */
- public class Stack {
- char[] stack;
- int stackSize, size = 0;
- public boolean isFull() {
- return stackSize == size;
- }
- public boolean isEmpty() {
- return size == 0;
- }
- public Stack(int stackSize) {
- this.stackSize = stackSize;
- stack = new char[stackSize];
- }
- public int getTop() {
- return size - 1;
- }
- public int getSize() {
- return size;
- }
- public String allItem() {
- String result = "";
- for (int i = 0; i < size; i++) {
- result += ((i + 1) + "." + stack[i] + "\n");
- }
- return result;
- }
- public Object pop() {
- if (isEmpty()) {
- throw new EmptyStackException();
- } else {
- Object temp;
- temp = stack[size - 1];
- stack[size - 1] = 0;
- size--;
- return temp;
- }
- }
- public void push(char item) {
- if (isFull()) {
- throw new FullStackException();
- } else {
- stack[size] = item;
- size++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement