


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















A vector has two disadvantages:– It is not efficient when the number of elements to be
stored varies widely during a program execution.
at the end (possibly the beginning)
List:
A
list
is a kind of sequence that supports
bidirectional iterators and allows constant time insert anderase operations anywhere within the sequence, withstorage management handled automatically. Unlike vectorsfast random access to list elements is not supported, butmany algorithms only need sequential access anyway
Object elementNode * next;
a^
b^
c^
d
Front of list
Figure 17.
Inserting a node into a list: Node *tmp = new Node;tmp->element = x;tmp->next = current->next;current->next = tmp;
a^
b
x
current
tmp
Fig. 17.
a^
b^
c
header
Fig. 17.
header Fig. 17.5 an empty list
Node
Node
Node
Node
A list
Node Node
Node
Node
Node
Node
Node
Node
Node
Node
struct Node{
**Object element;Node prev;Node next;
};
newNode = new Node; newNode->element = x;newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode;
b
x
a
3 1
2
4
Fig. 17.
#include
List header
list
Default constructor O(1)
list
Copy constructor O(n)
v = aList;
Assignment O(n)
v.swap(aList);
Swap values with another list O(1)
v.remove(value);
Remove all occurrences of value O(n)
v.remove_if(predicate);
Remove all values that match the conditionO(n)
list
Declare a new iterator O(1)
v.begin();
Starting iterator O(1)
v.end();
Ending iterator O(1)
v.rbegin();
Starting backward moving iterator O(1)
v.rend()
Ending backward moving itarator O(1)
v.merge(aList);
Merge with another ordered list O(n)
v.sort();
Place elements in accending order O(nlogn)
#include list
bitsetdequelist queuestackVector
Header
Class
post: size() == distancebetween i and j.constructs a sequence equalto the range
[i,j
X(i, j)X a(i, j);
post: size() == n.constructs a sequence with
n
copies of t.
X(n, t)X a(n, t);
assertion/notepre/post-condition
returntype
expression