19+-+Templates

# 19+-+Templates - Linked Lists Double-ended list EECS 280...

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 Double-ended list 2 circle5 What if we wanted to insert something at the end of the list? circle5 Intuitively, with the current representation, we'd need to walk down the list until we found "the last element", and then insert it there. circle5 That's not very efficient, because we'd have to examine every element to insert anything at the tail. circle5 Instead, we'll change our concrete representation to track both the front and the back of our list. first Linked Lists Double-ended list 3 circle5 The new representational invariant has two node pointers: class IntList { node *first; node *last; public: }; circle5 The invariant on first is unchanged. circle5 The invariant on "last" is: circle5 last points to the last node of the list if it is not empty, and is NULL otherwise. Linked Lists Double-ended list 4 circle5 So, in an empty list, both data members point to NULL. circle5 However, if the list is non-empty, they look like this: circle5 Note: Adding this new data member requires that every method (except isEmpty ) be re-written. circle5 In lecture, we'll only write insertLast . first last Linked Lists Double-ended list 5 circle5 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 Double-ended list 6 circle5 To actually insert, there are two cases: circle5 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) circle5 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 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 Linked Lists Double-ended list 8 circle5 This is efficient, but only for insertion. circle5 Question : Why is removal from the end expensive? first last np first last Linked Lists Double-ended list 9 circle5 To make removal from the end efficient, as well, we have to have a “doubly-linked” list, so we can go forward and backward. circle5 To do this, we're going to change the representation yet again. circle5 In our new representation, a node is: struct node { node *next; node *prev; int value; } circle5 The next and value fields stay the same.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern