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

\"There is a data structure called a drop-out stack that behaves like a stack in

ID: 3695213 • Letter: #

Question

"There is a data structure called a drop-out stack that behaves like a stack in every way except that the stack has a maximum capacity n. If the stack contains n elements, when the n+1 element is pushed, the bottom element is lost. For example, if the stack has a maximum capacity of 3, and contains the values (1, 2, 3), with 3 being the top element, then pushing a 4 would give the stack (2, 3, 4), with 4 being the top element. Implement a drop-out stack using an array. Follow the StackADT interface. See Base_A05Q2.java for a starting place."

this push method needs to be altered to accomplish this but i'm stuck.

public void push(T element)

{

if (size() == stack.length)

expandCapacity();

  

stack[top] = element;

top++;

}

Explanation / Answer

I have executed it under NetBeans IDE

DropOutStack Implementation:

package dropoutstack;
import java.util.*;

/* Class arrayStack */
class DropOutStack
{
protected int arr[],darr[];
protected int top, size, len,last=0;
/* Constructor for arrayStack */
public DropOutStack(int n)
{
size = n;
len = 0;
arr = new int[size];
darr = new int[size];
top = -1;
}
/* Function to check if stack is empty */
public boolean isEmpty()
{
return top == -1;
}
/* Function to check if stack is full */
public boolean isFull()
{
return top == size -1 ;
}
/* Function to get the size of the stack */
public int getSize()
{
return len ;
}
/* Function to check the top element of the stack */
public int peek()
{
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return arr[top];
}
/* Function to add an element to the stack */
public void push(int i)
{
  
if(top + 1 >= size){
last=i;
  
for(int j=0;j<size;j++)
{
darr[j]=pop();
}

top=-1;
for(int j=0;j<size-1;j++)
{
push(darr[j]);
}
push(i);
  
}
if(top + 1 < size )
arr[++top] = i;
len++ ;
}
/* Function to delete an element from the stack */
public int pop()
{
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
len-- ;
return arr[top--];
}
/* Function to display the status of the stack */
public void display()
{
System.out.print(" Stack = ");
if (len == 0)
{
System.out.print("Empty ");
return ;
}
for (int i = top; i >= 0; i--)
System.out.print(arr[i]+" ");
System.out.println();
}
}
Main class:

package dropoutstack;

import java.util.Scanner;
public class StackImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Stack Test ");
System.out.println("Enter Size of Integer Stack ");
int n = scan.nextInt();
/* Creating object of class arrayStack */
DropOutStack stk = new DropOutStack(n);
/* Perform Stack Operations */
char ch;
do{
System.out.println(" Stack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to push");
try
{
stk.push( scan.nextInt() );
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}   
break;   
case 2 :
try
{
System.out.println("Popped Element = " + stk.pop());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;   
case 3 :   
try
{
System.out.println("Peek Element = " + stk.peek());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;   
case 4 :
System.out.println("Empty status = " + stk.isEmpty());
break;
case 5 :
System.out.println("Full status = " + stk.isFull());
break;   
case 6 :
System.out.println("Size = " + stk.getSize());
break;   
default :
System.out.println("Wrong Entry ");
break;
}
/* display stack */
stk.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);

} while (ch == 'Y'|| ch == 'y');   
}
}