# Class 37: Introdution to Linear Structures

Back to Design, Revisited. On to Stacks.

Held Monday, April 12

Summary

• Linear structures
• Meaning
• Core operations
• Types of linear structures
• Three kinds of linear structures:
• Stacks
• Queues
• Priority queues
• Reverse polish notation
• Reading: Java Plus Data Structures, chapter 7.

Contents

Notes

• I was disappointed that so many of you missed class on Friday. Please do your best to make it to class. (We'll again have prospectives this Friday; come and make Grinnell look Good.)
• Today is the last day for sophomores to declare a major.
• Cliff and I have graded the exams.
• The average grade in our class was 84. The median was 88. (This means that there were more low outliers than high outliers.)
• Scores were high enough that I won't be discussing the exam in class.
• There were some errors on the answer key. We've done our best to correct those.
• I'm guest-teaching an afternoon class this week, and therefore won't be available afternoons.
• Guidelines for projects will be available tomorrow (I hope).
• Any questions on assignment 8?

## Linear Structures

• A problem with some kinds of lists is that they provide just too many options.
• In ordered lists, you might put at and retrieve elements from the front, back, or even middle of the list.
• Should you use simple lists, ordered lists, sorted lists, indexed lists, vectors, or what?
• This leads to some difficulties
• This can make implementation more difficult, as we've already seen.
• This can also make usage of lists more difficult, as a user needs to consider the interrelationship of the different functions.
• Many uses of lists can be simplified to four basic operations:
• Add an element to the list (or related structure).
• Remove an element from the list (or related structure).
• Peek at the next element to be removed.
• Check if there are any elements remaining.
• Structures that support these three operations are often called linear structures.
• A policy determines the relationship between objects being added and objects being removed.
• A first-in, first-out (FIFO) policy says that objects are removed in the order that they're put in.
• A last-in, first-out (LIFO) policy says that objects are removed in the opposite order that they're put in (so the object most recently added is the next one to be removed).
• Other policies might place priorities on the objects, or randomly select objects.

### Stacks

• Stacks are linear structures that use the LIFO policy.
• They are similar to the stacks of dishes that you may see in cafeterias. As each dish is cleaned, it is added to the top of the stack. As customers need dishes, they remove them from the top of the stack.
• They are also similar to the stacks used to implement function calls in typical languages.
• The standard names for stack operations are
• `push(Object o)` -- add an element to the top of the stack
• `Object pop()` -- remove an element from the top of the stack
• `Object peek()` -- look at the element on the top of the stack
• `boolean empty()` -- check if the stack is empty
• There are many uses for stacks, and we'll keep coming back to them as the term progresses. These uses include:
• Providing support for recursive procedure calls
• Searching structures
• Computation

### Queues

• Often, linear structures are used to organize tasks to be done (e.g., places in a maze to look at, potential solutions to a programming problem, ...).
• One problem with stacks is that the first things that are added to the stack are the last removed, so if we keep finding new things to try, we'll never try the first ones.
• To provide a sense of fairness, we might instead require that the earlier tasks are done first.
• Queues are first-in first-out (FIFO) linear structures. They are similar to lines at a bank teller or elsewhere.
• The main methods in queues are
• `enqueue(Object o)` -- add an object to the back of the queue
• `Object dequeue()` -- remove an object from the front of the queue
• `Object front()` -- determine what's at the front of the queue, but don't remove it.
• `boolean empty()` -- are there any elements in the queue?
• Like stacks, queues are used for a variety of purposes.
• Like stacks, queues can be implemented in multiple ways.

### Priority Queues

• We might further refine our sense of ``queue'' by adding a priority to the elements of the queue. Rather than using FIFO, we'll now use ``lowest/highest priority first''.
• Linear structures that use priority in choosing the next element are priority queues.
• Are priority queues queues?
• No, in the sense that many elements do not follow a FIFO ordering.
• Yes, in the sense that all elements of equal priority are expected to be returned in a FIFO ordering.
• It is likely the second observation that led to the naming.

### Doubly-Ended Queues

• In some odd cases, it may make sense to add elements to or remove elements from both the front and back of the queue.
• Queues that support this extension are called doubly-ended queues or simply dequeues (isn't that a wonderful name conflict with the operation?)
• In effect, these are a kind of ordered list, but without cursors.

History

• Created Monday, January 11, 1999
• Added short summary on Friday, January 22, 1999
• Filled in the details on Monday, April 12, 1999. Many of these details were based on outline 37 of CS152 98S (although they were rearranged and I added new sections on priority queues and implementation).
• On Tuesday, April 13, 1999, I moved the notes on implementation to the next outline.

Back to Design, Revisited. On to Stacks.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Outlines/outline.37.html

Source text last modified Tue Apr 13 09:20:29 1999.

This page generated on Wed Apr 14 09:30:54 1999 by SiteWeaver. Validate this page's HTML.

Contact our webmaster at rebelsky@math.grin.edu