Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you...
-
Upload
kelley-garrison -
Category
Documents
-
view
214 -
download
0
description
Transcript of Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you...
![Page 1: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/1.jpg)
StacksQueues
Introduction to Trees
![Page 2: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/2.jpg)
Stacks
![Page 3: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/3.jpg)
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).
![Page 4: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/4.jpg)
In general...
• A stack can keep track of, “Where was I?”– Activation Stack– Compilers
ifif
if
endifendif
if
• Cafeterias use stacks: Plates, trays...
LB
![Page 5: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/5.jpg)
The Stack
Push Pop
![Page 6: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/6.jpg)
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
![Page 7: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/7.jpg)
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...
![Page 8: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/8.jpg)
Stacks:Dynamic Implementation
A linked list with restricted set of operations to change its state: only modified from one end
4
17
42
top
![Page 9: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/9.jpg)
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 Nodeendrecord // Node
![Page 10: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/10.jpg)
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 Numendrecord // Student_Rec
Node definesa Record data isoftype Student_Rec next isoftype Ptr toa Nodeendrecord // Node
Complex Node Definition
![Page 11: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/11.jpg)
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
![Page 12: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/12.jpg)
Push
• Create new node• Add it to the front
17
42
top
![Page 13: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/13.jpg)
Push
• Create new node• Add it to the front
42
17
top
4
temp
?
![Page 14: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/14.jpg)
Push
• Create new node• Add it to the front
42
17
top
4
temp
![Page 15: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/15.jpg)
Push
• Create new node• Add it to the front
4
42
17
top
![Page 16: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/16.jpg)
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 <- tempendprocedure // Push
![Page 17: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/17.jpg)
Pop
• Capture the first value (to return)• Remove the first node (move top to next)
4
42
17
top
![Page 18: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/18.jpg)
Pop
• Capture the first value (to return)• Remove the first node (move top to next)
4
42
17
top
value = 4
![Page 19: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/19.jpg)
Pop
• Capture the first value (to return)• Remove the first node (move top to next)
4
42
17
top
value = 4
![Page 20: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/20.jpg)
Pop
• Capture the first value (to return)• Remove the first node (move top to next)
42
17
top
value (4) is returned
![Page 21: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/21.jpg)
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 endifendprocedure // Pop
![Page 22: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/22.jpg)
Student’s Choice?
DoTrace
SkipTrace
![Page 23: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/23.jpg)
Algorithm Fragment.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
![Page 24: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/24.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
![Page 25: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/25.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
OK =N =
![Page 26: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/26.jpg)
top.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
OK =N =
![Page 27: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/27.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
OK =N =
![Page 28: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/28.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
topProcedure 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 <- tempendprocedure temp =
OK =N =
![Page 29: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/29.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
topProcedure 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 <- tempendprocedure
OK =N =
temp =
![Page 30: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/30.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
topProcedure 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 <- tempendprocedure
OK =N =
temp =
![Page 31: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/31.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
topProcedure 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 <- tempendprocedure
42
OK =N =
temp =
![Page 32: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/32.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
topProcedure 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 <- tempendprocedure
42
OK =N =
temp =
![Page 33: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/33.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
topProcedure 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 <- tempendprocedure
42
OK =N =
temp =
![Page 34: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/34.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
topProcedure 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 <- tempendprocedure
42
OK =N =
temp =
![Page 35: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/35.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
42
OK =N =
![Page 36: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/36.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 <- tempendprocedure
OK =N =
temp =
![Page 37: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/37.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 <- tempendprocedure
OK =N =
temp =
![Page 38: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/38.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 <- tempendprocedure
2
OK =N =
temp =
![Page 39: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/39.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 <- tempendprocedure
2
OK =N =
temp =
![Page 40: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/40.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 <- tempendprocedure
2
OK =N =
temp =
![Page 41: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/41.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 <- tempendprocedure
2
OK =N =
temp =
![Page 42: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/42.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
42
2
OK =N =
![Page 43: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/43.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 endifendprocedure
value =result =
![Page 44: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/44.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 endifendprocedure
value =result =
![Page 45: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/45.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 endifendprocedure
value =result = T
![Page 46: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/46.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 endifendprocedure
value = 2result = T
![Page 47: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/47.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 endifendprocedure
value = 2result = T
![Page 48: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/48.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)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 endifendprocedure
value = 2result = T
![Page 49: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/49.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
42
OK = TN = 2
![Page 50: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/50.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
42
OK = TN = 2
2
![Page 51: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/51.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
42
OK = TN = 2
7
![Page 52: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/52.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
42
OK = TN = 7
![Page 53: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/53.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
OK = TN = 42
![Page 54: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/54.jpg)
.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).
top
OK = TN = 42
![Page 55: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/55.jpg)
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)
![Page 56: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/56.jpg)
Questions?
![Page 57: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/57.jpg)
Queues
![Page 58: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/58.jpg)
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
![Page 59: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/59.jpg)
The Queue
Enqueue
Dequeue
![Page 60: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/60.jpg)
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
![Page 61: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/61.jpg)
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...
![Page 62: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/62.jpg)
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
tailhead
![Page 63: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/63.jpg)
Queue Record Definition
Queue definesa record head, tail isoftype Ptr toa Nodeendrecord // Queue
![Page 64: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/64.jpg)
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 Nodeendrecord // Node
![Page 65: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/65.jpg)
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 Numendrecord // Student_Rec
Node definesa Record data isoftype Student_Rec next isoftype Ptr toa Nodeendrecord // Node
Complex Node Definition
![Page 66: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/66.jpg)
Application Programmer Interfaceprocedure 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)
![Page 67: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/67.jpg)
Enqueue
• Create a new node with data• Add it to the end– Update pointers as needed
4 17
tailhead
![Page 68: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/68.jpg)
Enqueue
• Create a new node with data• Add it to the end– Update pointers as needed
4 17
tailhead42
temp
![Page 69: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/69.jpg)
Enqueue
• Create a new node with data• Add it to the end– Update pointers as needed
4 17
tailhead42
temp
![Page 70: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/70.jpg)
Enqueue
• Create a new node with data• Add it to the end– Update pointers as needed
4 17
tailhead
42
temp
![Page 71: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/71.jpg)
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 endifendprocedure // Enqueue
![Page 72: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/72.jpg)
Dequeue
• Capture the first value (to return)• Remove the first node (move head to next)
4 17
head
42
tail
![Page 73: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/73.jpg)
Dequeue
• Capture the first value (to return)• Remove the first node (move head to next)
value = 4
4 17
head
42
tail
![Page 74: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/74.jpg)
Dequeue
• Capture the first value (to return)• Remove the first node (move head to next)
value = 4
4 17
head
42
tail
![Page 75: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/75.jpg)
Dequeue
• Capture the first value (to return)• Remove the first node (move head to next)
value (4) is returned
17
head
42
tail
![Page 76: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/76.jpg)
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 endifendprocedure // Dequeue
![Page 77: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/77.jpg)
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 <- NILendprocedure // QInit
![Page 78: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/78.jpg)
Student’s Choice
DoTrace
SkipTrace
![Page 79: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/79.jpg)
Algorithm Fragment.myQ isoftype QueueOK isoftype BooleanN 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.
![Page 80: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/80.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
![Page 81: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/81.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
headtail
![Page 82: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/82.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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. OK N
![Page 83: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/83.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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. OK N
![Page 84: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/84.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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. OK N
procedure Qinit (Q isoftype out Queue) Q.head <- NIL Q.tail <- NILendprocedure
![Page 85: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/85.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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. OK N
procedure Qinit (Q isoftype out Queue) Q.head <- NIL Q.tail <- NILendprocedure
![Page 86: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/86.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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. OK N
procedure Qinit (Q isoftype out Queue) Q.head <- NIL Q.tail <- NILendprocedure
![Page 87: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/87.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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. OK N
![Page 88: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/88.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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. 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 endifendprocedure
![Page 89: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/89.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
![Page 90: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/90.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
![Page 91: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/91.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
![Page 92: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/92.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
42
![Page 93: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/93.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
42
![Page 94: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/94.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
42
![Page 95: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/95.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
42
![Page 96: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/96.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
42
![Page 97: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/97.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
42
![Page 98: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/98.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N
42
![Page 99: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/99.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
![Page 100: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/100.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
![Page 101: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/101.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
2
![Page 102: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/102.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
2
![Page 103: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/103.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
2
![Page 104: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/104.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
2
![Page 105: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/105.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
2
![Page 106: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/106.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure temp
2
![Page 107: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/107.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N
42
OK N
2
![Page 108: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/108.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure
![Page 109: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/109.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure
![Page 110: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/110.jpg)
42
2
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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. OK N OK T N 42
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 endifendprocedure
![Page 111: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/111.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure
![Page 112: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/112.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N
42
OK T N 42
2
Peeking backat calling program.
![Page 113: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/113.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure
![Page 114: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/114.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure
![Page 115: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/115.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
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 endifendprocedure
![Page 116: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/116.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N 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 endifendprocedure
![Page 117: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/117.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N 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 endifendprocedure
![Page 118: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/118.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N 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 endifendprocedure
![Page 119: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/119.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 42
2
![Page 120: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/120.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 42
2
![Page 121: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/121.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 42
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 endifendprocedure temp
![Page 122: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/122.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 42
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 endifendprocedure temp
7
![Page 123: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/123.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 42
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 endifendprocedure temp
7
![Page 124: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/124.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 42
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 endifendprocedure temp
7
![Page 125: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/125.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 42
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 endifendprocedure temp
7
![Page 126: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/126.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 42
2
7
![Page 127: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/127.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 2
7
![Page 128: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/128.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 2
7
![Page 129: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/129.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 2
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 endifendprocedure
![Page 130: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/130.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 2
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 endifendprocedure
![Page 131: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/131.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 2
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 endifendprocedure
![Page 132: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/132.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 2
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 endifendprocedure
![Page 133: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/133.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 2
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 endifendprocedure
![Page 134: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/134.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 2
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 endifendprocedure
![Page 135: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/135.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 2
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 endifendprocedure
![Page 136: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/136.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T 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 endifendprocedure
![Page 137: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/137.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T 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 endifendprocedure
![Page 138: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/138.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 7
![Page 139: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/139.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 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 endifendprocedure
![Page 140: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/140.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 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 endifendprocedure
![Page 141: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/141.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 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 endifendprocedure
![Page 142: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/142.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK N OK T N 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 endifendprocedure
![Page 143: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/143.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK F N OK N 7
![Page 144: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/144.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK F N OK N 7
myQ is empty
![Page 145: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/145.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK F N OK N 7
myQ is empty
![Page 146: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/146.jpg)
.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(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.
OK F N OK N 7
myQ is empty
![Page 147: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/147.jpg)
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)
![Page 148: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/148.jpg)
Questions?
![Page 149: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/149.jpg)
Introduction to Trees
![Page 150: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/150.jpg)
Trees
A non-linear, hierarchical collection with a “one to many” relationships
![Page 151: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/151.jpg)
Visual Representation of a Tree
Trees allow each node to have multiple successors
Parent
Child
![Page 152: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/152.jpg)
Tree Terminology
Parent
Child
Root
Leaf
![Page 153: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/153.jpg)
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.
![Page 154: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/154.jpg)
Binary Trees
Binary trees can have at most two successors.
![Page 155: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/155.jpg)
Binary Tree ExampleNo imposed ordering.
25
42 7
105 111312
6817
![Page 156: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/156.jpg)
Structure of a Binary Tree Node
<Type Name> definesa Record data isoftype <type> left_child isoftype Ptr toa <Type Name> right_child isoftype Ptr toa <Type Name>endrecord
left_child right_child
data
![Page 157: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/157.jpg)
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_Nodeendrecord // Tree_Node
![Page 158: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/158.jpg)
root 34
25
21 29
45
41 52
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
![Page 159: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/159.jpg)
Can a Tree be a BST and not a BST?
LB
![Page 160: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/160.jpg)
1100Bob
9494Sal
2102Abe
882Zak
904Ned
7856Tom
1234Ava
LB
![Page 161: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/161.jpg)
Node Depth in a Tree• Measures how far down a node is in the tree.• How many “ancestor” nodes “live above” the
node?
0123
![Page 162: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/162.jpg)
Balanced TreesRoughly symmetrical• There is a difference of a most one level of depth
among the various leaves
Balanced Not Balanced
![Page 163: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/163.jpg)
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
![Page 164: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/164.jpg)
Questions?
![Page 165: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.](https://reader036.fdocuments.us/reader036/viewer/2022081404/5a4d1b467f8b9ab0599a34fc/html5/thumbnails/165.jpg)