This material aims to enable students to:
1) Understanding stack concept
2) Understanding push, pop, peek or top, isEmpty, isFull operation in a stack
3) Understanding working of stack
4) Knowing of stack application
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Β
Algorithm and Data Structure - Stack
1. Algorithm and
Data Structure
Andi Nurkholis, S.Kom, M.Kom
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2020-2021
April 12, 2021
3. 3
What is Stack?
Stack is a linear data structure which follows a
particular order in which the operations are
performed. The order may be LIFO (Last In First Out)
or FILO (First In Last Out)
In stacks, the insertion and deletion of elements
happen only at one endpoint of it.
6. 6
Push
Push: Adds an item in the stack. If the stack is full, then it is said to
be an Overflow condition.
7. Steps of Push
Step 1 β Checks if the stack is full.
Step 2 β If the stack is full, produces an error and exit.
Step 3 β If the stack is not full, increments top to
point next empty space.
Step 4 β Adds data element to the stack location,
where top is pointing.
Step 5 β Returns success.
7
8. 8
Push Algorithm
begin procedure push: stack, data
if stack is full
return null
endif
top β top + 1
stack[top] β data
end procedure
9. 9
Pop
Pop: Removes an item from the stack. The items are popped in
the reversed order in which they are pushed. If the stack is
empty, then it is said to be an Underflow condition.
10. Steps of Pop
Step 1 β Checks if the stack is empty.
Step 2 β If the stack is empty, produces an error and
exit.
Step 3 β If the stack is not empty, accesses the data
element at which top is pointing.
Step 4 β Decreases the value of top by 1.
Step 5 β Returns success.
10
11. Pop Algorithm
begin procedure pop: stack
if stack is empty
return null
endif
data β stack[top]
top β top - 1
return data
end procedure
11
12. Peek or Top
Peek or Top: Returns the element on top of the stack.
It takes O(1)O(1) time as finding the value stored at a
particular index in an array is a constant time
operation. The operation is to get the top data
element of the stack, without removing it
12
Peek Algorithm:
begin procedure peek
return stack[top]
end procedure
13. IsEmpty
IsEmpty: Checks whether the
stack is empty. Returns true if
stack is empty, else false.
IsEmpty Algorithm:
begin procedure isempty
if top less than 1
return true
else
return false
endif
end procedure
13
14. IsFull
IsEmpty: Checks whether
the stack is full. Returns true
if stack is full, else false.
IsFull Algorithm:
begin procedure isfull
if top equals to MAXSIZE
return true
else
return false
endif
end procedure
14
15. Working of Stack
This abstract data typeβ can be implemented in C in
multiple ways. One such way is by using an array.
Pro of using an array:
No extra memory required to store the pointers.
Con of using an array:
The size of the stack is pre-set so it cannot increase
or decrease.
15
16. Stack Application
1. Balancing of symbols
2. Infix to Postfix /Prefix conversion
3. Redo-undo features at many places like editors, photoshop.
4. Forward and backward feature in web browsers
5. Used in many algorithms like Tower of Hanoi, tree traversals,
stock span problem, histogram problem.
16
17. Stack Application (Cont.)
6. Backtracking is one of the algorithm designing technique.
7. In Graph Algorithms like Topological Sorting and Strongly
Connected Components
8. In Memory management any modern computer uses stack as
the primary-management for a running purpose.
9. String reversal is also a another application of stack.
17
18. Thank You, Next β¦
Queue
April 12, 2021
Andi Nurkholis, S.Kom, M.Kom
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2020-2021