Data Structures — Unit 1 — Pointers and Linked Lists (continued)

Packaging the algorithms

We can package the last three algorithms in subroutines.

I'll use the convention that a success flag is to be set to false, if space on the heap runs out, and to true, otherwise.

Pointers to links.

Consider inserting a node at the end of a list.

There are two cases that may be coded separately

When the list is empty: Inserting at the Head (we saw this in the reverse copy):

Node * newborn = new(nothrow) node ;
newborn->data = whatever ;
newborn->next = 0 ;
head = newborn ;

When the list is not empty: We insert after the last node. Assume p points to the last node.

Node * newborn = new(nothrow) node ;
newborn-data = whatever ;
newborn->next = 0 ;
p->next = newborn ;

Notice that "head" and "p->next" play similar roles. Both represent the last link in the list.

If only there where a way that a single expression could represent either the variable "head" or the field "p->next".

There is. We can use a pointer that always points to the last link of the list.

Since the links are of type

pointer to Node,

a pointer to a link must have type

pointer to pointer to Node.

Making a list of input values (revisited).

This time we will make the list in "forward order", meaning that the first value read will be at the head of the list.

We will use a variable linkP that always point to the last link in the list.

Making a copy of a list

This time, we will make the list forward.

The idea is the same, use linkP to point to the last link in the list.

Inserting at a given point.

Suppose we have a list of at least n nodes.

We wish to insert a new node at position n, where position 0 means at the head, position 1 means after the first node, etc.

Links are numbered as follows: head is link number 0. head-next is link number 1 and so on.

We need to find link n and then insert a new node after it.

Deleting node number n from a list

Suppose a node has at least n+1 nodes. How can you delete node n of the list. (The first node is node 0, etc.)

[If you are interested in how to accomplish the same tasks without using pointers to links, follow this link.]

A Linked list implementation of the list ADT.

We can reimplement out list ADT using singly linked lists.

File SinglyLinkedList.h.

Note: I made one slight change to the way things are done above; I don't bother setting the tail link to null; instead, I keep track of the length in a separate field.

C++ Digression

Q? Why are the copy-constructor, the destructor, and the assignment operator not commented out in the header?

A. Commenting them out indicates that we will not provide an implementation for them. Thus we are stuck with the default implementation .

Changes to one list then also affect the original list, but the ``len'' field of the original list would then be wrong! This is called data-structure corruption.

E.g. deleting all the nodes in one list could cause a dangling reference in the other. Violating the rule of of stale pointers.

Design Tip: "The RULE of the big three''. Either implement none or all of: the copy constructor, the destructor, and the assignment operator. If one needs a special implementation, then likely all three do.

Comparison of Array and linked-list implementations.

Array
Linked-list
Space
Fixed at max
Flexible
Insert/Delete @ position i
len_-i data copies
i pointer jumps
Retrieve @ position i
Independent of i
i pointer jumps
Copy/Assign/Destruct
max data copies
len_ data copies

[Pointers and Linked Structures -- Continued]


© Theodore S. Norvell 1996--2004.