# Class 48: LInear Structures

Held: Friday, 25 April 2003

Summary: Today we consider a variety of linear structures, among the simplest forms of collections.

Related Pages:

Assignments:

• Read the chapter on linear structures in Bailey.

Notes:

• CSC151 project presentations Monday at 2:15.

Overview:

• Stacks.
• Queues.
• Implementation notes.

• Linear Structures are one of the simplest forms of collections.
• Linear structures typically provide two basic operations:
• `put`, add an element to the collection
• `get`, get an element from the collection, removing it from the collection
• Since `get` requires a non-empty collection, most collections provide a `isEmpty` predicate.
• The choice of what `get` returns is up to the policy of the particular kind of linear structure.
• Stacks implement a last in, first out (LIFO) policy.
• Queues implement a first in, first out (FIFO) policy.
• Priority queues implement a best first policy.
• There are a few related operations that you may find helpful:
• `peek` returns the object to be returned by `get`, but without deleting it.
• `size` returns the number of elements in the structure.
• It might also be useful to have an `isFull` operation.
• The individual operations traditionally have different names in the different types of linear structures.
• Why? The structures were invented first. Then someone realized they had a commonality. By that point, the names were ingrained.

## Stacks

• LIFO policy.
• LIFO mimics many common operations, such as what happens with procedure calls in most programming languages.
• `add` is typically called `push`.
• `get` is typically called `pop`.
• `peek` is typically called `top`.

## Queues

• FIFO policy.
• `add` is typically called `enqueue`.
• `get` is typically called `dequeue`.
• `peek` is typically called `front`.

## Implementation Notes

• As we noted earlier, you can implement lists with arrays or nodes.
• Similarly, we can implement linear structures with arrays or nodes (or even with lists).
• If we're implementing a queue with nodes, we probably want to keep a pointer to both the front and the back of the queue.
• We'll probably look at that implementation in more detail.

## History

Thursday, 15 January 2003 [Samuel A. Rebelsky]

• Created as a mostly-blank outline.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 6 09:21:07 2003.
The source to the document was last modified on Thu Jan 16 15:16:16 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.48.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu