Q1  java : If in the ArrayStack, the first element that we push to the stack, is stored in location capacity – 1 (capacity is the capacity of the array theData) in the array theData and the subsequent elements that we push, we keep on adding before the last element added in the array. That is, in the constructor of class ArrayStack, we initialize the topOfStack to capacity and we write the push method as follows: public E push(E obj) {        if (topOfStack == 0)              reallocate();        topOfStack--;        theData[topOfStack] = obj;        return obj;  }   This push method can work but the if statement should be:   if (topOfStack == theData.length - 1) reallocate();   This push method will work provided we modify the reallocate method.   This push method can work, but topOfStack-- ; should come after  theData[topOfStack] = obj;   This push method can work, but topOfStack-- ; should be topOfStack++;

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question
100%

Q1 

java :

If in the ArrayStack, the first element that we push to the stack, is stored in location capacity – 1 (capacity is the capacity of the array theData) in the array theData and the subsequent elements that we push, we keep on adding before the last element added in the array. That is, in the constructor of class ArrayStack, we initialize the topOfStack to capacity and we write the push method as follows:

public E push(E obj)

{

       if (topOfStack == 0)

             reallocate();

       topOfStack--;

       theData[topOfStack] = obj;

       return obj;

 }

 
  1. This push method can work but the if statement should be:

      if (topOfStack == theData.length - 1) reallocate();

     
  2. This push method will work provided we modify the reallocate method.

     
  3. This push method can work, but topOfStack-- ; should come after

     theData[topOfStack] = obj;

     
  4. This push method can work, but topOfStack-- ; should be topOfStack++;

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning