L15- Stacks-Queues

L15- Stacks-Queues - Outline Stacks Implementing Stacks...

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

View Full Document Right Arrow Icon
1-1 Outline Stacks Implementing Stacks with Arrays and Link List Queues Implementing Queues with Arrays and Link List Analysis of Stack and Queue Implementations
Background image of page 1

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

View Full DocumentRight Arrow Icon
1-2 Stacks A stack is a linear collection whose elements are added and removed from the same end Stacks are processed in a last in, first out (LIFO) manner Usually, stacks are depicted vertically, and we refer to the top of the stack as the end to which elements are added and removed
Background image of page 2
1-3 Stacks Top of stack Adding an element Removing an element
Background image of page 3

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

View Full DocumentRight Arrow Icon
1-4 Stack Operations Operation Description push Adds an element to the top of the stack pop Removes an element from the top of the stack peek Examines the element at the top of the stack isEmpty Determines if the stack is empty size Determines the number of elements on the stack
Background image of page 4
1-5 javafoundations.Stack //******************************************************************** // Stack.java Java Foundations // // Defines the interface to a stack collection. //******************************************************************** package javafoundations; public interface Stack<T> { // Adds the specified element to the top of the stack. public void push (T element); // Removes and returns the top element from the stack. public T pop(); // Returns a reference to the top element of this stack // without removing it. public T peek(); // Returns true if the stack contains no elements and false // otherwise. public boolean isEmpty(); // Returns the number of elements in the stack. public int size(); // Returns a string representation of the stack. public String toString(); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
1-6 // 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()); test.push("E"); System.out.println(test.toString()); test.push("F"); System.out.println(test.toString()); System.out.println(test.pop()); test.push("G"); System.out.println(test.toString()); System.out.println(test.peek()); System.out.println(test.toString()); } } /** output [A, B, C, D] D [A, B, C, D] D [A, B, C, E] [A, B, C, E, F] F [A, B, C, E, G] G [A, B, C, E, G] **/
Background image of page 6
1-7 Evaluating Postfix Expressions Arithmetic operations are traditionally written in infix notation, meaning that the operator is placed between its operands in the form < operand > < operator > < operand > Example: 4 + 5
Background image of page 7

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

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

This note was uploaded on 04/18/2011 for the course CSE 205 taught by Professor Matasu during the Spring '08 term at ASU.

Page1 / 29

L15- Stacks-Queues - Outline Stacks Implementing Stacks...

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

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