1.65k likes | 1.67k Vues
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.”
E N D
Lecture 11 StacksQueuesIntroduction to Trees
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).
LB In 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