0% found this document useful (0 votes)
82 views29 pages

Stacks and Queues

This document discusses stacks and queues data structures. It defines stacks and queues, describes their common operations like push and pop. It provides implementations of stacks and queues using arrays and linked lists. It also discusses various algorithms for stack and queue operations and their time complexities. It gives examples of applications of stacks like expression evaluation, matching parentheses, quicksort, and recursion. Finally, it explains conversion between infix, prefix and postfix notations with examples.

Uploaded by

Rajat Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views29 pages

Stacks and Queues

This document discusses stacks and queues data structures. It defines stacks and queues, describes their common operations like push and pop. It provides implementations of stacks and queues using arrays and linked lists. It also discusses various algorithms for stack and queue operations and their time complexities. It gives examples of applications of stacks like expression evaluation, matching parentheses, quicksort, and recursion. Finally, it explains conversion between infix, prefix and postfix notations with examples.

Uploaded by

Rajat Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Data Structure and Algorithms Using Java Programming

Stacks & Queues

Narendra L Lokhande

Department of Electronics & Telecommunication


R C Patel Institute of Technology, Shirpur
Dist: Dhule

Email: [email protected]

August 27, 2019


Data Structure and Algorithms Using Java Programming

Content
I ADT Stack and Its operations
I Algorithms and their complexity analysis
I Applications of stacks
I Expression Conversion and Evaluation
I ADT Queue
I Type of Queue
I Simple Queue
I Circular Queue
I Priority Queue
I Operations on each type of Queue
Data Structure and Algorithms Using Java Programming

What is Stack?
A stack is a simple data structure used for storing data (similar to
Linked Lists).
In a stack, the order in which the data arrives is important.
Definition: A stack is an ordered list in which insertion and deletion
are done at one end, called top. The last element inserted is the first
one to be deleted. Hence, it is called the Last in First out(LIFO) or First
in Last out (FILO) list.
When an element is inserted in a stack, the concept is called push,
and when an element is removed from the stack, the concept is called
pop.
Trying to pop out an empty stack is called underflow and trying to
push an element in a full stack is called overflow.
Data Structure and Algorithms Using Java Programming

Operations on the Stack

Two basic operations on the stack are


I Push: This operation refers to the insertion of a new element in
to the stack.
I A new element is always inserted at the top of the stack.
I We can perform the push operation only when the stack is not full
i.e. stack has sufficient space to accommodate the new element.
I When the stack is full and an attempt is made to insert a element
into the stack then this condition is called as stack overflow.
I Pop: This operation refers to the removal of an element from the
top of the stack.
I We can perform the pop operation only when the stack is not
empty. Therefore we must ensure that stack is not empty before
applying pop operation.
I When the stack is empty and we are attempting to remove an
element from stack then this condition is called as stack underflow.
Data Structure and Algorithms Using Java Programming

Operations on the Stack


Data Structure and Algorithms Using Java Programming

Array Representation of Stack


It is simplest form of representation of stack. But an array puts certain
restriction while representing stack and they are
I The stack must contain homogeneous data elements.
I One must specify the upper bound of the array i.e. maximum
size of the stack must be defined before implementing it.
I Stack grows and shrinks over time but it is confined to the space
allocated to the array implementing that array.
Data Structure and Algorithms Using Java Programming

Algorithm: Push Operation


Push operation involves a series of steps
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.
Data Structure and Algorithms Using Java Programming

Push Algorithm
Insert a new element ‘data’ at the top of the stack represented by an
array ‘S’ of size ‘Max’ with a stack index variable ‘Top’ pointing to
the topmost element of the stack.
initialization;
if Top = NULL then
Print: “Stack is already Empty, underflow condition”;
Exit;
end
Set Data = S[Top];
Set Top = Top - 1;
Exit;
Algorithm 1: Push Operation
Data Structure and Algorithms Using Java Programming

Pop Algorithm
Delete an element from the stack represented by an array ‘S’ and
returns the element ‘Data’ which is at the top of the stack.
initialization;
if Top = Max then
Print: “Stack is already full”;
Exit;
end
Set Top = Top + 1;
Set S[Top] = data;
Exit;
Algorithm 2: Pop Operation
Data Structure and Algorithms Using Java Programming

Linked List Representation of Stack


Data Structure and Algorithms Using Java Programming

Push Operation using linked list


Insert a new element ‘data’ at the top of the stack represented by the
linked list with a stack pointer variable ‘Top’ pointing to the topmost
node of the stack.
initialization;
if Free = NULL then
Print: “Free space not available, overflow condition”;
Exit;
end
Allocate memory to a node New from free memory pool;
Set New = Free and Free = Free −→ Next;
Set New −→ Info = Data and New −→ Next = Top;
Set Top = New;
Exit;
Algorithm 3: Push Operation using linked list
Data Structure and Algorithms Using Java Programming

Pop Operation using linked list


Delete an element from the stack represented by linked list and
returns the element ‘Data’ which is at the top of the stack.
initialization;
if Top = NULL then
Print: “Stack is empty, underflow condition”;
Exit;
end
Set Data = Top−→ Info and Temp = Top ;
Set Top = Top −→ Next ;
Deallocate the deleted node Temp to free storage list ;
Set Temp −→ Next = Free and Free −→ Temp ;
Exit ;
Algorithm 4: Pop Operation using linked list
Data Structure and Algorithms Using Java Programming

Applications of Stack
I Evaluation of Arithmetic Expression
I Infix Notation
I Prefix Notation
I Postfix Notation
I Matching Parenthesis
I Quick Sort
I Recursion
Data Structure and Algorithms Using Java Programming

Evaluation of Arithmetic Expression


I Infix Notation: If the operator symbol is placed between its
operands then the expression is in Infix Notation. e.g. Y = A + B
I Prefix Notation: If the operator symbol is placed before its
operands then the expression is in Prefix Notation. e.g. Y = +AB
I Postfix Notation: If the operator symbol is placed after its
operands then the expression is in Postfix Notation. e.g. Y =
AB+
Notation Prefix is also called as Polish Notation
Notation Postfix is also called as Reverse Polish Notation
Data Structure and Algorithms Using Java Programming

Operator Priority

Priority Operator Associativity


1 Brackets Inner to Out and Left to Right
2 Exponent ∧ Left to Right
3 */ Left to Right
4 +- Left to Right
5 = Right to Left
Data Structure and Algorithms Using Java Programming

Precedence Example
Consider an expression:
e = 4 - 2 ∧ 4 + 8 * 3 + 18 / 3 + 6
∧ is having highest precedence over the other operators will be
solved first
e = 4 - 16 + 8 * 3 + 18 / 3 + 6
Now , * and / operations will be performed from left to right because
both are having same level of precedence
e = 4 - 16 + 24 + 18 / 3 + 6
e = 4 - 16 + 24 + 6 + 6
Now , + and - operations will be performed from left to right because
both are having same level of precedence
e = -12 + 24 + 6 + 6
e = 12 + 6 + 6
e = 18 + 6
e = 24
Data Structure and Algorithms Using Java Programming

Precedence another Example


Consider an expression:
e = 5 * 3 + (60 - 36) / 6 + ( 3 * 10 ) + 7
Bracket is having highest precedence over the other operators hence
will be solved first
e = 5 * 3 + 24 / 6 + 30 + 7
Now , * and / operations will be performed from left to right because
both are having same level of precedence
e = 15 + 4 + 30 + 7
Now , + and - operations will be performed from left to right because
both are having same level of precedence
e = 19 + 30 + 7
e = 49 + 7
e = 56
Data Structure and Algorithms Using Java Programming

Example for conversion Infix to Prefix


In order to translate an arithmetic expression from infix to polish no-
tation, we will do it step by step using square bracket [ ] to indicate
partial conversion.
This means the expression within the square bracket will be treated ad
a single operand.
Example 1:
Iin = (a − b)/c
= [−ab]/c
IPre = / − abc
Example 2:
Iin = (x − y ) × ((z + v )/f )
= [−xy ] × ([+zv ]/f
= [−xy ] × ([/ + zvf ]
IPre = × − xy / + zvf
Data Structure and Algorithms Using Java Programming

Example for conversion Infix to Prefix


Example 3:

Iin = ((a + b)/d ∧ ((e − f ) + g))


= [+ab]/d ∧ ([−ef ] + g))
= [+ab]/[∧d + −efg]
IPre = / + ab ∧ d + −efg
Data Structure and Algorithms Using Java Programming

Example for conversion Infix to Prefix


Example 4:

Iin = (x ∗ y) + (z + ((a + b − c) ∗ d)) − i ∗ (j/k )


= (x ∗ y) + (z + (([+ab] − c) ∗ d)) − i ∗ (j/k )
= (x ∗ y) + (z + ([− + abc) ∗ d) − i ∗ (j/k )
= (x ∗ y) + (z + ([∗ − +abcd]) − i ∗ (j/k )
= [∗xy ] + (z + ([∗ − +abcd]) − i ∗ (j/k)
= [∗xy ] + [+z ∗ − + abcd] − i ∗ (j/k)
= [∗xy ] + [+z ∗ − + abcd] − i ∗ [/jk]
= [∗xy ] + [+z ∗ − + abcd] − [∗i/jk]
= [+ ∗ xy + z ∗ − + abcd] − [∗i/jk]
IPre = − + ∗xy + z ∗ − + abcd ∗ i/jk
Data Structure and Algorithms Using Java Programming

Example for conversion Infix to Postfix


Example 1:

Iin = (a − b)/c
= [ab−]/c
IPost = ab − c/

Example 2:

Iin = (x − y ) × ((z + v )/f )


= [xy −] × ([zv +]/f
= [xy −] × ([zv + f /]
IPost = xy − zv + f /×
Data Structure and Algorithms Using Java Programming

Example for conversion Infix to Postfix


Example 3:

Iin = ((a + b)/d ∧ ((e − f ) + g))


= [ab+]/d ∧ ([ef −] + g))
= [ab+]/d ∧ [ef − g+]
= [ab+]/[def − g + ∧]
IPost = ab + def − g + ∧/
Data Structure and Algorithms Using Java Programming

Example for conversion Infix to Postfix


Example 4:

Iin = (x ∗ y) + (z + ((a + b − c) ∗ d)) − i ∗ (j/k )


= (x ∗ y) + (z + (([ab+] − c) ∗ d)) − i ∗ (j/k )
= (x ∗ y) + (z + ([ab + c−) ∗ d) − i ∗ (j/k )
= (x ∗ y) + (z + ([ab + c − d∗]) − i ∗ (j/k )
= [xy ∗] + (z + ([ab + c − d∗]) − i ∗ (j/k )
= [xy ∗] + [zab + c − d ∗ +] − i ∗ (j/k )
= [xy ∗ zab + c − d ∗ ++] − i ∗ [jk/]
= [xy ∗ zab + c − d ∗ ++] − [ijk /∗]
IPost = xy ∗ zab + c − d ∗ + + ijk / ∗ −
Data Structure and Algorithms Using Java Programming

Algorithm: Infix expression to Prefix Expression


A is Arithmetic expression & B is Prefix expression
Step 1: Push “)” onto STACK, and add “(” to end of the A
Step 2: Scan A from right to left and repeat step 3 to 6 for each ele-
ment of A until the STACK is empty
Step 3: If an operand is encountered add it to B
Step 4: If a right parenthesis is encountered push it onto STACK
Step 5: If an operator is encountered then:
a. Repeatedly pop from STACK and add to B each operator(on the top
of STACK) which has same
or higher precedence than the operator.
b. Add operator to STACK
Step 6: If left parenthesis is encountered then
a. Repeatedly pop from the STACK and add to B (each operator on
top of stack until a left parenthesis is encountered)
b. Remove the left parenthesis
Step 7: Exit
Data Structure and Algorithms Using Java Programming

Algorithm: Infix expression to Postfix Expression


A is Arithmetic expression & B is Postfix expression
Step 1: Push left parenthesis “(” into STACK, and add right parenthe-
sis “)” to end of the A
Step 2: Scan A from left to right and repeat step 3 to 6 for each ele-
ment of A until the STACK is empty
Step 3: If an operand is encountered add it to B
Step 4: If a left parenthesis is encountered push it onto STACK
Step 5: If an operator is encountered then:
a. Repeatedly pop from STACK and add to B each operator(on the top
of STACK) which has same
or higher precedence than the operator.
b. Add operator to STACK
Step 6: If right parenthesis is encountered then
a. Repeatedly pop from the STACK and add to B (each operator on
top of stack until a left parenthesis is encountered)
b. Remove the left parenthesis (Do not add left parenthesis to B)
Step 7: Exit
Data Structure and Algorithms Using Java Programming

Evaluation of Prefix Expression


/* Reading the expression take place from right to left */
Step 1: Read the next element
Step 2: If element is operand then
push the element in the stack
Step 3: If element is operator then
i) Pop two operands from the stack
ii) Evaluate the expression formed by two operands and operator
iii) Push the results of expression in the stack end
Step 4: If no more elements then
Pop the Result
else
goto step 1
Step 5: Exit
Data Structure and Algorithms Using Java Programming

Evaluation of Postfix Expression


/* Reading the expression take place from left to right */
Step 1: Read the next element /* first element for first time */
Step 2: If element is operand then
push the element in the stack
Step 3: If element is operator then
i) Pop two operands from the stack /* POP one operand in case of
NOT operator */
ii) Evaluate the expression formed by two operands and operator
iii) Push the results of expression in the stack end
Step 4: If no more elements then
Pop the Result
else
goto step 1
Step 5: Exit
Data Structure and Algorithms Using Java Programming

Parenthesis Checking
procedure check()
Declare a character stack S.
Now traverse the expression from left to right
a) If the current character is a starting bracket then push it to stack.
b) If the current character is a closing bracket then pop from stack and
if the popped character is the matching starting bracket then fine else
parenthesis are not balanced.
After complete traversal, if there is some starting bracket left in stack
then “not balanced”
End procedure
Data Structure and Algorithms Using Java Programming

Parenthesis Check

You might also like