unit-II -

Post on 31-Jan-2016

215 views 0 download

Tags:

description

data stuructures lesson 2

Transcript of unit-II -

| Jul 2012| © 2012 UPES

UPES

STACKS & QUEUES

© 2012 UPESJul 2012Jul 2012

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.

© 2012 UPESJul 2012Jul 2012

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

Which is the first element to pick up?

© 2012 UPESJul 2012Jul 2012 CIS 068

What do these tools have in common ?

Plate Dispenser

PEZ ® Dispenser

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

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.

© 2012 UPESJul 2012Jul 2012

Representation of Stack A stack may be represented in the memory in various ways.

Mainly there are two ways:

One –Dimensional array (static time )

Single linked list (dynamic time)

Representation of Stack:

© 2012 UPESJul 2012Jul 2012

Operations on stacks

PUSH an object (e.g. a plate) onto dispenserPOP an object out of dispenserSTATUS to know the present state of a stack

© 2012 UPESJul 2012Jul 2012

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

© 2012 UPESJul 2012Jul 2012

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

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

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

© 2012 UPESJul 2012Jul 2012

Evaluation of Arithmetic Expressions

An arithmetic expression consists of operands and operators

Operands are variables or constants

Operators are:

Precedence and Associativity of Operator

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

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.

© 2012 UPESJul 2012Jul 2012

Notations for arithmetic expressions

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>

© 2012 UPESJul 2012Jul 2012

Examples

order of operation1)parentheses () { } [ ]

2) Exponents (right to left )

3)Multiplication and division left to right

4)Addition and subtraction left to right

infix

2+3

4+6*2

2*6/2-3+7

© 2012 UPESJul 2012Jul 2012

Conversion of an infix expression to postfix expression

© 2012 UPESJul 2012Jul 2012

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

© 2012 UPESJul 2012Jul 2012

Assignment-I

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

© 2012 UPESJul 2012Jul 2012

Evaluation of postfix expression

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Implementation of Recursion

© 2012 UPESJul 2012Jul 2012

Three popular Recursive Computations:

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

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.

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Quick sort by using stack

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Tower of Hanoi Problem

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Activation Record Management

Static scope

Dynamic scope

© 2012 UPESJul 2012Jul 2012

Dynamic scope

© 2012 UPESJul 2012Jul 2012

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

© 2012 UPESJul 2012Jul 2012

Execution of program and its run-time stack

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Implementation of static scope rule A) B)

C) d)

© 2012 UPESJul 2012Jul 2012

E) f)

G) h)

© 2012 UPESJul 2012Jul 2012

i)

J)

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Implementation of dynamic scope rule

A) B)

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Queue

Difference between stack Vs. Queue

© 2012 UPESJul 2012Jul 2012

Queue Applications Queuing in front of a counter Traffic control at a turning point

process synchronization in multi-user environment Resource sharing in a

Computer system

© 2012 UPESJul 2012Jul 2012

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

© 2012 UPESJul 2012Jul 2012

Representation of Queue using Array

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

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

VARIOUS QUEUE STRUCTURES

Circular Queue

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Length=4

© 2012 UPESJul 2012Jul 2012

Deque

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

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.

© 2012 UPESJul 2012Jul 2012

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.

© 2012 UPESJul 2012Jul 2012

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.

© 2012 UPESJul 2012Jul 2012

(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