Question: How do I use the expand method to increment the size of the array whenever the stack is full in the push method in this

How do I use the expand method to increment the size of the array whenever the stack is full in the push method in this class?

import java.util.*; public class GenericStack { private T[ ] data; private int size; private int top; private MainStack[] s; GenericStack stack = new GenericStack(); public GenericStack() { this.size = 3; top = -1; size = 3; data = (T[ ]) new Object[3]; } public int getSize() { return this.size; } public int getNoOfElement() { return this.top + 1; } public GenericStack(int n) { top = -1; size = n; data = (T[ ]) new Object[n]; } public boolean push(T newNode) { GenericNode node = (GenericNode) newNode; if(top == size-1) return false; // ** overflow error ** else { top = top +1; data[top] = (T) node.deepCopy(); return true; // push operation successful } } public T pop( ) { int topLocation; if(top == -1) return null; // ** underflow error ** else { topLocation = top; top = top -1; return data[topLocation]; } } public void showAll( ) { for(int i = top; i >= 0; i--) System.out.println(data[i].toString()); } public void reset( ) { System.out.println("Before the stack is reset:" + stack); stack.reset(); System.out.println("After the stack is reset:" + stack); } public T peek( ) { return stack.peek(); } boolean isEmpty( ) { if (top == -1) { return true ; } else return false; } boolean isFull( ) { if(top == (data.length - 1)) { return true; } else return false; } private boolean expand() { int s=this.size; T [] d=this.data; this.size*=2; this.data=(T[]) new Object[this.size]; for(int i=0;i                                            

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!