Lecture12

Lecture12 - L … assume L has some elements...

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

View Full Document Right Arrow Icon
CS2134 Lecture 12 Linked Lists
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS2134 Linked Lists Review of basic list ops (pictures): insert after delete after insert at front delete from front Design issues with or without header nodes singly vs doubly linked list circular lists iterator awareness
Background image of page 2
CS2134 Textbook’s Implementation Three classes: LList LListNode LListItr Friend relationship between them and lots of conversions back and forth between pointers and iterators In depth look at the LinkedList.cpp and LinkedList.h code. Sorted List as subclass Book also has STL style implementation of doubly linked list (we won’t cover)
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS2134 List Traversals Lots of applications require iteration through list, accessing each element (until end or until some condition is met) Sometimes useful to keep additional iterators, such as “prev” (sometimes called “trailer”) that stays one step behind “current” iterator or pointer.
Background image of page 4
CS2134 Example //find first negative element in list LList<int>
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: L; // … assume L has some elements LLItr<int> current = L.first(); while (current.IsValid() && (current.retrieve() >= 0)) current.advance(); if (current.IsValid()) cout << “First negative element is “ << x; else cout << “All elements are positive.” CS2134 Example2 // Remove first negative element from list LList<int> L; … LLItr<int> current = L.first(); LLItr<int> prev = L.zeroth(); while (current.IsValid() && (current.retrieve() >= 0)) { prev = current; current.advance(); } if (current.IsValid()) L.removeAfter(prev); // book’s implementation doesn’t // provide this, but it should CS2134 STL Lists • http://www.sgi.com/tech/stl/List.html • Doubly linked list with – Forward & reverse iterators – O(1) insertion and deletion at front, back and arbitrary points – Iterators not invalidated on insertion, splicing or removal (except removed node) – … • STL also provides singly linked list: slist...
View Full Document

This note was uploaded on 12/09/2009 for the course CS 2134 taught by Professor Hellerstein during the Spring '07 term at NYU Poly.

Page1 / 7

Lecture12 - L … assume L has some elements...

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

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