20-templates.student

20-templates.student - Last time: * Linked structures *...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Last time: * Linked structures * single-ended list Today: * Double-ended list * doubly-linked list * The "container-of-references" model * Polymorphic containers +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ What if we wanted to insert something at the end of the list? Intuitively, with the current representation, we'd need to walk down the list until we found "the last element", and then insert it there. That's not very efficient, because we'd have to examine every element to insert anything at the tail. Instead, we'll change our concrete representation to track both the front and the back of our list. The new rep has *two* node pointers: class IntList { node *first; node *last; public: ... }; The invariant on first is unchanged. The invariant on "last" is: last points to the last node of the list if it is not empty, and is NULL otherwise. So, in an empty list, both data members point to NULL. However, if the list is non-empty, they look like this: +---+ +---+ +---+ +---+ first----> | -----> | -----> | -----> | -----\ +---+ +---+ +---+ +---+ \ ^ --- last-----------------------------------/ - This can make insert efficient, but not removal. To make removal from the end efficient, as well, we have to have a doubly-linked list, so we can go forward *and* backward. To do this, we're going to change the representation yet again. In our new representation, a node is:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
struct node { node *next; node *prev; int value; } The next and value fields are the same. The "prev" field's invariant is: The "prev" field points to the previous node in the list, or NULL if no such node exists. With this representation, an empty list is unchnaged While the list (2, 3) would look like this: first------V V--------last +---+ +---+ | ------->| ----------\ +---+ +---+ | /------- |<------ | ----- | +---+ +---+ --- ----- | 2 | | 3 | - --- +---+ +---+ - ++++++++++++++++++++++++++++++++++++++++ Note: adding this new data member requires that *every* method (except isEmpty) be re-written. We'll only do insertBack here, but you'll get to do them all in Project 5! In lecture, we'll only re-write insertLast. First, we create the new node, and establish its invariants: void IntList::insertLast(int v) { node *np = new node; np->next = NULL; np->prev = last; // If empty, NULL, if not, last node np->value = v; ... } To actually insert, there are two cases---if the list is empty, we
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/28/2010 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 8

20-templates.student - Last time: * Linked structures *...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online