STACKS
LECTURE # 04
LEARNING OUTCOME
CLO_1:
Describe basic ADTs (stack, queue, array, list, node list, priority
queue, tree, map and dictionary) and their related data structure
implementations(array, single linked structure, double linked
structure, heap, hash table, binary search tree, AVL tree).
College of CS & IS, Najran University 2
Stacks
A stack is a special kind of linear list
in which only two main operations,
insertion and deletion can be performed.
These two operations may occur only
at its top end. The items in it are stored
and retrieved in Last In First Out
(LIFO) manner. First in last out
Stacks have very restricted use.
However they are very efficient and
easier to implement.
03:55:57 PM Muhammad Akram, College of CS & IS, Najran University 3
Common Stack Examples
College of CS & IS, Najran University 4
Representation of Stack
A stack is usually represented by a linear array in the computer
memory.
Bottom Top
69 77 75 66
1 2 3 4 5 6 7 8
Four items are stored in above stack, it can store maximum of 8
items. The position of the top item is four.
The most accessible item in the stack is called the Top of the
stack and the least accessible item is called Bottom of the stack.
College of CS & IS, Najran University 5
Applications of Stack
• Direct:
• Visited web pages history (Back button)
• Undo changes on your word document: this operation is accomplished by
keeping all text changes in a stack.
• Undo/Redo stacks in Excel or Word.
• Indirect:
• Functions calls
• Arithmetic Expression evaluation : (Prefix, Infix, Postfix Notations)
• Reversing Strings We can use stacks to reverse data (example: files, strings)
03:55:57 PM College of CS & IS, Najran University 6
Operations on Stack
The operations that can be performed on a stack are:
•Construct an empty Stack.
•IsEmpty( )
•IsFull( )
•Insertion (Push Operation).
•Deletion (Pop Operation)
Muhammad Akram, College of CS & IS, Najran University 7
Stack Overflow Vs. Stack Underflow
Stack Overflow:
If adding an element onto the top of the stack and if that stack is
full this situation is called stack overflow.
Stack Underflow:
Removing an element from the stack and if that stack is empty
this situation is called stack underflow.
• Note: Java program throws EmptyStackException in case of
Stack underflows.
03:55:57 PM Muhammad Akram, College of CS & IS, Najran University 8
Push Operations on Stack
The process of inserting or adding new items into stack is
called Pushing.
Before pushing an item into a stack, it is tested whether
or not there is any room in the stack to store the item.
03:55:57 PM Muhammad Akram, College of CS & IS, Najran University 9
Algorithm for PUSHING element in STACK
Algorithm to add an item 1. [Check for overflow condition]
IF TOP >= N Then
“X” into a stack “S”. PRINT “Stack overflow”
Suppose the stack has N elements RETURN
and pointer TOP represents the top [End of IF Structure ]
element in the stack.
2. [Add element “X” into Stack
“S”]
S[TOP] = X
3. [increment 1 into TOP]
TOP = TOP + 1
4. RETURN
03:55:57 PM College of CS & IS, Najran University 10
Pop Operations on Stack
The process of removing or deleting items from a stack
is called popping.
Before removing an item from a stack it is tested whether
or not at least one element exists in the stack.
Muhammad Akram, College of CS & IS, Najran University 11
Algorithm to POP element from STACK
Algorithm to POP an 1. [Check for Underflow
Condition]
item from stack “S”. IF TOP == 0 Then
Suppose pointer TOP represents the PRINT “Stack UnderFlow”
top element in the stack.
RETURN
[End of IF Structure ]
2. [Decrement Pointer to
delete the Top Item]
TOP = TOP - 1
[END OF PROCEDURE]
3. RETURN
College of CS & IS, Najran University 12
?
?
?
?
QUESTIONS? ?
?
?
?
?
College of CS & IS, Najran University 13