# Class 40: Introduction to Abstract Data Types and Data Structures

Back to Polymorphism. On to Scheme Lists.

Held: Monday, 12 April 2004

Summary: Today we begin our in-depth investigation of abstract data types and data structures by considering some of the general rules for data type design. We also start building a partial hierarchy of data types.

Related Pages:

Assignments:

• Read the section on lists in the data structures book distributed to you. Summarize the methods associate with the list ADT.

Notes:

• We're moving away from language details toward data structures and abstract data types.
• Are there questions on homework 6?

Overview:

• Organizing Data.
• Example: Arrays.
• Collections.

## Organizing Data

• As you saw from some of the algorithms we've written way that you organize your information can have a significant effect on the running time of your algorithms.
• For example, we can find the element in the middle of an array in O(1) time, but it takes O(n) time to find the element in the middle of a list.
• Hence, we often define data structures that help us organize information.
• As you might guess, there is a close relationship between data structures and algorithms.
• Typically, we use data structures to create efficient implementations of the operations needed by algorithms.
• Some data structures suggest new algorithms.
• As in many other cases, we separate the implementation and specification when talking about ways to organize information.
• Abstract Data Type (ADT) is the term typically used for the specification. For most data types, we specify the core operations in the ADT. We will almost always use interfaces for ADTs.
• Data Structure is the term typically used for the implementation. We will almost always use classes for data structures.
• But people are casual with terminology.
• Note that their are typically four issues we will consider when we work with ADTs:
• Their overall concept: what grounds the design of this ADT or structure (sometimes this speeks to expected efficiency);
• Their relationships to other ADTs: where do they fit in the ADT hierarchy?
• Their functionality: what methods the structures provide (the ADT);
• Their applications: how we might use them.
• Similarly, there are three issues we will consider when we work with data structures:
• Their strategy: the central idea in the implementation;
• Their implementation: how they do what they do;
• Their efficiency: how fast they are (or with how much memory they consume); and
• Sorry, no clever equivalent to the 6P's.
• How do we build new data structures? Using other data structures, including Java's built-in arrays.

## Arrays, Revisited

• We'll begin by reconsidering arrays.
• What is the purpose of an array?
• What are the key operations that you might use to describe arrays?
• Don't read the next few lines until we've had a chance to discuss them.
• We want to create new arrays
• Input: size of the array
• Input: type of the elements in the array
• We want to set elements of arrays
• Expected running time: O(1)
• We want to get elements of arrays
• Expected running time: O(1)
• We want to get the size of arrays
• Expected running time: O(1)
• What else?

## Collections

• Most data structures are used to collect a number of objects
• Arrays
• Lists
• Can you think of others?
• Hence, most data structures we create will be variations (subinterfaces) of a general `Collection` interface.
• Java now includes such an interface, but we'll design our own.
• What methods might such an interface include? We'll need to consider purpose and detailed meaning (perhaps even the legendary 6 P's).
• Here are some basics (without detailed definitions):
• We might create collections.
• We might add elements to the collection.
• We might delete elements from the collection.
• We might check if there is room for more elements.
• We might clear all the elements in the collection.
• Are there others you can think of?
• I tend to start with very simple collections and then immediately split into
• Iterated Collections: Collections in which you can step through the values, one-by-one.
• Tables: Collections in which you can look up values. (By number, by name, by ....).

## History

Back to Polymorphism. On to Scheme Lists.

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 Fri May 7 09:43:27 2004.
The source to the document was last modified on Tue Jan 13 10:26:12 2004.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Outlines/outline.40.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu