170 likes | 343 Views
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
E N D
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 • How would the choice be made? • Might it change in a later version?
Array-Based List item 1 item 2 item 3 item 4 item 5
Singly Linked List item 1 item 2 item 3 item 4 item 5 null
Doubly Linked List item 1 item 2 item 3 item 4 item 5 null null
Circular Linked List item 2 item 3 item 1 item 5 item 4
Head and Tail Pointers item 1 item 2 item 3 item 4 item 5 null
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() );
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.
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
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()
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.)
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