Advertisement
Guest User

Stack

a guest
Oct 22nd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. package hw4;
  2.  
  3. import com.sun.org.apache.xpath.internal.SourceTree;
  4.  
  5. public class Stack {
  6.     Node[] arr; // regular array
  7.     int capacity;
  8.     int size;
  9.  
  10.     public Stack(int cap){
  11.         capacity = cap;
  12.         size = 0;
  13.         arr = new Node[cap];
  14.     }
  15.    
  16.     public void push(Node node){
  17.         if (!isFull()){
  18.             arr[size++] = node;
  19.         }else{
  20.             System.out.println("Stack Overflow!!!");
  21.         }
  22.     }
  23.     public Node pop(){
  24.         if (!isEmpty()){
  25.             return arr[--size];
  26.         }else{
  27.             System.out.println("Stack Overflow!!!");
  28.         }
  29.         return null; // fix this (out of place)
  30.     }
  31.     public boolean isFull(){
  32.         return size == capacity; // fix this
  33.     }
  34.     public boolean isEmpty(){
  35.         return size==0; // fix this
  36.     }
  37.    
  38.     public void printStack(){
  39.         if (!isEmpty()) {
  40.             System.out.print("[Bottom] ");
  41.             for(int i = 0; i<size; i++)
  42.                 System.out.print(arr[i].data+" ");
  43.             System.out.println("[Top]");
  44.         } else {
  45.             System.out.println("Empty Stack!!!");
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement