FahimFaisal

ListStack

Mar 8th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package stackPractice;
  2.  
  3. /**
  4.  *
  5.  * @author acer
  6.  */
  7. public class ListStack implements Stack {
  8.     // The number of items on the stack
  9.     int size;
  10.     Node top;
  11.     int capacity;
  12.    
  13.     public ListStack(){
  14.        top=null;
  15.        size=0;
  16.        
  17.     }
  18.    
  19.     public int size(){
  20.         int count=0;
  21.         for(Node n=top;n!=null;n=n.next){
  22.             count++;
  23.         }
  24.         return count;
  25.     }
  26. // Returns true if the stack is empty
  27.     public boolean isEmpty(){
  28.         if(top==null){
  29.             return true;
  30.         }else{
  31.             return false;
  32.         }
  33.     }
  34. // Pushes the new item on the stack, throwing the
  35. // StackOverflowException if the stack is at maximum capacity. It
  36. // does not throw an exception for an "unbounded" stack, which
  37. // dynamically adjusts capacity as needed.
  38.     public void push(Object e) throws StackOverflowException{
  39.         Node mn=new Node(e,null);
  40.         mn.next=top;
  41.         top=mn;
  42.         size++;
  43.     }
  44. // Pops the item on the top of the stack, throwing the
  45. // StackUnderflowException if the stack is empty.
  46.     public Object pop() throws StackUnderflowException{
  47.         Node rn=top;
  48.         Object val=rn.element;
  49.         top=top.next;
  50.         rn.element=null;
  51.         rn.next=null;
  52.         size--;
  53.         return val;
  54.        
  55.     }
  56. // Peeks at the item on the top of the stack, throwing
  57. // StackUnderflowException if the stack is empty.
  58.     public Object peek() throws StackUnderflowException{
  59.         if(top==null)
  60.             throw new StackUnderflowException();
  61.         return top.element;
  62.     }
  63. // Returns a textual representation of items on the stack, in the
  64. // format "[ x y z ]", where x and z are items on top and bottom
  65. // of the stack respectively.
  66.     public String toString(){
  67.         String str="[";
  68.         for(Node n=top;n!=null;n=n.next){
  69.             if(n.next==null){
  70.                 str+=n.element+"]";
  71.             }
  72.             else{
  73.                 str+=n.element+",";
  74.             }
  75.         }
  76.         return str;
  77.     }
  78. // Returns an array with items on the stack, with the item on top
  79. // of the stack in the first slot, and bottom in the last slot.
  80.     public Object[] toArray(){
  81.         Object [] arr=new Object[size];
  82.         int i=0;
  83.         for(Node n=top;n!=null;n=n.next){
  84.             arr[i]=n.element;
  85.             i++;
  86.         }
  87.         return arr;
  88.     }
  89. // Searches for the given item on the stack, returning the
  90. // offset from top of the stack if item is found, or -1 otherwise.
  91.     public int search(Object e){
  92.         int idx=-1;
  93.         int count=0;
  94.         for(Node n=top;n!=null;n=n.next){
  95.            if(e.equals(n.element)){
  96.                idx=count;
  97.            }
  98.            count++;
  99.         }
  100.         return idx;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment