Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Question
Book Icon
Chapter 20, Problem 2AW
Program Plan Intro

Stack:

  • A stack is a linear data structure.
  • It follows the order FILO (first in last out) LIFO or (last-in-first-out) for operations on it.
  • The two main operations performed on a stack are,
    • Push() – Add item to stack.
    • Pop() – Remove item from stack.

Blurred answer
Students have asked these similar questions
IN JAVA Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows:   The first new method checks whether we should reduce the size of the array: private boolean isTooBig()   This method returns true if the number of entries in the stack is less than half the size of the array and the size of the array is greater than 20.   The second new method creates a new array that is three quarters the size of the current array and then copies the objects in the bag to the new array: private void reduceArray()   Implement each of these two methods, and then use them in the definition of pop()
Java Consider the following class definition for an array-based stack implementation:pubic class Stack {   private int[] m_array;   private int m_index;      public Stack(int cap) {      m_array = new int[cap];      m_index = 0;   }    public void push(int v) {      if (m_index == m_array.length)         throw new RuntimeException("push attempted on a full stack");      else {         m_array[m_index] = v;         m_index++;      }   }} Follow the steps below to create a class SpecialStack with required instance variables and methods.a.    Make sure that the SpecialStack class inherits from Stack. b.    Declare two private instance variables: a boolean variable m_multiply, and an int variable m_number.c.    Create a constructor that takes three parameters: an int cap, a boolean multiply, and an int number.d.    The constructor should call the constructor of the super class and initialize the instance variables properly.e.    Override the push method so it verifies if m_multiply is…
In Java please create a generic stack code and implement these methods peek() returns a reference to the item at the top of the stack without removing it from the stack contains(T t) is a boolean method that returns true if the stack contains t, the parameter, otherwise returns false. The stack contains t if at least one T on the stack is equal to t using the equals() method.  Note that there is always an available equals() method in Java, because Object, the common parent of all classes, contains one.  Implement this method without using the contains()method of the list.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning