unit-II -
-
Upload
mayank-yadav -
Category
Documents
-
view
215 -
download
0
description
Transcript of unit-II -
![Page 1: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/1.jpg)
| Jul 2012| © 2012 UPES
UPES
STACKS & QUEUES
![Page 2: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/2.jpg)
© 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.
![Page 3: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/3.jpg)
© 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?
![Page 4: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/4.jpg)
© 2012 UPESJul 2012Jul 2012 CIS 068
What do these tools have in common ?
Plate Dispenser
PEZ ® Dispenser
![Page 5: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/5.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 6: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/6.jpg)
© 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.
![Page 7: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/7.jpg)
© 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:
![Page 8: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/8.jpg)
© 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
![Page 9: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/9.jpg)
© 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
![Page 10: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/10.jpg)
© 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
![Page 11: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/11.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 12: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/12.jpg)
© 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
![Page 13: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/13.jpg)
© 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
![Page 14: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/14.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 15: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/15.jpg)
© 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.
![Page 16: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/16.jpg)
© 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>
![Page 17: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/17.jpg)
© 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
![Page 18: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/18.jpg)
© 2012 UPESJul 2012Jul 2012
Conversion of an infix expression to postfix expression
![Page 19: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/19.jpg)
© 2012 UPESJul 2012Jul 2012
Example: Input: ( A + B ) ^ C – ( D * E ) / F )
![Page 20: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/20.jpg)
© 2012 UPESJul 2012Jul 2012
Assignment-I
((A-(B+C))*D)$(E+F)
![Page 21: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/21.jpg)
© 2012 UPESJul 2012Jul 2012
Evaluation of postfix expression
![Page 22: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/22.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 23: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/23.jpg)
© 2012 UPESJul 2012Jul 2012
Implementation of Recursion
![Page 24: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/24.jpg)
© 2012 UPESJul 2012Jul 2012
Three popular Recursive Computations:
![Page 25: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/25.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 26: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/26.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 27: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/27.jpg)
© 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.
![Page 28: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/28.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 29: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/29.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 30: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/30.jpg)
© 2012 UPESJul 2012Jul 2012
Quick sort by using stack
![Page 31: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/31.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 32: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/32.jpg)
© 2012 UPESJul 2012Jul 2012
Tower of Hanoi Problem
![Page 33: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/33.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 34: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/34.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 35: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/35.jpg)
© 2012 UPESJul 2012Jul 2012
Activation Record Management
Static scope
Dynamic scope
![Page 36: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/36.jpg)
© 2012 UPESJul 2012Jul 2012
Dynamic scope
![Page 37: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/37.jpg)
© 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
![Page 38: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/38.jpg)
© 2012 UPESJul 2012Jul 2012
Execution of program and its run-time stack
![Page 39: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/39.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 40: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/40.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 41: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/41.jpg)
© 2012 UPESJul 2012Jul 2012
Implementation of static scope rule A) B)
C) d)
![Page 42: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/42.jpg)
© 2012 UPESJul 2012Jul 2012
E) f)
G) h)
![Page 43: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/43.jpg)
© 2012 UPESJul 2012Jul 2012
i)
J)
![Page 44: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/44.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 45: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/45.jpg)
© 2012 UPESJul 2012Jul 2012
Implementation of dynamic scope rule
A) B)
![Page 46: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/46.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 47: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/47.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 48: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/48.jpg)
© 2012 UPESJul 2012Jul 2012
Queue
Difference between stack Vs. Queue
![Page 49: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/49.jpg)
© 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
![Page 50: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/50.jpg)
© 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
![Page 51: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/51.jpg)
© 2012 UPESJul 2012Jul 2012
Representation of Queue using Array
Here Rear is used for insertion and front is used for deletion
![Page 52: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/52.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 53: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/53.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 54: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/54.jpg)
© 2012 UPESJul 2012Jul 2012
VARIOUS QUEUE STRUCTURES
Circular Queue
![Page 55: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/55.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 56: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/56.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 57: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/57.jpg)
© 2012 UPESJul 2012Jul 2012
Length=4
![Page 58: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/58.jpg)
© 2012 UPESJul 2012Jul 2012
Deque
![Page 59: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/59.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 60: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/60.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 61: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/61.jpg)
© 2012 UPESJul 2012Jul 2012
![Page 62: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/62.jpg)
© 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.
![Page 63: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/63.jpg)
© 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.
![Page 64: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/64.jpg)
© 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.
![Page 65: unit-II -](https://reader035.fdocuments.in/reader035/viewer/2022062500/5695d29f1a28ab9b029b223f/html5/thumbnails/65.jpg)
© 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