220 likes | 228 Views
Learn about lists and iterators in data structures. Understand list operations, inserting and removing elements, and ordered lists. Explore the list ADT and the use of iterators to access and manipulate list elements.
E N D
Chapter 6 The List Container and Iterators
Outline • Sample list • The list ADT • CLASS list Constructors • CLASS list Operations • CLASS list::iterator Operations • Inserting an element into a list • Removing an element from a list • Ordered lists • Splicing two lists
§- list - A Sequence of elements stored by position. - Index access is not available… §- to access the value of an element, must pass through its preceding elements. 3
Shifting blocks of elements to insert or delete a vector item
Model of a list object with links to next and previous element
The List ADT • The list API documents the member function prototype as well as pre- and postconditions. • provides three constructors to declare a list object.
list(); Create an empty list. This is the default constructor. CLASS list <list> Constructors list(int n, const T&value = T()); Create a list with n elements, each having a specified value. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T(). list(T *first, T *last); Initialize the list, using the address range [first, last).
T& back(); Return the value of the item at the rear of the list. Precondition: The list must contain at least one element. CLASS list <list> Operations bool empty() const; Return true if the list is empty, false otherwise. T& front(); Return the value of the item at the front of the list. Precondition: The list must contain at least one element.
void push_back(const T& value); Add a value at the rear of the list. Postcondition: The list has a new element at the rear, and its size increases by 1. CLASS list <list> Operations void pop_back(); Remove the item at the rear of the list. Precondition: The list is not empty. Postcondition: The list has a new element at the rear or is empty.
void push_front(const T& value); Add a value at the front of the list. Postcondition: The list has a new element at the front, and its size increases by 1. CLASS list <list> Operations void pop_front(); Remove the item at the front of the list. Precondition: The list is not empty. Postcondition: The list has a new element at the front or is empty. int size() const; Return the number of elements in the list.
iterator begin(); Returns an iterator that references the first position (front) of the list. If the list is empty, the iterator value end() is returned. CLASS list <list> Operations const_iterator begin(); Returns a const_iterator that points to the first position (front) of a constant list. If the list is empty, the const_iterator value end() is returned. iterator end(); Returns an iterator that signifies a location immediately out of the range of actual elements. A program must not dereference the value of end() with the * operator.
iterator end(); Returns an iterator that signifies a location immediately out of the range of actual elements. A program must not dereference the value of end() with the * operator. CLASS list <list> Operations const_iterator end(); Returns a const_iterator that signifies a location immediately out of the range of actual elements in a constant list. A program must not dereference the value of end() with the * operator.
void erase(iterator pos); Erase the element pointed to by pos. Precondition: The list is not empty. Postcondition: The list has one fewer element. CLASS list <list> Operations void erase(iterator first, iterator last); Erase all list elements within the iterator range [first, last]. Precondition: The list is not empty. Postcondition: The size of the list decreases by the number of elements in the range.
CLASS list <list> Operations iterator insert(iterator pos, const T& value); Insert value before pos, and return an iterator pointing to the position of the new value in the list. The operation does not affect any existing iterators. Postcondition: The list has a new element.
§-iterator - A generalized pointer that moves through a container(eg. vector, list) element by element… forward or backward - At any point, the * operator accesses the value of a container item. Example: PP1010 15
*: Accesses the value of the item currently pointed to by the iterator. *iter; CLASS list::iterator <list> Operations ++: Moves the iterator to the next item in the list. iter++; --: Moves the iterator to the previous item in the list. iter--; ==: Takes two iterators as operands and returns true when they both point at the same item in the list. iter1 == iter2 !=: Returns true when the two iterators do not point at the same item in the list. iter1 != iter2
Inserting an element into a list Insertion function returns an iterator pointing at the new element The original iterator continues to point at the same element
Removing an element from a list Erase function make the iterator invalid and the iterator must be reinitialized.
§- The list class has two iterator types: 1) iterator: A generalized list traversal pointer. 2) const_iterator: must be used with a constant list object. Each type is a nested class of list and must be accessed by using the scope operator :: For example: list<int>::iterator iter; 19
§- the list member function begin() - Gives an iterator an initial value that points to the first element. §- the list member function end() - Returns an iterator pointing just past the last element of the list. 20
§- list class member functions insert() and erase() • - Both use an iterator argument to modify a list. • insert(): places value in the list before the data referenced by the iteratorpos. • erase(): removes the data item referenced by pos from the list. 21
§- The sequential search of a list object - implemented by using an iterator range [first, last). - It returns an iterator that points at the target value or has value last if the target is not in the list. Example: PP 1057: program 17-6 22