Download presentation
Presentation is loading. Please wait.
Published byKelley Garrison Modified over 9 years ago
1
Stacks Queues Introduction to Trees
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.” We’ll end up doing the last item first (last in, first out).
4
In general... A stack can keep track of, “Where was I?” –Activation Stack –Compilers if endif if Cafeterias use stacks: Plates, trays... LB
5
The Stack Push Pop
6
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 Properties
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, we’ll focus on dynamic implementations using dynamic data...
8
Stacks: Dynamic Implementation A linked list with restricted set of operations to change its state: only modified from one end 4 17 42 top
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
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 Complex Node Definition
11
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 Application Programmer Interface
12
Push Create new node Add it to the front 17 42 top
13
Push Create new node Add it to the front 42 17 top 4 temp ?
14
Push Create new node Add it to the front 42 17 top 4 temp
15
Push Create new node Add it to the front 4 42 17 top
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 <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure // Push
17
Pop Capture the first value (to return) Remove the first node (move top to next) 4 42 17 top
18
Pop Capture the first value (to return) Remove the first node (move top to next) 4 42 17 top value = 4
19
Pop Capture the first value (to return) Remove the first node (move top to next) 4 42 17 top value = 4
20
Pop Capture the first value (to return) Remove the first node (move top to next) 42 17 top 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 <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure // Pop
22
Student’s Choice? Do Trace Skip Trace
23
Algorithm Fragment. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK).
24
top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top
25
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top OK = N =
26
top. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). OK = N =
27
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top OK = N =
28
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =
29
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure OK = N = temp =
30
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure OK = N = temp =
31
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure 42 OK = N = temp =
32
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure 42 OK = N = temp =
33
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure 42 OK = N = temp =
34
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure 42 OK = N = temp =
35
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 OK = N =
36
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure OK = N = temp =
37
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure OK = N = temp =
38
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure 2 OK = N = temp =
39
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure 2 OK = N = temp =
40
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure 2 OK = N = temp =
41
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure 2 OK = N = temp =
42
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 2 OK = N =
43
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 2 OK = N = Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = result =
44
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 2 OK = N = Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = result =
45
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 2 OK = N = Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = result = T
46
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 2 OK = N = Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = 2 result = T
47
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 2 OK = N = Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = 2 result = T
48
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 OK = N = Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = 2 result = T
49
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 OK = T N = 2
50
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 OK = T N = 2 2
51
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 OK = T N = 2 7
52
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 42 OK = T N = 7
53
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top OK = T N = 42
54
. top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK). top 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 Enqueue Dequeue
60
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 Properties of Queues
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, we’ll focus on dynamic implementations using dynamic data...
62
Queues: Dynamic 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. 4 17 42 tail head
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
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 Complex Node Definition
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 4 17 tail head
68
Enqueue Create a new node with data Add it to the end –Update pointers as needed 4 17 tail head 42 temp
69
Enqueue Create a new node with data Add it to the end –Update pointers as needed 4 17 tail head 42 temp
70
Enqueue Create a new node with data Add it to the end –Update pointers as needed 4 17 tail head 42 temp
71
procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then // was empty Q.tail <- temp Q.head <- temp else // was not empty Q.tail^.next <- temp Q.tail <- temp endif endprocedure // Enqueue
72
Dequeue Capture the first value (to return) Remove the first node (move head to next) 4 17 head 42 tail
73
Dequeue Capture the first value (to return) Remove the first node (move head to next) value = 4 4 17 head 42 tail
74
Dequeue Capture the first value (to return) Remove the first node (move head to next) value = 4 4 17 head 42 tail
75
Dequeue Capture the first value (to return) Remove the first node (move head to next) value (4) is returned 17 head 42 tail
76
procedure Dequeue (value iot out Num, Q iot in/out Queue, result iot out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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 <- NIL Q.tail <- NIL endprocedure // QInit
78
Student’s 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) 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) 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) 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) if(NOT OK) then print(“myQ is empty”) endif. OK N
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) if(NOT OK) then print(“myQ is empty”) endif. OK N
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Qinit (Q isoftype out Queue) Q.head <- NIL Q.tail <- 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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Qinit (Q isoftype out Queue) Q.head <- NIL Q.tail <- 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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Qinit (Q isoftype out Queue) Q.head <- NIL Q.tail <- 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) if(NOT OK) then print(“myQ is empty”) endif. OK N
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- 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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 42
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 42
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 42
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 42
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 42
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) if(NOT OK) then print(“myQ is empty”) endif. OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 42
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 2
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 2
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 2
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 2
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 2
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 2
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N 2
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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N 2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N 2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endprocedure
110
42 2. 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN42 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N 2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OKTN42 2 Peeking back at calling program. 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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N 2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N 2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N 42 OK N 2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N N 2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N N 2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N N 2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN42 2
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN42 2
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN42 2 procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN42 2 procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 7
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN42 2 procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 7
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN42 2 procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 7
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN42 2 procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endif endprocedure temp 7
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN42 2 7
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 7
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 7
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN2 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN7
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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OK N TN7 procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL 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) if(NOT OK) then print(“myQ is empty”) endif. OKFN N7
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) if(NOT OK) then print(“myQ is empty”) endif. OKFN N7 myQ is empty
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) if(NOT OK) then print(“myQ is empty”) endif. OKFN N7 myQ is empty
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) if(NOT OK) then print(“myQ is empty”) endif. OKFN N7 myQ is empty
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 Parent Child Root 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 427 105111312 6817
156
Structure of a Binary Tree Node definesa Record data isoftype left_child isoftype Ptr toa right_child isoftype Ptr toa endrecord left_childright_child data
157
Example Definition: Binary Tree Node Tree_Node definesa Record data isoftype Num left_child isoftype Ptr toa Tree_Node right_child isoftype Ptr toa Tree_Node endrecord // Tree_Node
158
root 34 25 2129 45 4152 Binary Search Trees For each node, the value stored in it is greater than the value in every node in the left sub-tree and less than the value in every node in the right sub-tree
159
Can a Tree be a BST and not a BST? LB
160
1100 Bob 9494 Sal 2102 Abe 882 Zak 904 Ned 7856 Tom 1234 Ava LB
161
Node Depth in a Tree Measures how far down a node is in the tree. How many “ancestor” nodes “live above” the node? 01230123
162
Balanced Trees Roughly symmetrical There is a difference of a most one level of depth among the various leaves Balanced Not Balanced
163
Summary Trees allow us to create nonlinear collections Binary Trees have at most two successors (children) Binary Search Trees impose structure –Every node’s value is greater than all the values in its left sub-tree –Every node’s value is less than all the values in its right sub-tree
164
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.