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

Please draw the dynamic array stack structure (you must mention the size and cap

ID: 3606362 • Letter: P

Question

Please draw the dynamic array stack structure (you must mention the size and capacity at each step) after the following commands 1-9 are executed. You should assume that a resize doubles the capacity.


1. struct dynArr *stack = createDynArray(2);

2. pushDynArray (&stack, 2);

3. popDynArray (&stack);

4. pushDynArray (&stack, 4);

5. pushDynArray (&stack, 5);

6. pushDynArray (&stack, 11);

7. pushDynArray (&stack, 13);

8.  pushDynArray (&stack, 2);

9. popDynArray (&stack);

You can type the answer in  

[ ] [ ]          size = , capacity =

Explanation / Answer

public class StackResizeArray { T[] content; int size; public static void main(String[] args) { StackResizeArray stack = new StackResizeArray(); stack.push(1); stack.push(2); stack.push(3); System.out.println(stack.toString()); try { stack.pop(); stack.pop(); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } System.out.println(stack.toString()); } public StackResizeArray(){ this.content = (T[]) new Object[1]; this.size = 0; } /* * This is a method which resizes the capacity of the array of the stack * @author Gong Li * @param capacity the expected size of the array in this stack * */ private void resize(int capacity){ T[] copy = (T[]) new Object[capacity]; for (int i = 0; i 0 && size == content.length / 4) resize(content.length / 2); return item; } @Override public String toString(){ StringBuffer sb = new StringBuffer(); for (int i = 0; i