Stacks: Basic Stack Operations Linked-List Implementation Stack Applications Array Implementation
Stacks: Basic Stack Operations Linked-List Implementation Stack Applications Array Implementation
Slide 1
Restricted list: data can be inserted/deleted and operations are performed only at the ends of the list
Slide 2
Stack
All insertions and deletions are restricted to one end called the top Last-In First-Out (LIFO) data structure
Slide 3
Stack
Stack
Slide 4
Overflow
Data
Top
Stack
Slide 5
Pop
Data
Top Top
Stack Stack
Slide 6
Underflow
Top
Stack
Slide 7
Stack
Stack
Slide 8
Linked-List Implementation
Stack structure top 5
Top
Conceptual
Physical
Slide 9
Linked-List Implementation
stack count <integer> top <node pointer> end stack
node data <dataType> next <node pointer> end node
Stack structure
count
top
data
next
Slide 10
Create Stack
Before ?
count
After 0
count top
?
top
(no stack)
(empty stack)
Slide 11
Create Stack
Algorithm createStack (ref stack <metadata>)
3 return
End createStack
Slide 12
Push Stack
Before
pNew stack 2 count top blue data next red data next stack 3 count top blue data next
After
pNew red data next
Slide 13
Push Stack
Algorithm pushStack (ref stack <metadata>,
Pre
Post
Slide 14
Push Stack
1 if (stack full) Before 1 success = false pNew 2 else 1 allocate (pNew) stack 2 pNew -> data = data 2 3 pNew -> next = stack.top count top 4 stack.top = pNew 5 stack.count = stack.count + 1 6 success = true 3 return success End pushStack
Faculty of Computer Science and Engineering HCMUT
Slide 15
Pop Stack
Before
dltPtr stack red data next
After
dltPtr
recycled
data
next
stack blue
top data next
3
count
2
count top
blue
data next
Slide 16
Pop Stack
Algorithm popStack (ref stack <metadata>,
Pre
Post
Slide 17
Pop Stack
1 if (stack empty) Before 1 success = false 2 else dltPtr 1 dltPtr = stack.top 2 dataOut = stack.top -> data stack 3 stack.top = stack.top -> next 3 4 stack.count = stack.count - 1 count top 5 recycle (dltPtr) 6 success = true 3 return success End popStack
Faculty of Computer Science and Engineering HCMUT
green
data next
Slide 18
Stack Top
Algorithm stackTop (val stack <metadata>, ref dataOut <dataType>) Retrieves the data from the top of the stack without changing the stack Pre stack is a metadata structure to a valid stack dataOut is to receive top stack data Post data have been returned to caller Return true if successful; false
Slide 19
Stack Top
1 if (stack empty) 1 success = false 2 else 1 dataOut = stack.top -> data 2 success = true 3 return success End stackTop
Slide 20
Empty Stack
Algorithm emptyStack (val stack <metadata>) Determines if the stack is empty Pre stack is a metadata structure to a valid stack Post returns stack status Return true if the stack is empty; false otherwise 1 return (stack.count is 0) End emptyStack
Slide 21
Full Stack
Algorithm fullStack (val stack <metadata>) Determines if the stack is full Pre stack is a metadata structure to a valid stack Post returns stack status Return true if the stack is full; false otherwise 1 if (memory available) 1 result = false 2 else 1 result = true 3 return result End fullStack
Faculty of Computer Science and Engineering HCMUT Slide 22
Stack Count
Algorithm stackCount (val stack <metadata>) Returns the number of elements currently in the stack Pre stack is a metadata structure to a valid stack Post returns stack count Return integer count of the number of elements in the stack 1 return (stack.count) End stackCount
Slide 23
Destroy Stack
Algorithm destroyStack (ref stack <metadata>) Releases all nodes back to memory Pre stack is a metadata structure to a valid stack Post stack empty and all nodes recycled 1 if (stack not empty) 1 loop (stack.top not null) 1 temp = stack.top 2 stack.top = stack.top -> next 3 recycle (temp) 2 stack.count = 0 3 return End destroyStack
Faculty of Computer Science and Engineering HCMUT Slide 24
Stack Applications
Postponement of processing data items Reversing data items
Slide 25
Reverse a List
Algorithm reverseNumber Reverses a list of integers by pushing them in a stack and retrieving them one by one. 1 2 3 4 createStack (stack) prompt (Enter a number) read (number) Fill stack loop (not end of data AND not fullStack(stack)) 1 pushStack (stack, number) 2 prompt (Enter a number: <EOF> to stop) 3 read (number) Print numbers in reverse loop (not emptyStack(stack)) 1 popStack (stack, dataOut) 2 print (dataOut) Destroy stack and return stack = destroyStack (stack) Slide 26
createStack (stack) prompt (Enter a decimal to convert to binary) read (number) loop (number > 0) 1 digit = number modulo 2 2 pushOK = pushStack (stack, digit) 3 if (pushOK false) 1 print (Stack overflow) 2 quit algorithm 4 number = number / 2 Print binary number created in stack loop (not emptyStack(stack)) 1 popStack (stack, digit) 2 print (digit) Destroy stack and return stack = destroyStack (stack)
End decimalToBinary
Slide 27
Array Implementation
stack
stackAry count 3
Top
stackMax top 5 2
data [0]
data [1]
Conceptual
Physical
Slide 28
Array Implementation
stack stackAry <pointer to array of dataType> count <integer> stackMax <integer> top <index> end stack
Slide 29
Create Stack
Algorithm createStack (ref stack <metadata> val stackElem <integer>)
Allocates memory for data array and initializes metadata for a stack
Pre Post stack is a metadata structure to a valid stack stackElem is max size of stack data array allocated and metadata initialized
Slide 30
Create Stack
1 if (memory not available) 1 success = false 2 else 1 stack.count = 0 2 stack.top = -1 3 stack.stackMax = stackElem 4 allocate (stack.stackAry) 5 success = true 3 return success End createStack
Slide 31
Push Stack
Algorithm pushStack (ref stack <metadata>, val data <dataType>)
Post
Slide 32
Push Stack
1 if (stack.count is at maximum) 1 success = false 2 else 1 stack.count = stack.count + 1 2 stack.top = stack.top + 1 3 stack.stackAry[stack.top] = data 4 success = true 3 return success End pushStack
Slide 33
Pop Stack
Algorithm popStack (ref stack <metadata>, ref dataOut <dataType>)
Pops the item on the top of the stack and returns it to caller
Pre stack is a metadata structure to a valid stack dataOut is to receive the popped data
Slide 34
Pop Stack
1 if (stack empty) 1 success = false 2 else 1 dataOut = stack.stackAry[stack.top] 2 stack.top = stack.top - 1 3 stack.count = stack.count - 1 4 success = true 3 return success End popStack
Slide 35
Stack Top
Algorithm stackTop (val stack <metadata>, val dataOut <dataType>)
Retrieves the data from the top of the stack without changing the stack
Pre Post stack is a metadata structure to a valid stack dataOut is to receive top stack data data have been returned to caller
Slide 36
Stack Top
1 if (stack empty) 1 success = false 2 else 1 dataOut = stack.stackAry[stack.top] 2 success = true 3 return success end stackTop
Slide 37
Empty Stack
Algorithm emptyStack (val stack <metadata>) Determines if the stack is empty
Pre
Post
End emptyStack
Slide 38
Full Stack
Algorithm fullStack (val stack <metadata>) Determines if the stack is full
Pre
Post
Slide 39
Stack Count
Algorithm stackCount (val stack <metadata>) Returns the number of elements currently in the stack
Pre
Post
End stackCount
Slide 40
Destroy Stack
Algorithm destroyStack (ref stack <metadata>) Releases all nodes back to memory Pre stack is a metadata structure to a valid stack Post stack empty and stack array recycled 1 recycle (stack.stackAry) 2 stack.stackAry = null 3 stack.count = 0 4 stack.top = -1 5 stack.stackMax = 0 6 return End destroyStack
Faculty of Computer Science and Engineering HCMUT Slide 41
Reading
Drozdeks textbook:
Slide 42