This preview shows pages 1–3. Sign up to view the full content.
11/22/2009
1
Templates
EECS 280
Programming and Introductory Data Structures
1
Linked Lists
Doubleended list
2
c
What if we wanted to insert something at the end of the list?
c
Intuitively, with the current representation, we'd need to
walk down the list until we found "the last element", and
then insert it there.
c
That's not very efficient, because we'd have to examine every
element to insert anything at the tail.
c
Instead, we'll change our concrete representation to track
both the front and the back of our list.
first
Linked Lists
Doubleended list
3
c
The new representational invariant has
two
node pointers:
class IntList {
node *first;
node *last;
public:
…
};
c
The invariant on first is unchanged.
c
The invariant on "last" is:
c
last points to the last node of the list if it is not empty, and is
NULL otherwise.
Linked Lists
Doubleended list
4
c
So, in an empty list, both data members point to NULL.
c
However, if the list is nonempty, they look like this:
c
Note:
Adding this new data member requires that
every
method (except
isEmpty
) be rewritten.
c
In lecture, we'll only write
insertLast
.
first
last
Linked Lists
Doubleended list
5
c
First, we create the new node, and establish its invariants:
void IntList::insertLast(int v) {
node *np = new node;
np>next = NULL;
np>value = v;
...
}
Linked Lists
Doubleended list
6
c
To actually insert, there are two cases:
c
If the list is empty, we need to reestablish the invariants on
first
and
last
(the new node is both the first and last
node of the list)
c
If the list is
not
empty, there are two broken invariants.
The
"old"
last>next
element (incorrectly) points to NULL,
and the
last
field no longer points to the last element.
first
last
np
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 11/22/2009
2
Linked Lists
Doubleended list
7
void IntList::insertLast(int v) {
node *np = new node;
np>next = NULL;
np>value = v;
if (isEmpty()) {
first = last = np;
} else {
last>next = np;
last = np;
}
}
first
last
np
Linked Lists
Doubleended list
8
c
This is efficient, but only for insertion.
c
Question
:
Why is removal from the end expensive?
first
last
np
first
last
Linked Lists
Doubleended list
9
c
To make removal from the end efficient, as well, we have to have a
“doublylinked” list, so we can go forward
and
backward.
c
To do this, we're going to change the representation yet again.
c
In our new representation, a node is:
struct node {
node *next;
node *prev;
int
value;
}
c
The
next
and
value
fields stay the same.
c
The
prev
field's invariant is:
c
The
prev
field points to the previous node in the list, or NULL if
no such node exists.
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 10/26/2010 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.
 Fall '08
 NOBLE

Click to edit the document details