# Class 36: Introduction to Linear Structures

Back to Lists, Concluded. On to Stack Lab.

Held Wednesday, November 3, 1999

Overview

Today, we consider a simplification of lists, linear structures, collections which allow you to add and get elements, but little else.

Notes

• Our system was down this morning, so the outline is sketchier than normal.
• Clarified preconditions and postconditions.
• Are there any other questions.
• You should also be working on your projects. I'd like you to have at least partially-working implementations of your components by Friday, November 12.

Contents

Summary

• Linear structures
• Stacks
• Queues
• Priority queues
• Dequeues

## 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 multiplicity leads to some difficulties.
• Implementation can be more difficult, as we've already seen.
• Clients may find it difficult to use lists, as they must consider the interrelationship of the different functions.
• Many uses of lists can be simplified to three basic operations:
• get an element, removing it from the structure
• check if the sturcture is empty
• Some add a peek method which looks at the next element to be removed.
• Structures that support these three or four operations are often called linear structures.
• We can think of this in terms of a typical set of stuff to do. You add tasks as they come up. Whenever you have free time, you pick another task to do.
• 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.

### Detour: Naming Methods

• We call our basic methods `add` and `get`.
• Good design would suggest that every linear structure use the same names for these basic operations.
• Unfortunately, when these structures were first designed, no one had thought about the commonality. Hence, the ``standard'' names for operations in linear structures differ from structure to structure.

### 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
• One interesting aspect of stacks is that push and pop are clear inverses. If you push an element and then pop, the stack ends up the same. In other policies, this may not be the case.
• There are, of course, many ways to implement stacks. We can use arrays, lists, nodes, ....

### 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.
• Note that we cannot have dequeues match the three/four basic operations of linear structures.

### History

Tuesday, 10 August 1999

• Created as a blank outline.

Wednesday, 3 November 1999

• Filled in the details from outline 37 of CS152 99S.
• Reformatted and udpated.