Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the implementation of the class CircularStack. Read all the directives.

ID: 3823360 • Letter: C

Question

Complete the implementation of the class CircularStack. Read all the directives. In particular notice that this stack implementation uses a circular array. public interface Stack E Adds an element onto the top of this stack public abstract void push (E element Removes and returns the top element of the stack public abstract E pop throws java util Empty Stack ception Returns true if and only if this stack is empty public abstract boolean isEmpty This implementation uses a fixed-size circular array; When the stack is full, the method push replaces the bottom element with the new element to be added Therefore, the method push can always add new elements to the stack, even when the stack is full, but the oldest elements, those at the bottom of the stack, are lost If n is the capacity of the stack, then this stack memorises a maximum of n elements, the last ones added to the stack The constructor has a single parameter, which defines the capacity of the stack; The null value is a valid value for this stack.

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.EmptyStackException;

public class CircularStack<E> implements Stack<E> {

  

   private E[] elems;

   private int top, size;

  

   public CircularStack(int capacity) {

      

       elems = (E []) new Object[capacity];

       size = 0;

       top = -1;

   }

   @Override

   public void push(E elem) {

       top = (top+1)%elems.length;

       elems[top] = elem;

      

       if(size < elems.length){

           size++;

       }

      

   }

   @Override

   public E pop() throws EmptyStackException {

       if(isEmpty()){

           throw new EmptyStackException();

       }

      

       E saved = elems[top];

      

       elems[top] = null;

      

       if(size == 1){

           top = -1;

           size = 0;

       }else{

           top--;

           size--;

          

           if(top == -1){

               top = elems.length - 1;

           }

       }

      

       return saved;

   }

   @Override

   public boolean isEmpty() {

       return size == 0;

   }

}

####################

public class CircularStackTest {

  

   public static void main(String[] args) {

      

       CircularStack<Integer> stack = new CircularStack<>(5);

      

       stack.push(3);

       stack.push(4);

       stack.push(5);

       stack.push(6);

      

       System.out.println("Pop: "+stack.pop());

       stack.push(7);

       stack.push(8);

       stack.push(9);

       System.out.println("Pop: "+stack.pop());

       stack.push(10);

       stack.push(11);

       System.out.println("Pop: "+stack.pop());

       System.out.println("Pop: "+stack.pop());

       System.out.println("Pop: "+stack.pop());

       System.out.println("Pop: "+stack.pop());

       System.out.println("Pop: "+stack.pop());

   }

}

/*

Sample run:

Pop: 6

Pop: 9

Pop: 11

Pop: 10

Pop: 8

Pop: 7

Pop: 5

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote