Title: Stacks Queues Introduction to Trees
1StacksQueuesIntroduction to Trees
Lecture 11
2Stacks
3An Everyday Example
- Your boss keeps bringing you important items to
deal with and keeps saying - Put that last rush item I gave you on hold
this is more important, so rush it out first. - Well end up doing the last item first(last in,
first out).
4In general...
LB
- A stack can keep track of, Where was I?
- Activation Stack
- Compilers
- if
- if
- if
- endif
- endif
- if
- Cafeterias use stacks Plates, trays...
5The Stack
Push
Pop
6Properties
- Idea a Last In, First Out (LIFO) data
structure - Behaviors
- Push Add to top of stack
- Pop Remove from top of stack (and return that
top value) - Top Return topmost item (but leave it on the
stack) - Is_Full is it full?
- Is_Empty is it empty?
- Initialize empty stack
7The Stack as a Logical Data Structure
- The stack is an idea
- It implies a set of logical behaviors
- It can be implemented various ways
- Using a linked list or a tree or an array
- In this example, well focus on dynamic
implementations using dynamic data...
8StacksDynamic Implementation
- A linked list with restricted set of operations
to change its state only modified from one end
top
4
17
42
9Defining the Node Type
- This is the simple data structure we will use in
the following example. - Node definesa record
- data isoftype Num
- next isoftype Ptr toa Node
- endrecord // Node
10Complex Node Definition
- Recall that the same code (with some small
modifications) will work with more complex data
structures such as is shown here - Student_Rec definesa Record
- Name isoftype String
- SSN isoftype String
- GPA isoftype Num
- endrecord // Student_Rec
- Node definesa Record
- data isoftype Student_Rec
- next isoftype Ptr toa Node
- endrecord // Node
11Application Programmer Interface
- procedure Push (value isoftype in Num,
- top isoftype in/out Ptr toa Node)
- // Purpose push one value onto stack
- // Pre top points to NIL-terminated list
- // Post the list has one node added
- procedure Pop (value isoftype out Num,
- top isoftype in/out Ptr toa Node,
- result isoftype out Boolean)
- // Pop a value off stack if empty, result
- // contains FALSE (value will be undefined)
- // Pre top points to a NIL-terminated list
- // Post list has one fewer, value is old data
12Push
- Create new node
- Add it to the front
top
17
42
13Push
- Create new node
- Add it to the front
temp
top
42
4
17
?
14Push
- Create new node
- Add it to the front
temp
top
42
4
17
15Push
- Create new node
- Add it to the front
top
4
42
17
16Push
- Procedure Push (value isoftype in Num,
- top isoftype in/out Ptr toa Node)
- // Push one value onto stack
- temp isoftype Ptr toa Node
- temp lt- new(Node)
- temp.data lt- value
- temp.next lt- top
- top lt- temp
- endprocedure // Push
17Pop
- Capture the first value (to return)
- Remove the first node (move top to next)
top
4
42
17
18Pop
- Capture the first value (to return)
- Remove the first node (move top to next)
top
4
42
17
value 4
19Pop
- Capture the first value (to return)
- Remove the first node (move top to next)
top
4
42
value 4
17
20Pop
- Capture the first value (to return)
- Remove the first node (move top to next)
top
42
17
value (4) is returned
21Pop
- procedure Pop (value isoftype out Num,
- top isoftype in/out Ptr toa Node,
- result isoftype out Boolean)
- // Pop an element off the stack
- if(top NIL) then
- result lt- FALSE
- else
- result lt- TRUE
- value lt- top.data
- top lt- top.next
- endif
- endprocedure // Pop
22Students Choice?
Do Trace
Skip Trace
23Algorithm Fragment
- .
- top isoftype Ptr toa Node
- OK isoftype Boolean
- N isoftype Num
- top lt- NIL
- Push(42, top)
- Push(2, top)
- Pop(N, top, OK)
- if(OK) then
- print(N)
- endif
- Push(7, top)
- Pop(N, top, OK)
- Pop(N, top, OK)
- .
24top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
25top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK N
26top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK N
27top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK N
28top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
29top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
30top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
31top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
32top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
33top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
34top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
35top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK N
36top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
37top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
38top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
39top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
40top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
41top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Push (value isoftype in Num,
top isoftype in/out Ptr toa Node) temp
isoftype Ptr toa Node temp lt- new(Node)
temp.data lt- value temp.next lt- top top lt-
temp endprocedure
temp
OK N
42top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK N
43top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Pop value isoftype out Num, top
isoftype in/out Ptr toa Node, result
isoftype out Boolean) if(top NIL) then
result lt- FALSE else result lt- TRUE value lt-
top.data top lt- top.next endif endprocedure
value result
OK N
44top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Pop value isoftype out Num, top
isoftype in/out Ptr toa Node, result
isoftype out Boolean) if(top NIL) then
result lt- FALSE else result lt- TRUE value lt-
top.data top lt- top.next endif endprocedure
value result
OK N
45top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Pop value isoftype out Num, top
isoftype in/out Ptr toa Node, result
isoftype out Boolean) if(top NIL) then
result lt- FALSE else result lt- TRUE value lt-
top.data top lt- top.next endif endprocedure
value result T
OK N
46top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Pop value isoftype out Num, top
isoftype in/out Ptr toa Node, result
isoftype out Boolean) if(top NIL) then
result lt- FALSE else result lt- TRUE value lt-
top.data top lt- top.next endif endprocedure
value 2 result T
OK N
47top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Pop value isoftype out Num, top
isoftype in/out Ptr toa Node, result
isoftype out Boolean) if(top NIL) then
result lt- FALSE else result lt- TRUE value lt-
top.data top lt- top.next endif endprocedure
value 2 result T
OK N
48top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
Procedure Pop value isoftype out Num, top
isoftype in/out Ptr toa Node, result
isoftype out Boolean) if(top NIL) then
result lt- FALSE else result lt- TRUE value lt-
top.data top lt- top.next endif endprocedure
value 2 result T
OK N
49top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK T N 2
50top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK T N 2
51top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK T N 2
52top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK T N 7
53top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK T N 42
54top
. top isoftype Ptr toa Node OK isoftype Boolean N
isoftype Num top lt- NIL Push(42, top) Push(2,
top) Pop(N, top, OK) if(OK) then
print(N) endif Push(7, top) Pop(N, top,
OK) Pop(N, top, OK) .
OK T N 42
55Summary Stack
- Allow us to model last-in, first-out (LIFO)
behavior - Can be implemented using different data types
- Behavior is important (and defines a Stack)
- Push to the front
- Pop from the front
- (from the same end)
56Questions?
57Queues
58Some Examples
- Waiting in line
- At the grocery store
- At the movies
- Printer queue
- Ordering items
- Bills to pay
- Making pizzas
- We can use a queue to model each of these.
LB
59The Queue
Dequeue
Enqueue
60Properties of Queues
- Idea a First In, First Out (FIFO) data
structure - Behaviors
- Enqueue Add to end of queue
- Dequeue Remove from front of queue (and return
that front value) - Front Return front-most item (but leave it in
the queue) - Is_Full is it full?
- Is_Empty is it empty?
- Initialize empty queue
61The Queue as a Logical Data Structure
- The queue is an idea
- It implies a set of logical behaviors
- It can be implemented various ways
- Using a linked list or a tree or an array
- In this example, well focus on dynamic
implementations using dynamic data...
62QueuesDynamic Implementation
- A linked list with restricted set of operations
to change its state only modified by adding to
one end and deleting from the other.
63Queue Record Definition
- Queue definesa record
- head,
- tail isoftype Ptr toa Node
- endrecord // Queue
64Defining the Node Type
- This is the simple data structure we will use in
the following example. - Node definesa record
- data isoftype Num
- next isoftype Ptr toa Node
- endrecord // Node
65Complex Node Definition
- Recall that the same code (with some small
modifications) will work with more complex data
structures such as is shown here - Student_Rec definesa Record
- Name isoftype String
- SSN isoftype String
- GPA isoftype Num
- endrecord // Student_Rec
- Node definesa Record
- data isoftype Student_Rec
- next isoftype Ptr toa Node
- endrecord // Node
66Application Programmer Interface
- procedure Enqueue (value isoftype in Num,
- Q isoftype in/out Queue)
- // Pre Q is initialized
- // Purpose Add a value to the tail
- // Post Q has new element at tail
- procedure Dequeue (value isoftype out Num,
- Q isoftype in/out Queue,
- result isoftype out Boolean)
- // Pre Q is initialized
- // Purpose Remove first value from Q and
- // return it via value OR indicate
- // that Q is empty via result
- // Post element that was at head has been
- // removed OR (result FALSE)
67Enqueue
- Create a new node with data
- Add it to the end
- Update pointers as needed
head
tail
17
4
68Enqueue
- Create a new node with data
- Add it to the end
- Update pointers as needed
temp
42
head
tail
17
4
69Enqueue
- Create a new node with data
- Add it to the end
- Update pointers as needed
temp
42
head
tail
17
4
70Enqueue
- Create a new node with data
- Add it to the end
- Update pointers as needed
tail
head
temp
42
17
4
71- procedure Enqueue (value isoftype in Num,
- Q isoftype in/out Queue)
- temp isoftype Ptr toa Node
- temp lt- new(Node)
- temp.data lt- value
- temp.next lt- NIL
- if(Q.tail NIL) then // was empty
- Q.tail lt- temp
- Q.head lt- temp
- else // was not empty
- Q.tail.next lt- temp
- Q.tail lt- temp
- endif
- endprocedure // Enqueue
72Dequeue
- Capture the first value (to return)
- Remove the first node (move head to next)
head
tail
42
17
4
73Dequeue
- Capture the first value (to return)
- Remove the first node (move head to next)
- value 4
head
tail
42
17
4
74Dequeue
- Capture the first value (to return)
- Remove the first node (move head to next)
- value 4
head
tail
42
17
4
75Dequeue
- Capture the first value (to return)
- Remove the first node (move head to next)
- value (4) is returned
head
tail
42
17
76- procedure Dequeue (value iot out Num,
- Q iot in/out Queue,
- result iot out Boolean)
- if(Q.head NIL) then
- result lt- FALSE
- else
- result lt- TRUE
- value lt- Q.head.data
- Q.head lt- Q.head.next
- if(Q.head NIL) then
- Q.tail lt- NIL
- endif
- endif
- endprocedure // Dequeue
77Initializing the Queue
- procedure QInit (Q isoftype out Queue)
- // Initializes the Q head and tail
- // to point to nil, setting the Q to
- // be empty
- Q.head lt- NIL
- Q.tail lt- NIL
- endprocedure // QInit
78Students Choice
Do Trace
Skip Trace
79Algorithm Fragment
- .
- myQ isoftype Queue
- OK isoftype Boolean
- N isoftype Num
- Qinit(myQ)
- Enqueue(42, myQ)
- Enqueue(2, myQ)
- Dequeue(N, myQ, OK)
- Enqueue(7, myQ)
- Dequeue(N, myQ, OK)
- Dequeue(N, myQ, OK)
- Dequeue(N, myQ, OK)
- if(NOT OK) then
- print(myQ is empty)
- endif
- .
80. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
81. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
head
tail
82. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
83. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
84. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Qinit (Q isoftype out Queue)
Q.head lt- NIL Q.tail lt- NIL endprocedure
85. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Qinit (Q isoftype out Queue)
Q.head lt- NIL Q.tail lt- NIL endprocedure
86. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Qinit (Q isoftype out Queue)
Q.head lt- NIL Q.tail lt- NIL endprocedure
87. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
88. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num, Q
isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
89. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
90. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
91. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
92. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
93. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
94. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
95. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
96. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
97. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
98. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
99. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
100. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
101. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
102. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
103. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
104. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
105. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
106. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
107. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
108. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
109. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
110. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
111. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
112. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
Peeking back at calling program.
113. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
114. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
115. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
116. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
117. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
118. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
119. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
120. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
121. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
122. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
123. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
124. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
125. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Enqueue (value isoftype in Num,
Q isoftype in/out Queue) temp isoftype Ptr toa
Node temp lt- new(Node) temp.data lt- value
temp.next lt- NIL if(Q.tail NIL) then Q.tail
lt- temp Q.head lt- temp else Q.tail.next lt-
temp Q.tail lt- temp endif endprocedure
126. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
127. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
128. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
129. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
130. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
131. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
132. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
133. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
134. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
135. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
136. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
137. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
138. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
139. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
140. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
141. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
142. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
procedure Dequeue (value isoftype out Num, Q
isoftype in/out Queue, result isoftype out
Boolean) if(Q.head NIL) then result lt-
FALSE else result lt- TRUE value lt-
Q.head.data Q.head lt- Q.head.next if(Q.head
NIL) then Q.tail lt- NIL endif
endif endprocedure
143. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
144. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
145. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
146. myQ isoftype Queue OK isoftype Boolean N
isoftype Num Qinit(myQ) Enqueue(42,
myQ) Enqueue(2, myQ) Dequeue(N, myQ,
OK) Enqueue(7, myQ) Dequeue(N, myQ,
OK) Dequeue(N, myQ, OK) Dequeue(N, myQ,
OK) if(NOT OK) then print(myQ is empty) endif .
147Summary Queues
- Allow us to model first-in, first-out (FIFO)
behavior - Can be implemented using different data types
- Behavior is important (and defines a Queue)
- Enqueue to end
- Dequeue from front
- (or vice-versa just do at opposite ends)
148Questions?
149Introduction to Trees
150Trees
- A non-linear, hierarchical collection with a one
to many relationships
151Visual Representation of a Tree
- Trees allow each node to have multiple successors
Parent
Child
152Tree Terminology
Root
Parent
Child
Leaf
153Tree Terminology
- The first node is called the root.
- Successors are called children
- A parent node points to a child node.
- Nodes with no children are called leaves.
154Binary Trees
- Binary trees can have at most two successors.
155Binary Tree Example
25
42
7
105
111
3
12
68
17
156Structure of a Binary Tree Node
- ltType Namegt definesa Record
- data isoftype lttypegt
- left_child isoftype Ptr toa ltType Namegt
- right_child isoftype Ptr toa ltType Namegt
- endrecord
data
left_child
right_child
157Example Definition Binary Tree Node