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

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

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

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

View Full Document Right 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
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)
Image of page 3

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

View Full Document Right 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.
Image of page 4
CS2134 Example //find first negative element in list LList<int>
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern