Professional Documents
Culture Documents
131692048262733898
131692048262733898
Lecture 13
Stack
Implementation & Applications
Abstract Data Type
top() − get the top data element of the stack, without removing it.
POP(Stack, Top)
If Top = NULL
Print “Stack is empty”
Return
Item = Stack [Top]
Top = Top – 1
Return Item
Stack - Basic Operations – (IsEmpty)
Implementation of IsEmpty() function is slightly different. We
initialize top at -1, as the index in array starts from 0. So we check if
the top is below zero or -1 to determine if the stack is empty.
IsEmpty()
If Top = -1
Return True
Else
Return False
End
Stack Operations
top 1
top 7 7
top 5 5 5
top 2 2 2 2
push(2) push(5) push(7) push(1)
top 21
top 7 7 top 7
5 5 5 top 5
2 2 2 2 top 2
1 pop() push(21) 21 pop() 7 pop() 5 pop()
Stack Implementation: Array
• Worst case for insertion and deletion from an array when insert
and delete from the beginning: shift elements to the left.
• Best case for insert and delete is at the end of the array – no need
to shift any elements.
bool IsFull() {
if(top == MAXSIZE) return true;
else return false;
}
int Top() {
return stack[top];
}
Stack using an Array
int push(int data)
{
if( IsFull() )
{
cout<<"Stack is full.";
}
else
{
top = top + 1;
stack[top] = data;
}
}
Stack using an Array
int pop()
{
if(IsEmpty())
{
cout<<“Stack is empty.";
}
else
{
int data = stack[top];
top = top - 1;
return data;
}
}
Stack using an Array
void main() {
push(5);
push(9);
push(15);
while( !IsEmpty()) {
int data = pop();
cout<<data;
}
}
Stack using an Array
Pros:
Easy to implement.
Memory is saved as pointers are not involved.
Cons:
It is not dynamic.
It doesn’t grow and shrink depending on needs at runtime.
Stack Using Linked List
Pros:
The linked list implementation of stack can grow and shrink
according to the needs at runtime.
Cons:
Requires extra memory due to involvement of pointers.
Stack Using Linked List
1 7 5 2
top 7
5
2
head
top 9
7 5 2
7
5
newNode 99
2
push(9)
Stack - Basic Operations – (PUSH)
Push (Item, Top)
Create new_node
Set new_node -> Data = item
If (Top = NULL)
a. new_node -> next = NULL
b. Top = new_node
Else
a. new_node -> next = TOP
b. Top = new_node
Return
Stack: Array or List
• Since both implementations support stack operations in
constant time, any reason to choose one over the
other?
• Allocating and deallocating memory for list nodes does
take more time than preallocated array.
• List uses only as much memory as required by the
nodes; array requires allocation ahead of time.
• List pointers (head, next) require extra memory.
• Array has an upper limit; List is limited by dynamic
memory allocation.
Stack - Applications
• Parsing, Nested Structures, Recursion, Function Calling
• Balancing of symbols (Expression Evaluation)
• Infix to Postfix /Prefix conversion
• Redo-undo features at many places like editors, photoshop.
• Forward and backward feature in web browsers
• Used in many algorithms like Tower of Hanoi, tree,
traversals, stock span problem, histogram problem.
• Other applications can be Backtracking, Knight tour problem, rat
in a maze, N queen problem and sudoku solver
Expression Parsing
The way to write arithmetic expression is known as a notation.
An arithmetic expression can be written in three different but
equivalent notations, i.e., without changing the essence or
output of an expression. These notations are −
• Infix Notation
• Prefix (Polish) Notation
• Postfix (Reverse-Polish) Notation
Use of Stack
• Example of use: prefix, infix, postfix expressions.
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
Precedence
When an operand is in between two different operators, which
operator will take the operand first, is decided by the
precedence of an operator over others.
For example multiplication operation has precedence over
addition, b * c will be evaluated first
Parsing Expressions
Associativity
Associativity describes the rule where operators with the same
precedence appear in an expression.
For example, in expression a + b − c, both + and – have the same
precedence, and both + and − are left associative, so the
expression will be evaluated as (a + b) − c.
Sr. No. Operator Precedence Associativity
1 Exponentiation ^ Highest Right Associative
2 Multiplication ( ∗ ) & Division ( / ) Second Left Associative
Highest
3 Addition ( + ) & Subtraction ( − ) Lowest Left Associative