You are on page 1of 11

5.

- , , 5.1
,
. ,
,
.
, . ,
.
, .
,
( ),
.
:
-
-
, .
, .
:
.
.

: .
,
( ).
(
),
.

. ,
, ,
, (
) .
.
,
. ,
1

.
.

5.2 - stack

--- (last-in-first-out).

. .
, .
.
0.
.
,
.

Rob Roy
War & Peace
Moby Dick

War & Peace


Moby Dick

Misrables
War & Peace
Moby Dick

2001
Misrables
War & Peace
Moby Dick

5.1.
() :
1.
2.
3. ( push)
4. ( pop)
5. , .
, 2 ,
: .
,

.
depth
, elems maxdepth
elems[0..depth-1].
,
.
2

0
1
element element
depth=0

depth1
element

depth

maxdepth1

maxdepth1

(maxdepth = 6):

0
Moby
Dick

1
War &
Peace

2
Rob
Roy

depth=3

5.2
.
,
(
). .

element

element

element

Rob
Roy

War &
Peace

Moby
Dick

5.3
. ,
, , . ,
.
,
,
.
.
.
, ,
.
, , .,
.
. ,
. ,
3

.

. .
.
, :

.
, :

.
.
.
.
2
: 1. 2.
(, , ),
2 .
: {(}),
{}(), {...(...)...}.

s (s a) (s b) (s c)
( b + [b2 4ac]) / 2a

s (s a) (s b (s c)
s (s a) s b) (s c)
( b + [b2 4ac)] / 2a

.
.
( ), :

.
.


.

.
.




.
.
4

,
.
,
.
.
(5+9) * 2+6 * 5
,
, . ,

+*+592*65
.
,
. .
,
59+2*65*+
(),
.
.
( ), :


.








5.4

5.3 - queue

---.
(), ().
.
0.
5

() :
1.
2.
3. ( en-queue)
4. ( de-queue)
5. ,
.
2 ,
.
,
maxlen.
length, ,
(front) (rear), elems,
elems[frontrear-1].
.

front
element element

:
0

rear1
element

maxlen1

front=rear

maxlen1

5.5

, 6 .

Homer, Marge, Bart, Lisa:

elems
front

rear

length

front

elems Homer Marge Bart


0

rear

elems

2
Bart

rear

length

elems
front

rear

Lisa
length

Marge Bart
1

rear

Lisa Maggie
length

Ralph:

Lisa Maggie
length

Lisa Maggie

front

Maggie:

front

elems Homer Marge Bart

elems
front

2
Bart

rear

Lisa Maggie Ralph


length

5.6
( ),
?
( 6 ,
4). ?
,
, ,
.
O(n), .
?
.
n . ,
a[0] [n-1], a[n-1] a[0].
.
7

2
4

5.7
, ,
(maxlen),
length, , (front)
(rear), elems,
elems[frontrear-1] elems[frontmaxlen1]
elems[0rear1].

0

:
:

0
element
0

front
element element

rear1
element

maxlen1

rear1
element

front
element

maxlen1
element

front=rear

maxlen1

5.8

Homer, Marge, Bart, Lisa:

elems
front

rear

length

front

elems Homer Marge Bart


0

rear

elems

2
Bart

rear

elems

length

front

front

length

Marge Bart
1

rear

Lisa Maggie
length

length

elems

Lisa Maggie

front

2
Bart

rear

Lisa Maggie Ralph


length

Martin:

2
Bart

rear

rear

3
Lisa

Ralph:

Nelson:
elems Nelson

Lisa Maggie

front

Maggie:

front

elems Homer Marge Bart

Lisa Maggie Ralph


length

front

elems Nelson Martin Bart


2

rear

Lisa Maggie Ralph


length

elems Nelson Martin


front

rear

Lisa Maggie Ralph


2

length

5.9

.
, ,
, :
( - front) ( - rear) length.

:

front

element

element

element

rear
front
rear
front

Homer

Marge

Bart

Lisa

rear

5.10
.
.
, ,
8

,
, ,
. ,
.
(scheduler)
.

, ,
.
, ,
. , ,
, ,
. , 2
.

, :





, :


, :


.
.
.
,
.

5.11

5.12

5.4 - priority queue



, .
.
,
0.

,
.
.
.
,
, .
.
.
?
1.
2.
a.
3.
4.
5.
a. ( )

6.
?

, ,
,
.
:
1.
2.
3.
4.
5. ,
.
,
, 2 :
1.
2.
,
.
,
.
,
.
10

: (n)
. ,
, , O(log n).
, O(n).
: ,
O(n). O(1).
. ,
( ),
( ).
, : ,
, O(1).
.
. ,
, ,
O(n).
,
.

O(n)

O(1)

O(n)

O(1)

O(1)

O(n)

O(1)

O(n)

5.1

,
,
, ,
n , :
, n*O(n) + n*O(1),
(n2)
, n*O(1) +
n*O(n), (n2)

?
, .

11