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.3, Problem 20.5CP
Program Plan Intro

Stack:

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

“Push()” method:

The “Push()” is a method used to insert or add an item to the stack. The element is pushed at the top of the stack.

  • The “Push()” will have an item as its argument and it returns the argument which is passed.
  • If the stack is full and when “push()” method is calls the push will generate “StackOverFlowException” and return.

Syntax for push operation:

STACK.push(E element)

“Pop()” method:

  • The method “pop()” is used to delete an item from the stack.
  • This method does not have any argument and when this method called on a stack, the element at the top of the stack is removed.
  • This method will return the top element before removing it.
  • If the stack is empty (its size is equal to 0) and when the “pop()” method is called pop throws the “EmptyStackException” object.

Syntax for pop operation:

  STACK.pop()

Blurred answer
Students have asked these similar questions
A data structure known as a drop-out stack functions exactly like a stack, with the exception that if the stack size is n, the first element is lost when the n + 1 element is pushed. Use an array to implement a drop-out stack. (Hint: It would make sense to implement a circular array.)
A group of objects that have been piled atop one another is known as a stack. Which kinds of applications make use of stacks are there?
A drop-out stack is a data structure that acts just like a stack except that if the stack size is n, and the n + 1 element is pushed, the first element is lost. Using an array, create a drop-out stack. (Hint: A circular array implementation would be appropriate.)
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning