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

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

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

View Full Document Right 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
Image of page 2
1-3 Stacks Top of stack Adding an element Removing an element
Image of page 3

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

View Full Document Right 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
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(); }
Image of page 5

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

View Full Document Right 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] **/
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
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
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