main index contents 11 main index contents shifting blocks of elements… shifting blocks of...
TRANSCRIPT
![Page 1: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/1.jpg)
1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents
Shifting blocks of elements…Model of a list object…Sample listThe list ADTCLASS list ConstructorsCLASS list Operations (7 slides)CLASS list::iterator OperationsInserting an element into a listRemoving an element from a listOrdered listsSplicing two listsSummary Slides (5 slides)
Chapter 6 Chapter 6 – – The List Container and IteratorsThe List Container and Iterators
![Page 2: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/2.jpg)
2 Main IndexMain Index ContentsContents
Shifting blocks of elements to Shifting blocks of elements to insert or delete a vector iteminsert or delete a vector item
15 20 30 35 40Initial V ec to r
30 3515 40
20
Eras e 20 atP o s itio n 1
S hift left0 4321
15 20 30 35 4025Ins ert 25 at
P o s itio n 2
S hift right
32
10 54
15 20 30 35 40Initial V ec to r
0 4321 210 3
![Page 3: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/3.jpg)
3 Main IndexMain Index ContentsContents3 Main IndexMain Index ContentsContents
Model of a list object with links Model of a list object with links to next and previous elementto next and previous element
fro nt b ac k
![Page 4: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/4.jpg)
4 Main IndexMain Index ContentsContents
Sample listSample list
Pasta Delectica
6 :1 5
J o n es ( 4 )
G o n zales ( 5 )
F r an k s ( 3 )
Bo u to n ( 6 )
L iu ( 2 )6 :0 0
![Page 5: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/5.jpg)
5 Main IndexMain Index ContentsContents5 Main IndexMain Index ContentsContents
The List ADTThe List ADT The list API documents the member function
prototype as well as pre- and postconditions.– provides three constructors to declare a list object.
0 .00 .0 0 .00 .0 0 .00 .0 0 .00 .0(a) lis t< d o ub le> realL is t(8)
8 :3 08 :3 0 8 :3 08 :3 0 8 :3 0 8 :3 0(b ) lis t< tim e24> tim eLis t(6, 8:30)
ar ray
(c ) lis t< s tring> s trL is t(s trA rr, s trA rr+ 3)l is tve c to r
![Page 6: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/6.jpg)
6 Main IndexMain Index ContentsContents6 Main IndexMain Index ContentsContents
CLASS list Constructors <list>
list();Create an empty list. This is the default constructor.
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).
![Page 7: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/7.jpg)
7 Main IndexMain Index ContentsContents7 Main IndexMain Index ContentsContents
CLASS list Operations <list>
T& back();Return the value of the item at the rear of the list. Precondition: The vector must contain at least one
element.
bool empty() const;Return true if the vector is empty, false otherwise.
T& front();Return the value of the item at the front of the list. Precondition: The vector must contain at least one
element.
![Page 8: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/8.jpg)
8 Main IndexMain Index ContentsContents8 Main IndexMain Index ContentsContents
CLASS list Operations <list>
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.
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.
![Page 9: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/9.jpg)
9 Main IndexMain Index ContentsContents9 Main IndexMain Index ContentsContents
CLASS list Operations <list>
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.
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 vector.
![Page 10: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/10.jpg)
10 Main IndexMain Index ContentsContents10 Main IndexMain Index ContentsContents
CLASS list Operations <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.
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.
![Page 11: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/11.jpg)
11 Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents
CLASS list Operations <list>
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.
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.
![Page 12: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/12.jpg)
12 Main IndexMain Index ContentsContents12 Main IndexMain Index ContentsContents
CLASS list Operations <list>
void erase(iterator pos);Erase the element pointed to by pos.Precondition: The list is not empty.Postcondition: The list has one fewer element.
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.
![Page 13: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/13.jpg)
13 Main IndexMain Index ContentsContents13 Main IndexMain Index ContentsContents
CLASS list Operations <list>
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.
![Page 14: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/14.jpg)
14 Main IndexMain Index ContentsContents14 Main IndexMain Index ContentsContents
CLASS list::iterator Operations <list>
*:Accesses the value of the item currently pointed to by the iterator.
*iter;*iter;
++: Moves the iterator to the next item in the list. iter++;iter++;
--: Moves the iterator to the previous item in the list.
iter--;iter--;
==: Takes two iterators as operands and returns truewhen they both point at the same item in the list.
iter1 == iter2iter1 == iter2
!=: Returns true when the two iterators do not point at the same item in the list.
iter1 != iter2iter1 != iter2
![Page 15: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/15.jpg)
15 Main IndexMain Index ContentsContents15 Main IndexMain Index ContentsContents
Inserting an element into a listInserting an element into a list
fro nt
Lis t o b je c t (a fte r)
fro nt re a r
Lis t o b je c t (b e fo re )
ne w E ltre a r
ite r
2 55937 2 9374
ite r4
![Page 16: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/16.jpg)
16 Main IndexMain Index ContentsContents
Removing an element from a listRemoving an element from a list
fro nt
Lis t o b je c t (a fte r)
fro nt re a r
Lis t o b je c t (b e fo re )
re a rite r
2 5937 2 593
ite r? ?
![Page 17: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/17.jpg)
17 Main IndexMain Index ContentsContents17 Main IndexMain Index ContentsContents
Ordered listsOrdered lists8 27 46 0
fro nt re a r6 5
8 27 46 0fro ntre a r
5 0 8 27 46 5
fro nt re a rc u rr
6 06 5
5 0
B e fo re Ins e rt A fte r Ins e rt
Position the iterator curr at the front of the list.
Insert 50 in the list:
![Page 18: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/18.jpg)
18 Main IndexMain Index ContentsContents18 Main IndexMain Index ContentsContents
Splicing two listsSplicing two lists
7 1 5 1 6 3 47 1 5 3 47 3 4
d e s tLis t
1 5 1 6
s o u rc e Ite r
s o u rc e Lis t p o s
5
d e s tLis t (A fte r ins e rt o f 1 5 )
1 5 1 6
s o u rc e Ite r
s o u rc e Lis t p o s
5
d e s tLis t (A fte r ins e rt o f 1 6 )
1 5 1 6
s o u rc e Ite r
s o u rc e Lis t p o s
5
![Page 19: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/19.jpg)
19 Main IndexMain Index ContentsContents19 Main IndexMain Index ContentsContents
Summary Slide 1Summary Slide 1
§- 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.
§- list iterator- A generalized pointer that moves through a list
element by element… forward or backward
- At any point, the * operator accesses the value of a list item.
![Page 20: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/20.jpg)
20 Main IndexMain Index ContentsContents20 Main IndexMain Index ContentsContents
Summary Slide 2Summary Slide 2
§- The list class has two iterator types:1)1) iteratoriterator:
A generalized list traversal pointer.
2)2) constconst __ iteratoriterator:
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 ::::
![Page 21: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/21.jpg)
21 Main IndexMain Index ContentsContents21 Main IndexMain Index ContentsContents
Summary Slide 3Summary Slide 3
§- 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.
![Page 22: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/22.jpg)
22 Main IndexMain Index ContentsContents22 Main IndexMain Index ContentsContents
Summary Slide 4Summary Slide 4
§- The sequential search of a list object- implemented by using an iterator range
[firstfirst, lastlast).
- It returns an iterator that points at the target value or has value lastlast if the target is not in the list.
![Page 23: Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample](https://reader036.vdocument.in/reader036/viewer/2022081507/5518d09055034638098b5048/html5/thumbnails/23.jpg)
23 Main IndexMain Index ContentsContents23 Main IndexMain Index ContentsContents
Summary Slide 5Summary Slide 5
§- list class member fns insert() and erase()
- Both use an iterator argument to modify a list.
1)1) insert()insert(): places value in the list before the data
referenced by the iterator pospos.
2)2) erase()erase():removes the data item referenced by pospos from the list.