21+-+Templates - EECS 280 Programming and Introductory Data...

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

View Full Document Right Arrow Icon
Templates EECS 280 Programming and Introductory Data Structures 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Linked Lists Double-ended list 2 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 first
Background image of page 2
Linked Lists Double-ended list 3 The new representational invariant has two node pointers: class IntList { node *first; node *last; public: }; The invariant on first is unchanged. The invariant on "last" is:
Background image of page 3

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

View Full DocumentRight Arrow Icon
Linked Lists Double-ended list 4 So, in an empty list, both data members point to NULL. However, if the list is non-empty, they look like this: Note: Adding this new data member requires that every method (except first last
Background image of page 4
Linked Lists Double-ended list 5 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; ... }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Linked Lists Double-ended list 6 To actually insert, there are two cases: 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) 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
Background image of page 6
Linked Lists Double-ended 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
Linked Lists Double-ended list 8 This is efficient, but only for insertion. Question : Why is removal from the end expensive? first last np first last
Background image of page 8
Linked Lists Double-ended list 9 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: struct node { node *next; node *prev; int value;
Background image of page 9

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

View Full DocumentRight Arrow Icon
Double-ended list 10 With this representation, an empty list is unchanged. While the list (2, 3) would look like this:
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 31

21+-+Templates - EECS 280 Programming and Introductory Data...

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

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