Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Question
Book Icon
Chapter 21.3, Problem 21.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.)
In Python:Two stacks S1 and S2 can be implemented using one array. Write procedures: PopS1(Stack), PushS1(Stack,x), PopS2 (Stack), PushS2(Stack,x). The procedures should not declare an overflow unless every slot in the array is used.
Suppose you have a stack with a maximum size of 1000 elements and you want to perform the following operations: Push 500 elements onto the stack. Pop 200 elements from the stack. Push 800 elements onto the stack. Pop all remaining elements from the stack. What is the final size of the stack after performing all these operations?
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education