ydornberg

GenericStack

Oct 15th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. public class GenericStack<T> {    
  2.     private T S[];
  3.     private int t; // top index in array
  4.    
  5.     public static final int CAPACITY = 100;  
  6.     @SuppressWarnings("unchecked")
  7.     public GenericStack () {
  8.         S = (T[]) new Object[CAPACITY];
  9.         t = -1;
  10.     }
  11.     public void push (T x) {
  12.         if(size()==100){
  13.             System.out.println( "Stack is full." );
  14.         }
  15.         else{
  16.             t = t + 1;
  17.             S[t] = x;
  18.         }
  19.     }    
  20.     public T top () {
  21.         if(isEmpty()){
  22.             System.out.println("Stack is empty.");
  23.         }
  24.         return S[t];
  25.     }
  26.     public void pop () {
  27.         if( !isEmpty()){
  28.             t = t-1;
  29.         }
  30.         else{
  31.             System.out.println ( "Stack is empty." );
  32.         }
  33.     }
  34.     public boolean isEmpty () {
  35.         if(size() == 0)
  36.             return true;
  37.         else
  38.             return false;
  39.     }
  40.     public int size(){
  41.         return t+1;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment