Stacks Queues Introduction to Trees

1 / 165

# Stacks Queues Introduction to Trees - PowerPoint PPT Presentation

Lecture 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 last ‘rush’ item I gave you on hold; this is more important, so rush it out first.”

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Stacks Queues Introduction to Trees' - ishmael-olson

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Lecture 11

### Stacks

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

LBIn general...
• A stack can keep track of, “Where was I?”
• Activation Stack
• Compilers

if

if

if

endif

endif

if

• Cafeterias use stacks: Plates, trays...
The Stack

Push

Pop

Properties

Idea: a “Last In, First Out” (LIFO) data structure

Behaviors:

• Push: Add to top of stack
• Pop: Remove from top of stack (and return that top value)
• Top: Return topmost item (but leave it on the stack)
• Is_Full: is it full?
• Is_Empty: is it empty?
• Initialize: empty stack
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...
Stacks:Dynamic Implementation

A linked list with restricted set of operations to change its state: only modified from one end

top

4

17

42

Defining the Node Type

This is the simple data structure we will use in the following example.

Node definesa record

data isoftype Num

next isoftype Ptr toa Node

endrecord // Node

Complex Node Definition

Recall that the same code (with some small modifications) will work with more complex data structures such as is shown here:

Student_Rec definesa Record

Name isoftype String

SSN isoftype String

GPA isoftype Num

endrecord // Student_Rec

Node definesa Record

data isoftype Student_Rec

next isoftype Ptr toa Node

endrecord // Node

Application Programmer Interface

procedure Push (value isoftype in Num,

top isoftype in/out Ptr toa Node)

// Purpose: push one value onto stack

// Pre: top points to NIL-terminated list

// Post: the list has one node added

procedure Pop (value isoftype out Num,

top isoftype in/out Ptr toa Node,

result isoftype out Boolean)

// Pop a value off stack; if empty, result

// contains FALSE (value will be undefined)

// Pre: top points to a NIL-terminated list

// Post: list has one fewer, value is old data

Push
• Create new node
• Add it to the front

top

17

42

Push
• Create new node
• Add it to the front

temp

top

42

4

17

?

Push
• Create new node
• Add it to the front

temp

top

42

4

17

Push
• Create new node
• Add it to the front

top

4

42

17

Push

Procedure Push (value isoftype in Num,

top isoftype in/out Ptr toa Node)

// Push one value onto stack

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure // Push

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

top

4

42

17

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

top

4

42

value = 4

17

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

top

4

42

value = 4

17

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

top

42

17

value (4) is returned

Pop

procedure Pop (value isoftype out Num,

top isoftype in/out Ptr toa Node,

result isoftype out Boolean)

// Pop an element off the stack

if(top = NIL) then

result <- FALSE

else

result <- TRUE

value <- top^.data

top <- top^.next

endif

endprocedure // Pop

Student’s Choice?

Do

Trace

Skip

Trace

Algorithm Fragment

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Push

(value isoftype in Num,

top isoftype

in/out Ptr toa Node)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- top

top <- temp

endprocedure

temp =

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Pop

value isoftype out Num,

top isoftype

in/out Ptr toa Node,

result isoftype out Boolean)

if(top = NIL) then

result <- FALSE

else

result <- TRUE

value <- top^.data

top <- top^.next

endif

endprocedure

value =

result =

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Pop

value isoftype out Num,

top isoftype

in/out Ptr toa Node,

result isoftype out Boolean)

if(top = NIL) then

result <- FALSE

else

result <- TRUE

value <- top^.data

top <- top^.next

endif

endprocedure

value =

result =

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Pop

value isoftype out Num,

top isoftype

in/out Ptr toa Node,

result isoftype out Boolean)

if(top = NIL) then

result <- FALSE

else

result <- TRUE

value <- top^.data

top <- top^.next

endif

endprocedure

value =

result = T

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Pop

value isoftype out Num,

top isoftype

in/out Ptr toa Node,

result isoftype out Boolean)

if(top = NIL) then

result <- FALSE

else

result <- TRUE

value <- top^.data

top <- top^.next

endif

endprocedure

value = 2

result = T

OK =

N =

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Pop

value isoftype out Num,

top isoftype

in/out Ptr toa Node,

result isoftype out Boolean)

if(top = NIL) then

result <- FALSE

else

result <- TRUE

value <- top^.data

top <- top^.next

endif

endprocedure

value = 2

result = T

OK =

N =

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

Procedure Pop

value isoftype out Num,

top isoftype

in/out Ptr toa Node,

result isoftype out Boolean)

if(top = NIL) then

result <- FALSE

else

result <- TRUE

value <- top^.data

top <- top^.next

endif

endprocedure

value = 2

result = T

OK =

N =

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 2

42

2

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 2

7

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 7

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 42

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)

### Queues

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

The Queue

Dequeue

Enqueue

Properties of Queues

Idea: a “First In, First Out” (FIFO) data structure

Behaviors:

• Enqueue: Add to end of queue
• Dequeue: Remove from front of queue (and return that front value)
• Front: Return front-most item (but leave it in the queue)
• Is_Full: is it full?
• Is_Empty: is it empty?
• Initialize: empty queue
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...

tail

17

42

4

Queues:Dynamic Implementation

A linked list with restricted set of operations to change its state: only modified byadding to one end and deleting from the other.

Queue Record Definition

Queue definesa record

tail isoftype Ptr toa Node

endrecord // Queue

Defining the Node Type

This is the simple data structure we will use in the following example.

Node definesa record

data isoftype Num

next isoftype Ptr toa Node

endrecord // Node

Complex Node Definition

Recall that the same code (with some small modifications) will work with more complex data structures such as is shown here:

Student_Rec definesa Record

Name isoftype String

SSN isoftype String

GPA isoftype Num

endrecord // Student_Rec

Node definesa Record

data isoftype Student_Rec

next isoftype Ptr toa Node

endrecord // Node

Application Programmer Interface

procedure Enqueue (value isoftype in Num,

Q isoftype in/out Queue)

// Pre: Q is initialized

// Purpose: Add a value to the tail

// Post: Q has new element at tail

procedure Dequeue (value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

// Pre: Q is initialized

// Purpose: Remove first value from Q and

// return it via ‘value’ OR indicate

// that Q is empty via ‘result’

// Post: element that was at head has been

// removed OR (result = FALSE)

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

tail

17

4

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

temp

42

tail

17

4

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

temp

42

tail

17

4

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

tail

temp

42

17

4

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

else // was not empty

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure // Enqueue

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

tail

42

17

4

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

value = 4

tail

42

17

4

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

value = 4

tail

42

17

4

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

value (4) is returned

tail

42

17

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

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure // Dequeue

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.tail <- NIL

endprocedure // QInit

Student’s Choice

Do

Trace

Skip

Trace

Algorithm Fragment

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

tail

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Qinit

(Q isoftype out Queue)

Q.tail <- NIL

endprocedure

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Qinit

(Q isoftype out Queue)

Q.tail <- NIL

endprocedure

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Qinit

(Q isoftype out Queue)

Q.tail <- NIL

endprocedure

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

42

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

42

2

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

42

2

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

Peeking back

at calling program.

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

2

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

2

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

2

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

2

7

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

2

7

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

2

7

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

2

7

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Enqueue

(value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then

Q.tail <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

2

7

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

procedure Dequeue

(value isoftype out Num,

Q isoftype in/out Queue,

result isoftype out Boolean)

if(Q.head = NIL) then

result <- FALSE

else

result <- TRUE

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

OK

OK

F

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

myQ is empty

OK

OK

F

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

myQ is empty

OK

OK

F

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

myQ is empty

OK

OK

F

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

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)

### Introduction to Trees

Trees

A non-linear, hierarchical collection with a “one to many” relationships

Visual Representation of a Tree

Trees allow each node to have multiple successors

Parent

Child

Tree Terminology

Root

Parent

Child

Leaf

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.
Binary Trees

Binary trees can have at most two successors.

Binary Tree Example

No imposed ordering.

25

42

7

12

3

105

111

17

68

Structure of a Binary Tree Node

definesa Record

data isoftype

left_child isoftype Ptr toa

right_child isoftype Ptr toa

endrecord

data

left_child

right_child

Example Definition: Binary Tree Node

Tree_Node definesa Record

data isoftype Num

left_child isoftype Ptr toa Tree_Node

right_child isoftype Ptr toa Tree_Node

endrecord // Tree_Node

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

root

34

25

45

21

29

41

52

LB

### Can a Tree be a BST and not a BST?

1100

9494

2102

882

904

7856

1234

Bob

Sal

Abe

Zak

Ned

Tom

Ava

LB

Node Depth in a Tree
• Measures how far down a node is in the tree.
• How many “ancestor” nodes “live above” the node?

0

1

2

3

Balanced Trees

Roughly symmetrical

• There is a difference of a most one level of depth among the various leaves

Balanced

Not Balanced

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