stacks queues introduction to trees
Download
Skip this Video
Download Presentation
Stacks Queues Introduction to Trees

Loading in 2 Seconds...

play fullscreen
1 / 165

Stacks Queues Introduction to Trees - PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on

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

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

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
an everyday example
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).

in general
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
The Stack

Push

Pop

properties
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 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
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
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
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
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

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

top

17

42

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

temp

top

42

4

17

?

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

temp

top

42

4

17

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

top

4

42

17

slide16
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

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

top

4

42

17

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

top

4

42

value = 4

17

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

top

4

42

value = 4

17

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

top

42

17

value (4) is returned

slide21
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
Student’s Choice?

Do

Trace

Skip

Trace

algorithm fragment
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)

.

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

.

slide25
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 =

slide26
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 =

slide27
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 =

slide28
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 =

slide29
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 =

slide30
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 =

slide31
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 =

slide32
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 =

slide33
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 =

slide34
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 =

slide35
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 =

slide36
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 =

slide37
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 =

slide38
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 =

slide39
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 =

slide40
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 =

slide41
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 =

slide42
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 =

slide43
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 =

slide44
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 =

slide45
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 =

slide46
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 =

slide47
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 =

slide48
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 =

slide49
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

slide50
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

slide51
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

slide52
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

slide53
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

slide54
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
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)
some examples
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
The Queue

Dequeue

Enqueue

properties of queues
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 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...
queues dynamic implementation
head

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 Record Definition

Queue definesa record

head,

tail isoftype Ptr toa Node

endrecord // Queue

defining the node type1
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 definition1
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 interface1
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
Enqueue
  • Create a new node with data
  • Add it to the end
    • Update pointers as needed

head

tail

17

4

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

temp

42

head

tail

17

4

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

temp

42

head

tail

17

4

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

tail

head

temp

42

17

4

slide71
procedure Enqueue (value isoftype in Num,

Q isoftype in/out Queue)

temp isoftype Ptr toa Node

temp <- new(Node)

temp^.data <- value

temp^.next <- NIL

if(Q.tail = NIL) then // was empty

Q.tail <- temp

Q.head <- temp

else // was not empty

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure // Enqueue

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

head

tail

42

17

4

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

value = 4

head

tail

42

17

4

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

value = 4

head

tail

42

17

4

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

value (4) is returned

head

tail

42

17

slide76
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

endif

endprocedure // Dequeue

initializing the queue
Initializing the Queue

procedure QInit (Q isoftype out Queue)

// Initializes the Q head and tail

// to point to nil, setting the Q to

// be empty

Q.head <- NIL

Q.tail <- NIL

endprocedure // QInit

student s choice1
Student’s Choice

Do

Trace

Skip

Trace

algorithm fragment1
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

.

slide80
.

myQ isoftype Queue

OK 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

.

slide81
.

myQ isoftype Queue

OK isoftype Boolean

N isoftype Num

Qinit(myQ)

Enqueue(42, myQ)

Enqueue(2, myQ)

Dequeue(N, myQ, OK)

Enqueue(7, myQ)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

Dequeue(N, myQ, OK)

if(NOT OK) then

print(“myQ is empty”)

endif

.

head

tail

slide82
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

.

slide83
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

.

slide84
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.head <- NIL

Q.tail <- NIL

endprocedure

slide85
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.head <- NIL

Q.tail <- NIL

endprocedure

slide86
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.head <- NIL

Q.tail <- NIL

endprocedure

slide87
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

.

slide88
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide89
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide90
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide91
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide92
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide93
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide94
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide95
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide96
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide97
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide98
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

.

slide99
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide100
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide101
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide102
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide103
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide104
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide105
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide106
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide107
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

.

slide108
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide109
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

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

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

endif

endprocedure

slide111
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

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

slide113
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide114
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide115
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide116
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide117
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide118
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide119
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

.

slide120
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

.

slide121
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide122
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide123
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide124
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide125
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

Q.head <- temp

else

Q.tail^.next <- temp

Q.tail <- temp

endif

endprocedure

slide126
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

.

slide127
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

.

slide128
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

.

slide129
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide130
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide131
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide132
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide133
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide134
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide135
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide136
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide137
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide138
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

.

slide139
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide140
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide141
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide142
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

value <- Q.head^.data

Q.head <- Q.head^.next

if(Q.head = NIL) then

Q.tail <- NIL

endif

endif

endprocedure

slide143
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

.

slide144
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

.

slide145
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

.

slide146
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
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)
trees
Trees

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

visual representation of a tree
Visual Representation of a Tree

Trees allow each node to have multiple successors

Parent

Child

tree terminology
Tree Terminology

Root

Parent

Child

Leaf

tree terminology1
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

Binary trees can have at most two successors.

binary tree example
Binary Tree Example

No imposed ordering.

25

42

7

12

3

105

111

17

68

structure of a binary tree node
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
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
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

slide160
1100

9494

2102

882

904

7856

1234

Bob

Sal

Abe

Zak

Ned

Tom

Ava

LB

node depth in a tree
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
Balanced Trees

Roughly symmetrical

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

Balanced

Not Balanced

summary
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
ad