0% found this document useful (0 votes)
65 views42 pages

Stacks: Basic Stack Operations Linked-List Implementation Stack Applications Array Implementation

The document discusses stacks and their implementation using both linked lists and arrays. It provides algorithms for basic stack operations like push, pop, and peek. It also covers stack applications like reversing a list and converting decimals to binary. Slides include pseudocode for creating, manipulating and destroying stacks as well as diagrams illustrating linked list and array implementations.

Uploaded by

Hai Nguyen Duc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views42 pages

Stacks: Basic Stack Operations Linked-List Implementation Stack Applications Array Implementation

The document discusses stacks and their implementation using both linked lists and arrays. It provides algorithms for basic stack operations like push, pop, and peek. It also covers stack applications like reversing a list and converting decimals to binary. Slides include pseudocode for creating, manipulating and destroying stacks as well as diagrams illustrating linked list and array implementations.

Uploaded by

Hai Nguyen Duc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Stacks

Basic stack operations Linked-list implementation Stack applications Array implementation

Faculty of Computer Science and Engineering HCMUT

Slide 1

Linear List Concepts


General list: no restrictions on where data can be inserted/deleted, and on which operations can be used on the list

Restricted list: data can be inserted/deleted and operations are performed only at the ends of the list

Faculty of Computer Science and Engineering HCMUT

Slide 2

Stack
All insertions and deletions are restricted to one end called the top Last-In First-Out (LIFO) data structure

Faculty of Computer Science and Engineering HCMUT

Slide 3

Basic Stack Operations


Push Data
Top Top

Stack

Stack

Faculty of Computer Science and Engineering HCMUT

Slide 4

Basic Stack Operations

Overflow

Data

Top

Stack

Faculty of Computer Science and Engineering HCMUT

Slide 5

Basic Stack Operations

Pop
Data

Top Top
Stack Stack

Faculty of Computer Science and Engineering HCMUT

Slide 6

Basic Stack Operations

Underflow

Top

Stack

Faculty of Computer Science and Engineering HCMUT

Slide 7

Basic Stack Operations

Stack Top Data


Top Top

Stack

Stack

Faculty of Computer Science and Engineering HCMUT

Slide 8

Linked-List Implementation
Stack structure top 5

Top

Conceptual

Physical

Faculty of Computer Science and Engineering HCMUT

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

Stack node structure

data

next

Faculty of Computer Science and Engineering HCMUT

Slide 10

Create Stack

Before ?
count

After 0
count top

?
top

(no stack)

(empty stack)

Faculty of Computer Science and Engineering HCMUT

Slide 11

Create Stack
Algorithm createStack (ref stack <metadata>)

Initializes metadata for a stack


Pre stack is structure for metadata

Post metadata initialized


1 stack.count = 0 2 stack.top = null

3 return
End createStack

Faculty of Computer Science and Engineering HCMUT

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

green data next

green data next

Faculty of Computer Science and Engineering HCMUT

Slide 13

Push Stack
Algorithm pushStack (ref stack <metadata>,

val data <dataType>)


Inserts (pushes) one item into the stack

Pre
Post

stack is a metadata structure to a valid stack


data contains data to be pushed into stack data have been pushed in stack

Return true if successful; false if memory overflow

Faculty of Computer Science and Engineering HCMUT

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

red data next

blue data next

green data next

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

green data next

green data next

Faculty of Computer Science and Engineering HCMUT

Slide 16

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
Post

stack is a metadata structure to a valid stack


dataOut is to receive the popped data data have been returned to caller

Return true if successful; false if underflow

Faculty of Computer Science and Engineering HCMUT

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

red data next

blue data next

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

Faculty of Computer Science and Engineering HCMUT

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

Faculty of Computer Science and Engineering HCMUT

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

Faculty of Computer Science and Engineering HCMUT

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

Faculty of Computer Science and Engineering HCMUT

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

Faculty of Computer Science and Engineering HCMUT

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

End reverseNumber Faculty of Computer Science and Engineering HCMUT

Convert Decimal to Binary


Algorithm 1 2 3 4 decimalToBinary
Reads an integer and prints its binary equivalent form.

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

Faculty of Computer Science and Engineering HCMUT

Slide 27

Array Implementation
stack
stackAry count 3
Top

stackMax top 5 2

data [0]

data [1]

data [2] [3] [4]

Conceptual

Physical

Faculty of Computer Science and Engineering HCMUT

Slide 28

Array Implementation
stack stackAry <pointer to array of dataType> count <integer> stackMax <integer> top <index> end stack

Faculty of Computer Science and Engineering HCMUT

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

Return true if successful; false if no memory

Faculty of Computer Science and Engineering HCMUT

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

Faculty of Computer Science and Engineering HCMUT

Slide 31

Push Stack
Algorithm pushStack (ref stack <metadata>, val data <dataType>)

Inserts (pushes) one item into the stack


Pre stack is a metadata structure to a valid stack data contains data to be pushed into stack

Post

data have been pushed in stack

Return true if successful; false if memory overflow

Faculty of Computer Science and Engineering HCMUT

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

Faculty of Computer Science and Engineering HCMUT

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

Post data have been returned to caller


Returntrue if successful; false if underflow

Faculty of Computer Science and Engineering HCMUT

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

Faculty of Computer Science and Engineering HCMUT

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

Return true if successful; false if underflow

Faculty of Computer Science and Engineering HCMUT

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

Faculty of Computer Science and Engineering HCMUT

Slide 37

Empty Stack
Algorithm emptyStack (val stack <metadata>) Determines if the stack is empty

Pre
Post

stack is a metadata structure to a valid stack


returns stack status

Return true if the stack is empty; false otherwise


1 return (stack.count is 0)

End emptyStack

Faculty of Computer Science and Engineering HCMUT

Slide 38

Full Stack
Algorithm fullStack (val stack <metadata>) Determines if the stack is full

Pre
Post

stack is a metadata structure to a valid stack


returns stack status

Return true if the stack is full; false otherwise


1 return (stack.count = stack.stackMax) End fullStack

Faculty of Computer Science and Engineering HCMUT

Slide 39

Stack Count
Algorithm stackCount (val stack <metadata>) Returns the number of elements currently in the stack

Pre
Post

stack is a metadata structure to a valid stack


returns stack count

Return integer count of the number of elements in the stack


1 return (stack.count)

End stackCount

Faculty of Computer Science and Engineering HCMUT

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:

Case study: Exiting a maze

Faculty of Computer Science and Engineering HCMUT

Slide 42

You might also like