Question: Modify the code of Figure 4.18 to implement two stacks sharing the same array, as shown in Figure 4.20 . Figure 4.18 Figure 4.20 /**

Modify the code of Figure 4.18 to implement two stacks sharing the same array, as shown in Figure 4.20 .

Figure 4.18

/** Array-based stack implementation */ class AStack implements Stack { private static final int defaultSize

Figure 4.20

top1 top2

/** Array-based stack implementation */ class AStack implements Stack { private static final int defaultSize = 10; private int maxSize; private int top; private E [] listArray; // Constructors AStack() { this (defaultSize); } @SuppressWarnings ("unchecked") // Generic array allocation AStack (int size) { } public void clear() { top = 0; } public void push (E it) { assert top != maxSize : listArray [top++] = it; } maxSize = size; top = 0; listArray = (E[]) new Object [size]; // Create listArray public E pop() { assert top != 0 "Stack return listArray [--top]; } // Maximum size of stack // Index for top Object // Array holding stack } // Reinitialize stack // Push "it" onto stack "Stack is full"; // Pop top element is empty"; public E topValue () { assert top != 0: "Stack is empty"; return listArray [top-1]; // Return top element public int length() { return top; } // Return length Figure 4.18 Array-based stack class implementation.

Step by Step Solution

3.36 Rating (149 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

java Arraybased stack implementation class AStack implements Stack private static final int defaultS... View full answer

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 Practical Introduction To Data Structures Questions!