StacksQueues

StacksQueues - A stack is a data structure in which access...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Stacks Stacks A stack is a data structure in which access is  only allowed from one end. Imagine the candy Pez dispensers  You insert and remove candy from the same end.   The first piece in is the last piece out. You have a stack of books - to get to the  bottom, you have to remove the ones on top. This is called LIFO - Last-in/First-out
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stacks Stacks A stack adds items only to the front (top)  of the list and removes them from the front  (top). It is called a  LIFO  (Last-In, First-Out) data  structure.  Stacks are often drawn vertically. Stack class is in java.util package, it extends Vector  class
Background image of page 2
Sample of Stacks Sample of Stacks
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A Stack ADT A Stack ADT Some operations in a stack: Object push(Object item)  – adds an item to the top of the stack, and returns it Object pop( )  –  removes an item from the top of the stack and returns it Object peek( )  – retrieves the top item without removing it boolean empty()  – returns true if the stack is empty, false otherwise int size()  –  returns the number of elements A stack can be used to reverse a string check matching parentheses By a Compiler to check for balancing symbols
Background image of page 4
import java.util.Stack; public class StackTester2 { public static void main(String[] args)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

StacksQueues - A stack is a data structure in which access...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online