1 / 17

Other List Structures

Other List Structures. Reading: Chapter 8 Homework: p.403, #1, 2, 3; p. 448, #1, 3, 8, 10, 15. Design Alternatives. Consider alternative designs for the actual implementation of the list For example, array singly linked list doubly linked list circular linked list

uta-jarvis
Télécharger la présentation

Other List Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Other List Structures Reading: Chapter 8 Homework: p.403, #1, 2, 3; p. 448, #1, 3, 8, 10, 15

  2. Design Alternatives • Consider alternative designs for the actual implementation of the list • For example, • array • singly linked list • doubly linked list • circular linked list • How would the choice be made? • Might it change in a later version?

  3. Array-Based List item 1 item 2 item 3 item 4 item 5

  4. Singly Linked List item 1 item 2 item 3 item 4 item 5 null

  5. Doubly Linked List item 1 item 2 item 3 item 4 item 5 null null

  6. Circular Linked List item 2 item 3 item 1 item 5 item 4

  7. Head and Tail Pointers item 1 item 2 item 3 item 4 item 5 null

  8. Lists Datatype • Interface should allow • creation of empty list • tests for empty • reinitialization to empty • access to front and back • insertion and removal • operators for moving iterator to beginning and end of list (begin() and end() );

  9. STL List Constructors list<T> lst; Creates an empty list for elements of type T. list<T> lst(n, e); Creates list of n copies of e. list<T> lst(beg, end); Creates list with copies from range beg..end. lst.~list<T>(); Destroys all elems and frees memory.

  10. STL List: Front/Back Push/Pop

  11. STL List: Entire List Methods

  12. Lists and Iterators • Stack object had member data defined to specify access points • Ok for stack since it allows only a single access point • Each container class is equipped with an associated iterator class. • Iterators maintain pointer to “current” element

  13. Iterator • A pointer-like object • used to cycle through • all the elements stored in a container • // Built-in type • vector<int>::iterator p; • // Built-in values • p = v.begin(); • p != v.end()

  14. List Iterator Datatype • Interface provides • Operators for moving iterator to beginning of list ( begin()) and end (end() ) • Operators for moving through the list (++ and --) • Operator for returning the current item on the list (*) • Generic algorithms (Find, Remove, etc.)

  15. Traversing List using Iterator # include <list> list<int> L; L.push_back(0); L.push_front(1); L.push_back(2); L.sort(); list<int>::iterator itr; itr = L.begin(); while (itr != L.end()) { cout << *itr << " "; itr++; } // The values that are printed are 0 1 2

  16. Adding Elements to a List

  17. Erasing Elements from a List

More Related