stackFall10

stackFall10 - Stacks (Stack class in java.util package, it...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: Stacks (Stack class in java.util package, it extends Vector class) A stack adds items only to the top of the list and removes them from the top. It is called a LIFO (Last-In, First-Out) data structure. Stacks are often drawn vertically. 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 pop push //Example import java.util.Stack; public class StackTester2 { public static void main(String args) { Stack test = new Stack(); test.push("A"); test.push("B"); test.push("C"); test.push("D"); System.out.println(test.toString()); System.out.println(test.peek()); System.out.println(test.toString()); System.out.println(test.pop());System....
View Full Document

Page1 / 6

stackFall10 - Stacks (Stack class in java.util package, it...

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

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