Data Structures DDPC2423: Chapter 3 - STACKS
Data Structures DDPC2423: Chapter 3 - STACKS
Data Structures DDPC2423: Chapter 3 - STACKS
DDPC2423
Chapter 3 – STACKS
Chapter 3 Course Outline Content
• Stacks
o Stacks Concepts
o Stack Operations: Push, Pop, Stacks Top
o Array Based Stacks and Linked List Stacks
o Stacks Application: Reverse Data and Postponement
Infix To Postfix Transformation
Evaluating Postfix Expressions
Reverse Data
• LAB ACTIVITIES:
– Lab 3 : Stack
Stack Concepts
• Remove the item at the top of the stack and return it to the user.
• After removed the top item, the next older item in the stack
becomes the top.
• When the last item in the stack is deleted, the stack must set to
its empty state.
• If pop is called when the stack is empty, then the stack is in an
underflow state.
Stack Operation : Stack Top
1 stack.head = null
2 stack.count = 0
3 return
end createStack
Stack Algorithms : Push Stack
• Insert an element into the stack.
• Steps to push data into a stack:
1. Find a memory for the node by allocate STEP 2: STEP 1:
memory from dynamic memory.
2. Assign the data to the stack node.
3. Set the next pointer to point to the STEP 3:
STEP 4:
node currently indicated as the stack 3
STEP 5:
top.
4. Update the stack top pointer.
5. Add 1 to the stack count field.
• pNew pointer is used to indentified
the data to be inserted into the stack.
Stack Algorithms : Push Stack (continue)
• To develop the insertion algorithms, need to
analyze three different stack condition:
1. Insertion into empty stack.
2. Insertion into a stack with data.
3. Insertion into a stack when available memory is
exhausted.
Stack Algorithms : Push Stack (continue)
• When insert into a stack that contains a data
– The new node’s next pointer is set to point to the node
currently at the top.
– The stack’s top pointer is set to point to the new node.
• When insert into a an empty.
– The new node’s pointer is set to null.
– The stack’s top pointer is set to point to the new node
(since the stack’s pointer is null, it can be used to set the
new node’s next pointer to null)
• Stack overflow : depends on the application.
Stack Algorithms: Push Stack
Algorithm pushStack( ref stack <metadata>,
val data <dataType>)
Insert (push) one item into the stack.
Pre stack is metadata structure to a valid stack
data contain data to be pushed into stack
Post data have been pushed in stack
Return true if successful, false if memory overflow
1 If (stack full)
1 success = false
2 else
1 allocate (newPtr)
2 newPtr ->data = data
3 newPtr ->next = stack.top
4 stack.top = newPtr
5 stack.count = stack.count + 1
6 success = true
3 end if
4 return success
end pushStack
Stack Algorithms : Pop Stack
• Sends the data in the node at the top of the stack
back to the calling algorithm.
• It then deletes and recycles the node (returns it to
memory).
• Count is adjusted by subtracting 1.
• The algorithms returns to the caller.
• If the pop was successful, it returns true.
• If the stack is empty when pop is called, it returns
false.
Stack Algorithms: Pop Stack
Algorithm popStack( ref stack <metadata>,
ref dataOut <dataType>)
This algorithm pops the item on the top of the stack and returns it to the user.
Pre stack is metadata structure to a valid stack
dataOut is a reference variable to receive the data
Post data have been return to the calling algorithm
Return true if successful, false if underflow
1 If (stack empty)
1 success = false
2 else
1 dltPtr = stack.top
2 dataOut = stack.top->data
3 stack.top= stack.top->next
4 stack.count = stack.count – 1
5 recycle (dltPtr)
6 success = true
3 end if
4 return success
end popStack
Stack Algorithms : Stack Top
• Sends the data in the node at the top of the
stack back to the calling module without
deleting the top node.
• It allows the user to see what will be deleted
when the stack is popped.
• If the stack top was successful, it returns true.
• If the stack is empty when stack top is called,
it returns false.
Stack Algorithms: Stack Top
Algorithm stackTop( val stack <metadata>,
ref dataOut <dataType>)
This algorithm retrieves the data from the top of the stack without changing the
stack.
Pre stack is metadata structure to a valid stack
dataOut is a reference variable to receive the data
Post data have been return to the calling algorithm
Return true if successful, false if underflow
1 if (stack empty)
1 success = false
2 else
1 dataOut = stack.top->data
2 Success = true
3 end if
4 return success
end stackTop
Other Stack Operations
• Empty stack – determine whether the stack is
empty.
• Full stack – determine whether there is not
enough space or memory to allocate new
data.
• Stack count – returns the number of elements
currently in the stack.
• Destroy stack –delete all data in the stack
Stack Algorithms: Empty Stack
Algorithm emptyStack ( val stack <metadata> )
Determine if stack is empty and returns a Boolean.
Pre stack is metadata structure to a valid stack
Post return stack status
Return Boolean, true: stack empty, false: stack contain data
1 if (memory available)
1 result = false
2 else
1 result = true
3 end if
4 return result
end fullStack
Stack Algorithms: Stack Count
Algorithm stackCount ( val stack <metadata> )
Returns the number of elements currently in stack.
Pre stack is metadata structure to a valid stack
Post return stack count
Return integer count of number of elements in stack
1 return stack.count
end stackCount
Stack Algorithms: Destroy Stack
Algorithm destroyStack ( ref stack <metadata> )
This algorithm releases all nodes back to the dynamic memory.
Pre stack is metadata structure to a valid stack
Post stack empty and all nodes recycled
1 createStack (stack)
2 prompt (Enter a number)
3 Read (number)
Fill stack
4 loop (not end of data AND not fullStack(stack))
1 pushStack (stack, number)
2 prompt (Enter next number: <EOF> to stop)
3 read (number)
5 end loop
Now print numbers in reverse
6 loop (not emptyStack(stack))
1 popStack (stack, dataOut)
2 print (dataOut)
7 end loop
end reverseNumber
Stack Application: Postponement application
Priority 2: * /
Priority 1: + -
Priority 0: (
Convert Infix to Postfix Algorithms 4 elseif (token is operator)
Test priority of token to token at top of stack
Algorithm inToPostFix (val formula <string>) 1 stackTop (stack, topToken)
Convert infix formula to postfix. 2 loop (not emptyStack (stack) AND
Pre formula is infix notation that has been priority(token) <= priority(topToken))
edited to ensure that there are no 1 popStack (stack, tokenOut)
syntactical errors 2 concatenate tokenOut to postFix
Post postfix formula has been formatted as 3 stackTop (stack, topToken)
a string 3 end loop
Return postfix formula 4 pushStack (stack, token)
1 createStack (stack) 5 else
2 set postfix to null string Character is operand
3 looper = 0 1 concatenate token to postFix
4 loop (looper < sizeof formula) 6 end if
1 token = formula [looper] 7 looper = looper +1
2 if (token is open parenthesis) 5 end loop
1 pushStack (stack, token) Input formula empty. Pop stack to postFix
3 elseif (token is close parenthesis) 6 loop (not emptyStack(stack))
1 popStack (stack, token) 1 popStack (stack, token)
2 loop (token not open parenthesis) 2 concatenate token to postFix
1 concatenate token to postFix 7 end loop
2 popStack(stack, token) 8 return postFix
3 end loop end inToPostFix
Convert Infix to Postfix: Example
INPUT BUFFER OPERATOR STACK OUTPUT STRING
A*B–(C+D)+E Empty Empty
*B–(C+D)+E Empty A
B–(C+D)+E * A
–(C+D)+E * AB
–(C+D)+E Empty AB*
(C+D)+E - AB*
C+D)+E -( AB*
+D)+E -( AB*C
D)+E -(+ AB*C
)+E -(+ AB*CD
+E - AB*CD+
+E Empty AB*CD+-
E + AB*CD+-
+ AB*CD+-E
Empty AB*CD+-E+
Postfix expression evaluation
• Using stack postponement to evaluate
the postfix expressions.
• Example: Given postfix expression
below,
ABC+*
– Assume that A is 2, B is 4 and C is 6
– What is the expression value?
• Solving by
1. postpone the use of operands by push
into the stack
2. when an operator is found, pop the
two operands at the top of the stack
and perform the operation
3. Then push the value back into the stack
to be used later
Evaluation of PostFix Expressions Algorithms
Algorithm postFixEvaluate (val expr <string>)
This algorithm evaluates a postfix expression and returns its value.
Pre a valid expression
Post postfix value computed
Return value of expression
1 exprSize = length of string 3 end if
2 createStack (stack) 4 index = index + 1
3 index = 0 5 end loop
4 loop (index < exprSize) 6 popStack (stack, result)
1 if (expr [index] is operand) 7 return result
1 pushStack (stack, expr [index] ) end postFixEvaluate
2 else
1 popStack (stack, operand2)
2 popStack (stack, operand1)
3 operator = expr [index]
4 value = calculate (operand1, operator,operand2)
5 pushStack (stack, value)
Assignment 3 (Project) – Group of 4