Abstract data type
O
over items of type
int
has the following interface (here written as Java code):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
It satisfies the following four properties for every instance
O o
and item
int i
.
o.Q()
holds, then
o.P(i)
establishes
o.S() == i
.
! o.Q()
holds, then
o.P(i)
preserves
o.S()
(that is,
o.S()
is invariant/unchanged).
o.Q()
holds, then
o.P(i); o.R()
preserves
o.Q()
.
! o.Q()
holds, then
o.P(i); o.R()
and
o.R(); o.P(i)
are equivalent.
What abstract data type is
O
?
Notes:
!
means "not" (boolean negation)
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Every valid sequence op
O
,P
,R
commands, starting with a constructor callO o = new O();
can be rewritten, according to the postulated properties (especially properties 3 and 4), into an equivalent sequence of the formO o = new O(); o.P(i_1); o.P(i_2); ...; o.P(i_k);
for some
k
at least 0. That is, allR
commands can be eliminated.If
k > 0
, then after that sequence the condition! o.Q()
holds, and furthermoreo.S()
will then returni_1
(see properties 1 and 2), and after the commando.R()
the resulting state will be equivalent toO o = new O(); o.P(i_2); ...; o.P(i_k);
That is, the command
o.P(i_1);
is "canceled".Obviously, after the command
o.P(i_x)
, the resulting state will be equivalent toO o = new O(); o.P(i_1); o.P(i_2); ...; o.P(i_k); o.P(i_x);
Thus, the state of
O o
is uniquely determined by a sequence of items[i_1, i_2, ..., i_k]
.This shows that
O
is a Queue , whereQ()
queries for emptinessS()
serves (return) the item at the front (without removing it; this is a pure query)P(i)
puts itemi
at the endR()
removes the item at the front (without returning anything; this is a pure command)Notes: