Advertisement
Guest User

Stk.java

a guest
Sep 11th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. public class Stk implements IStk {
  2.  
  3.     public final int MAXSIZE =100;
  4.     private int top;
  5.     private int bot;
  6.     private int front;
  7.     private int rear;
  8.     private int maxStk;
  9.     private char items [];
  10.  
  11.     public Stk ()
  12.     {
  13.         this.front=0;
  14.         this.rear=0;
  15.         this.maxStk=0;
  16.         this.items = new char [MAXSIZE];
  17.     }
  18.  
  19.     public Stk(int max)
  20.     {
  21.         this.maxStk = max+1;
  22.         this.front = maxStk - 1;
  23.         this.rear = maxStk - 1;
  24.         this.items = new char [max];
  25.     }
  26.  
  27.     public boolean isFull ()
  28.     {
  29.         // WRAP AROUND
  30.         return ( (this.top + 1) % this.maxStk == this.bot );
  31.     }
  32.  
  33.     public boolean isEmpty ()
  34.     {
  35.         return ( this.bot == this.top );
  36.     }
  37.  
  38.     public void push (char item)
  39.     {
  40.         if (!isFull())
  41.         {  this.top = (this.top+1) % this.maxStk;
  42.             this.items[top] = item;
  43.         }
  44.         else
  45.             System.out.println ("Tried to insert into full stack.");
  46.     }
  47.  
  48.     public char pop ()
  49.     {
  50.         char item;
  51.         if (!isEmpty())
  52.         {
  53.             item = this.items[top];
  54.             this.top = (this.top-1) % this.maxStk;
  55.             return item;
  56.         }
  57.         else {
  58.             System.out.println ("Tried to remove from empty stack.");
  59.             return 0;
  60.         }
  61.  
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement