Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GenericStack<T> {
- private T S[];
- private int t; // top index in array
- public static final int CAPACITY = 100;
- @SuppressWarnings("unchecked")
- public GenericStack () {
- S = (T[]) new Object[CAPACITY];
- t = -1;
- }
- public void push (T x) {
- if(size()==100){
- System.out.println( "Stack is full." );
- }
- else{
- t = t + 1;
- S[t] = x;
- }
- }
- public T top () {
- if(isEmpty()){
- System.out.println("Stack is empty.");
- }
- return S[t];
- }
- public void pop () {
- if( !isEmpty()){
- t = t-1;
- }
- else{
- System.out.println ( "Stack is empty." );
- }
- }
- public boolean isEmpty () {
- if(size() == 0)
- return true;
- else
- return false;
- }
- public int size(){
- return t+1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment