Stack Jagjit Kaur Assistant Professor CGC College of Engineering Mohali, Punjab, India
Stack Jagjit Kaur Assistant Professor CGC College of Engineering Mohali, Punjab, India
Stack Jagjit Kaur Assistant Professor CGC College of Engineering Mohali, Punjab, India
STACK
JAGJIT KAUR
ASSISTANT PROFESSOR
CGC COLLEGE OF ENGINEERING
MOHALI, PUNJAB, INDIA
1
LECTURE OUTLINE
What is a Stack?
Array implementation of stacks
Operations on a Stack
Arithmetic expressions
stacks are used to evaluate postfix expressions
Infix expressions into postfix Expressions
Quicksort
STACKS
What is a Stack?
DEFINITION: A
stack is a homogeneous collection of elements in which an element may be
inserted or deleted only at one end, called the top of the stack.
Formally this type of stack is called a Last In, First Out (LIFO) stack.
Special terminology is used for two basic operations associated with stacks:
a) “Push” is the term used to insert an element into a stack.
b) “Pop” is the term used to delete an element from a stack.
DIAGRAM OF STACKS
Suppose the following 6 elements are pushed, in order, onto an empty stack:
AAA, BBB, CCC, DDD, EEE, FFF
Frequently designate the stack by writing:
STACK : AAA, BBB, CCC, DDD, EEE, FFF
1 2 3 4 5 6 7 8 9 10 N-1 N
TOP
ARRAY IMPLEMENTATION OF STACKS
To implement a stack, items are inserted and removed at the same end
(called the top)
To use an array to implement a stack, you need both the array itself and an
integer
The integer tells you either:
Which location is currently the top of the stack, or
How many elements are in the stack
ARRAY REPRESENTATION OF STACKS
STACK
1 2 3 4 5 6 7 8
MAXSTK 8
TOP 3
Maximum size of n.
Push : This operation adds or pushes another item onto the stack. The number
of items on the stack is less than n.
Pop: This operation removes an item from the stack. The number of items on
the stack must be greater than 0.
Top: This operation returns the value of the item at the top of the stack. Note:
It does not remove that item.
Is Empty: This operation returns true if the stack is empty and false if it is not.
Is Full: This operation returns true if the stack is full and false if it is not.
These are the basic operations that can be performed on a stack.
OPERATIONS ON A STACK
Push :
Push is the function used to add data items to the stack.
In order to understand how the Push function operates, we need to look at the algorithm in
more detail.
Procedure 6.1 : PUSH(STACK, TOP, MAXSTK, ITEM)
First, Push accepts a parameter - item. This parameter is of the same type as the rest of the
stack. Item is the data to be added to the stack.
if top = MAXSTK, then stack is full.
This line performs a check to see whether or not the stack is full.
top := top+1; If the stack is not full, top is increased by a value equal to the size of another
item. This allocates room for the insertion.
PUSHING
0 1 2 3 4 5 6 7 8 9
STACK 17 23 97 44
top = 3 N=4
top = 4 N=5
OPERATIONS ON A STACK
Pop :
Data is removed from a stack by using Pop. From a procedural perspective, pop is called
with the line Pop(item), where item is the variable to store the popped item in.
Once again, we will begin by looking at the algorithm.
If TOP = 0 then, stack is empty, there is no item to pop off the stack. Control can then
be passed to an error handling routine.
TOP := TOP-1; This statement removes the top item from the stack. Decrement top by 1 so
that it now accesses the new top of the stack.
POPING
0 1 2 3 4 5 6 7 8 9
STACK 17 23 97 44 80
top = 4 N=5
0 1 2 3 4 5 6 7 8 9
STACK 17 23 97 44
top = 3 N=4
LINKED REPRENTATION OF STACK:-
POP IN LINKED LIST
ALGO FOR PUSH OPERATION IN LINKED LIST
ALGO FOR POP IN LL
REPRESENTATION
ARITHMETIC EXPRESSIONS
Polish Notation (prefix) : Polish notation, also known as prefix notation, is a form of notation for logic,
arithmetic, and algebra. Its distinguishing feature is that it places operators to the left of their operands.
Lacking parentheses or other brackets.
Infix notation
Infix notation is the conventional notation for arithmetic expressions. It is called infix notation because
each operator is placed between its operands,
operands (as in the case with binary operators such as addition, subtraction, multiplication, division,
……).
When parsing expressions written in infix notation, you need parentheses and precedence rules to remove
ambiguity.
Postfix notation
In postfix notation, the operator comes after its operands. Postfix notation is also known as
reverse Polish
notation (RPN) and is commonly used because it enables easy evaluation of expressions.
Highest : Exponentiation(↑)
Next highest : Multiplication(*) and division(/)
Lowest : Addition (+) and subtraction (-)
Ex :
2 ↑ 3 + 5 *2 ↑ 2 – 12 / 6
Evaluating the Exponentiations :
8 + 5 * 4 -12 / 6
Evaluating the Multiplication and division
8 + 20 -2
Evaluating the Addition and subtraction
26
POLISH NOTATION
The computer evaluates an arithmetic expression written in infix notation in two steps.
1. It converts the expression to postfix notation
2. Then it evaluates the postfix expression
In each step stack is main tool that is used to accomplish the given task
STACKS ARE USED TO EVALUATE POSTFIX EXPRESSIONS
Algorithm 6.3 : This algorithm finds the VALUE of an arithmetic expression P written in
postfix notation.
1. Add a right parenthesis “)” at the end of P
2. Scan P from left to right and repeat steps 3 and 4 for each element of P until the sentinel
“)” is encountered.x
3. If an operand is encountered, put it on STACK.
4. If an operator is encountered, then :
(a) Remove the two topx element of STACK, where A is the top element and
B is the next – to – top element.
(b) Evaluate B A.
(C) Place the result of (b) back on STACK.
[End of if structure]
[End of step 2 loop]
5. Set VALUE equal to the top element on STACK.
6. Exit.
EXAMPLE 6.5
Postfix notation P : 5, 6, 2, +, *, 12, 4, /, -)
Infix Postfix
2+3 23+
2+3*6 36*2+
(2 + 3) * 6 23+6*
A / (B * C) + D * E - A * C ABC*/DE*+AC*-
QUICKSORT
Quicksort is a divide-and-conquer method for sorting.
Divide and conquer method:
It works by partitioning an array into parts, then sorting each part
independently.
S1 S2
QUICKSORT
40 20 10 80 60 50 7 30 100
PICK PIVOT ELEMENT
There are a number of ways to pick the pivot element. In this example,
we will use the first element in the array:
40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LOC = 0 40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 0 RIGHT = 8
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 0 40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 0 RIGHT = 8
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 0 40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 0 RIGHT = 7
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 7 30 20 10 80 60 50 7 40 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 0 RIGHT = 7
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 7 30 20 10 80 60 50 7 40 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 1 RIGHT = 7
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 7 30 20 10 80 60 50 7 40 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 2 RIGHT = 7
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 7 30 20 10 80 60 50 7 40 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 3 RIGHT = 7
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 3 30 20 10 40 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 3 RIGHT = 7
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 3 30 20 10 40 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 3 RIGHT = 6
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 6 30 20 10 7 60 50 40 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 3 RIGHT = 6
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 6 30 20 10 7 60 50 40 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 4 RIGHT = 6
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 4 30 20 10 7 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 4 RIGHT = 6
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 4 30 20 10 7 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 4 RIGHT = 5
1. Set LEFT = BEG, RIGHT = END and LOC = LEFT
LOC = 4 30 20 10 7 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
LEFT = 4 RIGHT = 4
30 20 10 7 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Apply the above procedure repetitively until each sub list contains one element