You are on page 1of 65

# UPES

## STACKS & QUEUES

| Jul 2012|

2012 UPES

Contents
Application of stack, Operations on Stacks: Push & Pop,
Algorithm: Stack Implementation through Array
Recursion: Recursive functions
Conversion of Infix to Prefix and Postfix Expressions
Evaluation of postfix expression using stack
Operations on Queue: Create, Add, Delete
Algorithm: Queues Representation and implementation through Array
Concepts: Full & Empty, Circular queue.
D-queues and Priority Queues.
Jul
Jul2012
2012

2012 UPES

What is a stack?
Stores a set of elements in a particular order
Stack principle: LAST IN FIRST OUT = LIFO
It means: the last element inserted is the first one to be removed
Example

Jul
Jul2012
2012

2012 UPES

## What do these tools have in common ?

Plate Dispenser
PEZ Dispenser
Jul
Jul2012
2012

2012 UPES

CIS 068

Jul
Jul2012
2012

2012 UPES

Definition
A stack is an ordered collection of data elements where the insertion
and deletion operations takes place at one end only.

## Difference between Array & stack

In array we can insert &delete the element at any position but not in
stack.

Jul
Jul2012
2012

2012 UPES

Representation of Stack
A stack may be represented in the memory in various ways.
Mainly there are two ways:
One Dimensional array (static time )

Representation of Stack:

Jul
Jul2012
2012

2012 UPES

Operations on stacks
PUSH
POP
STATUS

Jul
Jul2012
2012

## an object (e.g. a plate) onto dispenser

an object out of dispenser
to know the present state of a stack

2012 UPES

Algorithm PUSH_Array
Input : the new item ITEM to be pushed onto it
Output :A stack with newly pushed ITEM at the TOP position
Data structure :An array A with TOP as the pointer.

## If TOP>= Size then

Print stack is full
Else
Top=TOP+1
A[TOP]= ITEM
END IF
Stop
Jul
Jul2012
2012

2012 UPES

Algorithm Pop_Array
Input : A stack with elements
Output: removes an ITEM from the top of the stack if it is not empty
Data structure :an array A with TOP as the pointer

If TOP<0
Print stack is empty
Else
Item=A[top]
Top=top-1
End if
stop
Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Stack Applications
Real life

Pile

of books
Plate trays
More applications related to computer science

Program

## execution stack (read more from your

text)
Evaluating expressions

Jul
Jul2012
2012

2012 UPES

## Evaluation of Arithmetic Expressions

An arithmetic expression consists of operands and operators
Operands are variables or constants
Operators are:
Precedence and Associativity of Operator

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

## Whatever the way we specify the order of evaluations, the

problem is that we must scan the expression from left to right
repeatedly. Hence, the above mentioned processes is inefficient
because of the repeated scanning required.
Another problem is the ambiguity that the compiler how can
resolve to generate correct code for a given expression.
The last problem mainly occurs for a partially parenthesized
expression.
These problems can be solved with the following two steps:
1.Conversion of a given expression into a special notation
2. Evaluation/production of object code using stack.

Jul
Jul2012
2012

2012 UPES

Infix

## : operators come in between the operands

<operand> <operator> <operand>.

## prefix or Polish : operators come before the operands

<operator> <operand> <operand>
Postfix or Suffix : operator is suffixed by operands
<operand> <operand> <operator>

Jul
Jul2012
2012

2012 UPES

Examples
order of operation
1)parentheses
() { } [ ]
2) Exponents
(right to left )
3)Multiplication and division
left to right
left to right

infix
2+3
4+6*2
2*6/2-3+7

Jul
Jul2012
2012

2012 UPES

## Conversion of an infix expression to postfix expression

Jul
Jul2012
2012

2012 UPES

Example:

Input: ( A + B ) ^ C ( D * E ) / F )

Jul
Jul2012
2012

2012 UPES

Assignment-I

((A-(B+C))*D)\$(E+F)

Jul
Jul2012
2012

2012 UPES

## Evaluation of postfix expression

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Implementation of Recursion

Jul
Jul2012
2012

2012 UPES

## Three popular Recursive Computations:

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Quick Sort
Quick sort algorithm is based on divide and conquer technique.
Principle behind the divide and conquer technique is to divide a problem
into a number of sub-problems. Again each sub-problem into number of
smaller sub-problems and so on till a sub-problem is not decomposable.
Solving a problem means to solve all the sub-problems.

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Static scope
Dynamic scope

Jul
Jul2012
2012

2012 UPES

Dynamic scope

Jul
Jul2012
2012

2012 UPES

## Implementation of scope rules using stack

Implementation of scope rules actually is to solve the problem of
allocation of memory variable that are declared in different blocks
Structure of an activation Record

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

A)

B)

C)
d)

Jul
Jul2012
2012

2012 UPES

E)

f)

G)

h)

Jul
Jul2012
2012

2012 UPES

i)

J)

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

## Implementation of dynamic scope rule

A)

B)

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Queue
Difference between stack Vs. Queue

Jul
Jul2012
2012

2012 UPES

Queue Applications
Queuing in front of a counter

## process synchronization in multi-user

Resource sharing in a

Jul
Jul2012
2012

environment
Computer system

2012 UPES

DEFINITION
Queue is also a linear data structure like array
in a queue insertion (called ENQUEUE) operation is called
REAR
deletion (called DEQUEUE) operation is called FRONT
Queue follows first in first out (FIFO) principle

Jul
Jul2012
2012

2012 UPES

## Representation of Queue using Array

Here Rear is used for insertion and front is used for deletion

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

## VARIOUS QUEUE STRUCTURES

Circular Queue

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Length=4

Jul
Jul2012
2012

2012 UPES

Deque

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Jul
Jul2012
2012

2012 UPES

Priority Queue
Each element has been assigned a value , called priority of the element, and
an element can be inserted or deleted not only at the ends but at any position
on the queue

priority queue does not strictly follow first-in first-out (FIFO) principle
which is the basic principle of a queue
Let us consider a particular model of priority queue.
1. An element of higher priority is processed before any element of
lower priority.
2. Two elements with the same priority are processed according to the
order in which they were added to the queue.

Jul
Jul2012
2012

2012 UPES

## Two basic operations namely insertion or deletion. There are various

ways of implementing the structure of a priority queue. These are
(i) Using a simple/circular array
(ii) Multi-queue implementation
(iii) Using a double linked list, and
(iv) Using heap tree.

Jul
Jul2012
2012

2012 UPES

## Priority queue using an array

The element will be inserted at the REAR end as usual. The deletion
operation will then be performed either of the two following ways:
(a) Starting from the FRONT pointer, traverse the array for an element of the
highest priority.
Delete this element from the queue. If this is not the front most element shift
all its trailing elements after the deleted element one stroke each to fill up the
vacant position

## This implementation, however, is very inefficient as it involves searching the

queue for the highest priority element and shifting the trailing elements after
the deletion.
Jul
Jul2012
2012

2012 UPES

(b) Add the elements at the REAR end as earlier. Using a stable sorting
algorithm, sort the elements of the queue so that the highest priority
elements is at the FRONT end. When a deletion is required, delete it from
the FRONT end only

Jul
Jul2012
2012

2012 UPES