Stacks Queues Introduction to Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Stacks Queues Introduction to Trees

Description:

Stacks Queues Introduction to Trees – PowerPoint PPT presentation

Number of Views:146
Avg rating:3.0/5.0
Slides: 166
Provided by: ccGatechE8
Category:

less

Transcript and Presenter's Notes

Title: Stacks Queues Introduction to Trees


1
StacksQueuesIntroduction to Trees
Lecture 11
2
Stacks
3
An 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).

4
In 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...

5
The Stack
Push
Pop
6
Properties
  • 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

7
The 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...

8
StacksDynamic Implementation
  • A linked list with restricted set of operations
    to change its state only modified from one end

top
4
17
42
9
Defining 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

10
Complex 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

11
Application 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

12
Push
  • Create new node
  • Add it to the front

top
17
42
13
Push
  • Create new node
  • Add it to the front

temp
top
42
4
17
?
14
Push
  • Create new node
  • Add it to the front

temp
top
42
4
17
15
Push
  • Create new node
  • Add it to the front

top
4
42
17
16
Push
  • 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

17
Pop
  • Capture the first value (to return)
  • Remove the first node (move top to next)

top
4
42
17
18
Pop
  • Capture the first value (to return)
  • Remove the first node (move top to next)

top
4
42
17
value 4
19
Pop
  • Capture the first value (to return)
  • Remove the first node (move top to next)

top
4
42
value 4
17
20
Pop
  • Capture the first value (to return)
  • Remove the first node (move top to next)

top
42
17
value (4) is returned
21
Pop
  • 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

22
Students Choice?
Do Trace
Skip Trace
23
Algorithm 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)
  • .

24
top
. 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) .
25
top
. 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
26
top
. 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
27
top
. 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
28
top
. 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
29
top
. 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
30
top
. 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
31
top
. 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
32
top
. 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
33
top
. 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
34
top
. 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
35
top
. 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
36
top
. 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
37
top
. 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
38
top
. 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
39
top
. 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
40
top
. 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
41
top
. 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
42
top
. 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
43
top
. 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
44
top
. 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
45
top
. 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
46
top
. 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
47
top
. 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
48
top
. 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
49
top
. 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
50
top
. 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
51
top
. 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
52
top
. 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
53
top
. 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
54
top
. 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
55
Summary 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)

56
Questions?
57
Queues
58
Some 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
59
The Queue
Dequeue
Enqueue
60
Properties 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

61
The 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...

62
QueuesDynamic 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.

63
Queue Record Definition
  • Queue definesa record
  • head,
  • tail isoftype Ptr toa Node
  • endrecord // Queue

64
Defining 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

65
Complex 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

66
Application 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)

67
Enqueue
  • Create a new node with data
  • Add it to the end
  • Update pointers as needed

head
tail
17
4
68
Enqueue
  • Create a new node with data
  • Add it to the end
  • Update pointers as needed

temp
42
head
tail
17
4
69
Enqueue
  • Create a new node with data
  • Add it to the end
  • Update pointers as needed

temp
42
head
tail
17
4
70
Enqueue
  • 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

72
Dequeue
  • Capture the first value (to return)
  • Remove the first node (move head to next)

head
tail
42
17
4
73
Dequeue
  • Capture the first value (to return)
  • Remove the first node (move head to next)
  • value 4

head
tail
42
17
4
74
Dequeue
  • Capture the first value (to return)
  • Remove the first node (move head to next)
  • value 4

head
tail
42
17
4
75
Dequeue
  • 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

77
Initializing 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

78
Students Choice
Do Trace
Skip Trace
79
Algorithm 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 .
147
Summary 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)

148
Questions?
149
Introduction to Trees
150
Trees
  • A non-linear, hierarchical collection with a one
    to many relationships

151
Visual Representation of a Tree
  • Trees allow each node to have multiple successors

Parent
Child
152
Tree Terminology
Root
Parent
Child
Leaf
153
Tree 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.

154
Binary Trees
  • Binary trees can have at most two successors.

155
Binary Tree Example
  • No imposed ordering.

25
42
7
105
111
3
12
68
17
156
Structure 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
157
Example Definition Binary Tree Node
  • Tree_No
Write a Comment
User Comments (0)
About PowerShow.com