Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Overview of Containers
Sequence Containers
Arrays and Vectors (provides index access to data)
List/ sequence
Deque (can move either end, but not middle)
Adapter Containers
Stacks (last in, first out)
Queue (first in, first out)
Priority Queue (deletion returns the largest/smallest value
Associative Containers
Set and Multiset/ bag (elements are the information ordering is by content)
Map/ Multimap (key-data relationship; associative pairs)
All the container classes are templated and what the container does and how
it works is independent of elements it contains
Containers are abstract data types (ADTs) that hold values. They don't
change the content only hold it so that it can be retrieved later.
Containers
Sequence Containers
Adapter Containers
Associative Containers
Vector
Stack
Set, Multiset
Deque
Queue
Map, Mutltimap
List
Priority Queue
Trees
Hash
v e c to r v (w ith 5 e le m e n ts )
7
4
9
3
1
v .re s iz e (8 ); (g ro w to 8 e le m e n ts )
7
4
9
3
1
0
0
v .re s iz e (3 ); (s h rin k to 3 e le m e n ts )
7
4
9
Value n
Value 2
Pointer to V3
In order to access a specific data value in the list, it is necessary to search from position 1 and follow the pointers from element to element until
the data value is located
Advantage: unlike a vector, a list is able to add a remove items efficiently at any position in the sequence
Insertion requires 2 updates ideal for applications that require sequential access to data while also requiring frequent insertion and deletion of
elements
B e f o r Inserting
e
3
12
fro nt
Dr. Nazli Mollah
12
fte r
5
lecture notes 1adapted
from
Data Structures with C++ using STL
Containers are typically most useful when they will contain only a limited number of
items and when the retrieval order is predefined or irrelevant. The most popular type
of containers are:
Stacks: Supports retrieval in last in, first out order (LIFO). Stacks are simple to
implement, and very efficient. Indeed, stacks are probably the right container to use
when the retrieval order doesn't matter at all, as when processing batch jobs. The put
and get operations for stacks are usually called push and pop.
Queues: Supports retrieval in first in, first out order (FIFO). FIFO may seem the fairest
way to control waiting times. However, for many applications, data items have infinite
patience. Queues are trickier to implement than stacks and are appropriate only for
applications (like certain simulations) where the order is important. The put and get
operations for queues are usually called enqueue and dequeue.
Both stacks and queues are storage containers that restrict how elements
enter and leave a sequence
Stack Containers
A stack allows access at only one end of the sequence, called the top
C
A
Push A
to p
Push B
(a )
B
A
to p
Push C
B
A
to p
B
A
Pop C
to p
A
to p
Pop B
(b )
Queue Containers
D
In s e rt
re a r
fro n t
D e le t e
18
13
3
15
27
S et A
5
1
3
27
S et B
15
B u ic k
BM W
F o rd
Jagu ar
H onda
Jeep
In d ex
P art#
P r ic e
A 2 9 -4 6 8
D 7 B -9 1 6
4 .9 5
M ir a g e
D 7 B -9 1 6
W 9 1 -A 8 3
1 2 .5 0
C a llo w a y
W 9 1 -A 8 3
A 2 9 -4 6 8
8 .7 5
M a r t in
V endor
Stacks
Stacks
Stack - LIFO
Since pop removes the item last pushed into the stack it is a Last In First out
LIFO ordering container
C
B
Push A
Push B
Push C
Pop C
Pop B
Push D
CLASS Stack
CLASS stack
Constructor
<stack>
stack();
Create
an empty stack
CLASS
stack
Operations
<stack>
CLASS Stack
CLASS stack
Operations
<stack>
void pop();
Remove the item from the top of the stack.
Precondition:
The stack is not empty.
Postcondition: Either the stack is empty or
the stack has a new
topmost
item from a previous
void
push.push(const T& item);
Insert the argument item at the top of the stack.
Postcondition: The stack has a new item at
the top.
Dr. Nazli Mollah
CLASS Stack
CLASS stack
Operations
<stack>
1
A
' FF'
431 % 16 = 15
431 / 16 = 26
'1 '
A
'1 '
A
'A '
'A '
'A '
'F '
F
'F
F'
'F
F'
26 % 16 = 10
26 / 16 = 1
1 % 16 = 1
1 / 16 = 0
A' A '
F' F '
P o p '1 '
n u m Str = "1 "
P u s h D ig it C h a r a c t e r s
' FF'
P o p 'A '
P o p 'F '
n u m Str = "1 A " n u m Str = "1 A F "
P o p D ig it C h a r a c t e r s
T r a in B e f o r e U n c o u p lin g E
E
C
D
U n c o u p le E . M o v e t o s id e t r a c k
B
C
D
U n c o u p le D . M o v e t o s id e t r a c k
A
B
C
D
E
U n c o u p le C M o v e a s id e
A
B
D
E
A t t a c h D t o e n d o f t r a in
A
B
D
A t t a c h E t o e n d o f t r a in
B
D
E
pop
target
pop
2
push
c
b
a
stack s
d
e
tmpStk
e
d
b
a
stack s
A rg u m e n ts
in t n
R e tu rn A d d re ss
R e tL o c o r R e tL o c 2
A c tivatio n R e c o r d
S y s te m S ta ck
I n m a in ( ) :
c a ll f a c t ( 4 )
In fact(4 ):
c a ll f a c t ( 3 )
A rg u m e n t
R e tu rn
R etL o c1
A rg u m e n t
R e tu rn
R etL o c2
A rg u m e n t
R e tu rn
R etL o c1
R P N ( R e v e r s e PData
o l Structures:
i s h N o t aCSCI
t i o 362
n ) e Overview
x p r e s sofi oSTLn Container
2 3 Classes
+
S c an o f E x p re s s io n a n d A c tio n C u rre n t o p e ra n d S ta c k
1 . I d e n t if y 2 a s a n o p e r a n d .
P u s h in t e g e r 2 o n t h e s t a c k .
2 . I d e n t if y 3 a s a n o p e r a n d .
P u s h in t e g e r 3 o n t h e s t a c k .
3 . I d e n t if y + a s a n o p e r a t o r
B e g in t h e p r o c e s s o f e v a lu a t in g + .
4 . g e tO p e ra n d s () p o p s s ta c k
t w ic e a n d a s s ig n s 3 t o
r ig h t a n d 2 t o le f t .
o p e ran d S tac k e m p ty
5 . c o m p u t e ( ) e v a lu a t e s le f t + r ig h t
a n d r e t u r n s t h e v a lu e 5 . R e t u r n
v a lu e is p u s h e d o n t h e s t a c k .
Main Index
Contents
Precedence
Symbol
Input
precedence
+ * / %
^
(
)
1
2
4
5
0
Stack
precedence
1
2
3
-1
0
Main Index
Contents
Rank
-1
-1
-1
0
0
lecture notes adapted from
Data Structures with C++ using STL
Summary Slide 1
- Stack
- Storage Structure with insert (push) and erase (pop)
operations occur at one end, called the top of the
stack.
- The last element in is the first element out of the
stack, so a stack is a LIFO structure.
Main Index
Contents
Summary Slide 2
- Recursion
- The system maintains a stack of activation records
that specify:
1) the function arguments
2) the local variables/objects
3) the return address
- The system pushes an activation record when
calling a function and pops it when returning.
Main Index
Contents
Summary Slide 3
3)
Main Index
Contents
Summary Slide 4
- Infix notation
- A binary operator appears between its operands.
- More complex than postfix, because it requires the
use of operator precedence and parentheses.
- In addition, some operators are left-associative, and
a few are right-associative.
Main Index
Contents