appendix-q - Appendix Q Stacks and Queues(beta Copyright...

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

View Full Document Right Arrow Icon
Appendix Q Stacks and Queues (beta) Copyright © 2011 by Stuart Reges and Marty Stepp Q.1 Stack/Queue Basics o Stack Concepts o Queue Concepts Q.2 Creating and Filling Stacks and Queues o Randomly Filled Stacks/Queues o Transferring Between Stacks and Queues Q.3 Coding Example: Sum of a Stack/Queue's Elements o Sum of a Queue o Sum of a Stack Introduction In this chapter, we will explore two of the most fundamental collections in computer science, called stacks and queues . They are so simple that they almost seem not worth studying. They are like the programming equivalent of drawers and shelves. Drawers and shelves are very simple and, therefore, sort of boring, and yet we find uses for them everywhere we turn. It is useful to study stacks and queues as a way to understand a minimal kind of data structure. We'll find, for example, that they are less powerful than the list structures we have been looking at. But we often find ourselves wanting to think in terms of the simplest possible solution to a problem, as in, "You could solve that with a stack." Q.1 Stack/Queue Basics Like a list, a stack or queue stores an ordered sequence of values. A minimal set of operations for such a structure would require at least: some way to put values into the structure (an "add" operation) a way to take values out (a "remove" operation) a way to test whether there is anything left in the structure (is the structure empty?) These three operations are the bare bones that you'd need for such a structure and in their purest form, stacks and queues have just these three operations. Java's version of these also includes a size method that lets you ask for the number of elements in the structure.
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 and queues are similar in that they each store a sequence of values in a particular order. But stacks are what we call LIFO ("Last-In, First-Out") structures while queues are FIFO ("First-In, First-Out") structures: stacks queues L-ast F-irst I-n I-n F-irst F-irst O-ut O-ut Stack Concepts The analogy for stacks is to think of a cafeteria and how trays are stacked up. When you go to get a tray, you take the one on the top of the stack. You don't bother to try to get the one on the bottom, because you'd have to move a lot of trays to get to it. Similarly if someone brings clean trays to add to the stack, they are added on the top rather than on the bottom. The result is that stacks tend to reverse things. Each new value goes to the top of the stack, and when we take them back out, we draw from the top, so they come back out in reverse order. stack 1 bottom 2 3 top pop, peek push Figure 18.1: A stack In the case of a stack, the adding operation is called " push " and the removing operation is called " pop ". All operations occur at one end of the stack, at the top. We push values onto the top and we pop them off the top. There is also a method for testing whether the stack is empty and an operation for requesting the current size of the stack. The
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 12/17/2011.

Page1 / 10

appendix-q - Appendix Q Stacks and Queues(beta Copyright...

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