3 views

Uploaded by mayank yadav

data stuructures lesson 2

- list-v1.1
- CS2201.pdf
- Data Structures
- Mb0048 Assigment Spring 2013.
- TI
- cam32.txt
- How to perform LO extraction from SAP R-3 to BW system
- Data Structures and Algorithms 2009-4-3 0
- Data Structure Lec09 Handout
- BOOST Berkeley's Out-Of-Order Stack Thingy
- Programming All
- experiments of data structure.docx
- C ++
- CheatsheetOO_A4
- Calendar in SAP
- Lecture4 Assembly
- Chapter 6
- c2a7ef718be41c2df3cdf78801ff1490-original.pdf
- device drivers
- Stack Expressions

You are on page 1of 65

| 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

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.

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 )

Single linked list (dynamic time)

Representation of Stack:

Jul

Jul2012

2012

2012 UPES

Operations on stacks

PUSH

POP

STATUS

Jul

Jul2012

2012

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.

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

text)

Evaluating expressions

Jul

Jul2012

2012

2012 UPES

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

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

<operand> <operator> <operand>.

<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

4)Addition and subtraction

left to right

infix

2+3

4+6*2

2*6/2-3+7

Jul

Jul2012

2012

2012 UPES

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

Jul

Jul2012

2012

2012 UPES

Jul

Jul2012

2012

2012 UPES

Implementation of Recursion

Jul

Jul2012

2012

2012 UPES

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 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

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

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

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

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

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

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

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

- list-v1.1Uploaded bySin Stefan Jekic
- CS2201.pdfUploaded bySpandan Sinha
- Data StructuresUploaded byReddy Ghfsj
- Mb0048 Assigment Spring 2013.Uploaded byisce5b
- TIUploaded bypinco
- cam32.txtUploaded byammayi9845_930467904
- How to perform LO extraction from SAP R-3 to BW systemUploaded byAjay Kumar
- Data Structures and Algorithms 2009-4-3 0Uploaded byAmritpaul Singh
- Data Structure Lec09 HandoutUploaded byJʋŋaiɗ Aĸɓar
- BOOST Berkeley's Out-Of-Order Stack ThingyUploaded byitenuttoet
- Programming AllUploaded byjayeshrane2107
- experiments of data structure.docxUploaded byNikita
- C ++Uploaded byApoorv
- CheatsheetOO_A4Uploaded byChad Knott
- Calendar in SAPUploaded byNowneha
- Lecture4 AssemblyUploaded byathome
- Chapter 6Uploaded byCraig Siewai Perkinton
- c2a7ef718be41c2df3cdf78801ff1490-original.pdfUploaded byAamir Altaf
- device driversUploaded bySoundarya Svs
- Stack ExpressionsUploaded byAnonymous 9etQKwW
- pds prgs CSEUploaded byVigneshwar Suresh
- string arrayUploaded bysandeep_nayak_17
- ProgramsUploaded byDen Jensel Laroco Ares
- NewUploaded byVaibhav Agrawal
- Document 602863Uploaded byRajendran Suresh
- pg_0017Uploaded byfrenchman55
- Minimap Wratan ScriptUploaded by-'MikivaustscreaМotioМalloverz Sacramentremendeouszemotion
- HW RMarkdownExample 20181228.RmdUploaded byRuochong Ji
- Oracle9i and Performance TuningUploaded bypandey321
- Web AssignmetUploaded bylumiy

- Infix to PostfixUploaded byvivekgiri
- Chapt 05Uploaded byPaul Alkema Jr.
- 10-Unit10.pdfUploaded byrajendra_20_007
- UG- EC303 DSP Part-3 Fixed Point DSP Addressing Modes -PrintUploaded byShreyas Tater
- Embedded System - Question Bank (BE-ECE, VII Semester, R2008)Uploaded byDr. N.Shanmugasundaram
- Morgan Stanley InterviewUploaded byOmkar Nagare
- CS1201-DATASTRUCTUREUploaded byShruthi Bhat
- 55326126-Commodore-128-Book-7-Peeks-and-Pokes.pdfUploaded bybandihoot
- RTOS Lab Session 2, 3Uploaded bySaurabhLBonde
- New McaschsyllUploaded byvasunews
- Chapter 08Uploaded bymohammad
- mt2Uploaded bysandbox7117
- Stack vs QueueUploaded bytbijle
- Infix2postUploaded bySrinivasa Helavar
- Advanced Programming with lcc_win32Uploaded byJeff Pratt
- C++ as OOPUploaded byRaghavendra Rajendra Basvan
- traveling circuits robotic friends grades k-5Uploaded byapi-175422164
- Timex Datalink USB ManualUploaded bykpkarlbom
- Data StructureUploaded byThirukkovlaur Raghuveer
- L9 Understanding Atmega328P 2Uploaded byVIGHNESH AIYA
- Guia Inf.grafica v0.8Uploaded byRoberto
- Ethereum White PaperUploaded byJosephAffonso
- Final Revision Notes Computer ScienceUploaded byVivek Singh
- Addendum ExtendSimUploaded byCarolina Vidal
- 2Uploaded byapi-3723664
- Data Structures LabUploaded byNeha
- ACE and Mini-ACE_ Where Are They Used_ - Data Device CorporationUploaded byRamu Banoth
- 213736_Chapter_2Uploaded byThenes Kumar
- ProgramUploaded byNeerajBhargava
- MPM1Uploaded bySandeep Babu Vannempalli