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

I want this question to solve in java. The array implementation in Introduction

ID: 3802379 • Letter: I

Question

I want this question to solve in java.

The array implementation in Introduction to Collections – Stacks, keeps the top variable pointing to the next array position above the actual top of the stack. Rewrite the array implementation such that stack[top] is the actual top of the stack. There is a data structure called a dropout stack that behaves like a stack in every respect, except that if the stack size is n, when the n+1 element is pushed, the first element is lost.  Implement a dropout stack using an array.

Thanks

Explanation / Answer

//Main method is written to test the drop out stack

public class DropOutStack {

   int stack[];

   int size;

   int top;

  

   public DropOutStack(int s)

   {

       this.size = s;

       stack = new int[size];

       top=-1;

   }

   public DropOutStack()

   {

       this.size=10;    //Default size

       stack = new int[size];

       top=-1;

   }

  

   public void push(int num)

   {

       if(top+1==size)

           reOrder();

       stack[++top] = num;

   }

  

   public void reOrder()

   {

       for(int i=0;i<top;i++)

           stack[i] = stack[i+1];

      

       --top;

   }

  

   public void pop()

   {

       --top;

   }

  

   public void display()

   {

       for(int i=0;i<=top;i++)

           System.out.print(stack[i]+" ");

      

       System.out.println();

   }

  

   public static void main(String[] args)

   {

       DropOutStack stack = new DropOutStack(5);

       stack.push(1);

       stack.display();

       stack.push(2);

       stack.push(3);

       stack.push(4);

       stack.display();

       stack.push(5);

       stack.push(6);

       stack.display();

       stack.pop();

       stack.display();

   }

  

}

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