{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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
Image of page 2
Sample of Stacks Sample of Stacks
Image of page 3

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

View Full Document Right 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
Image of page 4
import java.util.Stack; public class StackTester2 {
Image of page 5

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern