# Class 37: Linked Lists (1)

Back to Reading Gries. On to A List Wrapper (1).

Held: Monday, 7 April 2003

Summary: Today we begin our consideration of how to implement Scheme-like lists, which are generally called linked lists.

Related Pages:

Assignments:

• Think about what procedures you might include in the more imperative list that is simply a collection of values that you can step through one-by-one. I will ask you for your answer tomorrow.

Notes:

• I've rearranged the schedule to make it easier to meet at Saints' Rest on Friday. In particular, we'll look at lists the first three days of the week and then consider Gries-style loops on Friday.
• Did you find last Friday's class useful?

Overview:

• Review: What is a list?
• Key linked-list operations.
• Implementing nodes as records.
• Implementing key linked-list operations.

## What is a List?

• You may recall that we identified two very different perspectives on lists:
• The Scheme perspective: A list is either (1) null or (2) a pair of a value and a list. Many people call these linked lists.
• The Java perspective: A list is a collection that you can iterate. I'm not sure what else people call these, so I'll call them iterable lists.
• Note that linked lists refer more to an underlying implementation and iterable lists more to how one uses the structure.
• Both of these definitions are somewhat heterogeneous.
• Some data structure designers like similar homogeneous structures:
• A list of XXX's is either (1) null or (2) a pair of an XXX and a list.
• A list of XXX's is a collection of XXX's that you can iterate.
• For example,
• A list of integers is either (1) null or (2) a pair of an integer and a list.
• A list of integers is a collection of integers that you can iterate.
• Our approximate series of topics:
• Linked lists of integers.
• Heterogeneous linked lists.
• Iterable lists of integers.
• Heterogeneous iterable lists.

## Linked List Operations

• So, what are the key operations you expect to see in a Scheme list (or, more precisely, or C variation)?
• Here are the ones I'd include:
• Create an empty list.
• Add something to the front of the list.
• Determine if a list is empty.
• Get the first element of the list.
• Get all but the first element of the list.
• I expect that every other operation can be built from these.
• However, if there are other operations I expect to use regularly, I might want to include them as basics.

## Implementing Linked Lists

• As you may recall, each Scheme list is built from pairs.
• A pair is, in effect, a structure with two fields
• The datum.
• A pointer to the next pair (or to null).
• Most imperative programmers refer to those kinds of structures as node.
• It's fairly straightforward to implement these in C
```typedef struct node {
int datum;
struct node *next;
} node;
```
• Note that this structure is necessarily self-referential.

## Implementing Linked List Operations

```node *empty_list() {
return null;
} /* empty_list() */

/* Note: cons returns NULL if it fails. */
node *cons(int val, node *next) {
node *new_node = (node *) malloc(sizeof(node));
if (new_node == (node *) NULL)
return new_node;
new_node->datum = val;
new_node->next = next;
return new_node;
} /* cons(int, node *) */

/* The rest left for discussion. */
```

## Homogenous Linked Lists

• So, how do we make these structures homogenous?

## History

Tuesday, 7 January 2003 [Samuel A. Rebelsky]

• Created generic version to set up course.

Sunday, 6 April 2003 [Samuel A. Rebelsky]

• Filled in the details.

Back to Reading Gries. On to A List Wrapper (1).

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 2 14:20:57 2003.
The source to the document was last modified on Sun Apr 6 23:20:13 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Outlines/outline.37.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu