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

A) Modify the specification shown below for the Stack class supposing you were r

ID: 3779227 • Letter: A

Question

A) Modify the specification shown below for the Stack class supposing you were required to use an array rather than using the Node class. Note: you do not have to provide new bodies for the methods, so only the following code needs to change:

   private class Node {

         private D data;

         private Node next;   

         public Node (D value, Node link) {          

         }

   }

      private Node top;

B) In a sentence or two, describe how the push() method would need to change to support this array-based implementation.

Explanation / Answer

For implementing a stack using array, you have to keep track of the top and if stack is full. Since size of array is fixed so you can't push after the stack is full. The Stack class will be like - class Stack{    private int capacity; // maximum capacity of array private D[] storageArray; // array to store elements private int top // index of the top element of stack, for empty stack it will be -1; } To support array implementation the push() method the changes need to be done are - check if stack is full that is size() = capacity or top==capacity-1. if stack is not full insert new element at top+1 and increase the value of top by one there are other ways to know the size, isEmpty, or full properties of stack. Code - public void push(D element){ if(top==capacity-1){ System.out.println("Stack is full.");    }else{ storageArray[top+1] = element;    top ++ ;    } }

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