finalinternalppt

20
STACKS AND QUEUES

Transcript of finalinternalppt

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 1/20

STACKS AND QUEUES

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 2/20

STACK OVERVIEW

Stack A T Basic operations of stack 

Pushing, popping etc.

Implementations of stacks using ± array

 ± linked list

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 3/20

Stac ADTA stac is a li ear ata structure i w ic ite s are

i serte a elete at t e sa e e w ic is

w as t e t f t e stac.

Stac s are w as LIFO (Last I , First Out) lists T e

last ele e t i serte will be t e first t be retrieve

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 4/20

� Push:Add an element to the top of the stack

Pri ar O erati s

top

empty stack

 Atop

push an element 

top

push another

 A

B

top

Stack full

 A

B

B

C

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 5/20

top

 A

top

pop an element 

top

pop another

 A

B

empty stack

Pop

Remove the element at the top of the stack

C

B

 A

B

Stack Full

Pri ar O erati s

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 6/20

Pseu c e f r  Pus erati f stac

Sub us (stac 1,t ,ele e t,size)

If t >=size t e

Dis la ³stac verfl w´

retur 

E if t =t +1

stac 1[t ]=ele e t

retur 

e sub

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 7/20

Pseu c e f r  Pop operation of stac

sub pop[stac 1,top]

if top=0 t endis pla ³stac in underflow´

exit

endif top<=top-1

retur n[s(top+1)]

endsub

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 8/20

A pplication of stac

Conversion of ex pressions: Using stac s we can 

easil convert ex pressions fr om infix to  postfix, prefix to 

 postfix etc Evaluation of ex pressions: An ex pression written in 

t e f or m of  postfix or  prefix can be easil evaluated

Recursion: Stac s can also be used in recursive

functions

Runtime memory management: Stacks are used in run

time memory management

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 9/20

Stack Implementation

Stack  can be implemented in two different wa s:

1. Con

tiguo

us stack 

: t e stack 

is imp

lem

en

ted

as an arra .

2. Link ed stack : pointers and dynamic memor y allocation is

used to implement t e stack .

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 10/20

QUEUES OVERVIEW

. . Queue ADT

asic operations of queue

Enqueuing, dequeuing etc.

Implementation of queueArray

Link ed list

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 11/20

A queue is an or dered gr ou p of  omogeneous items(elements), in w ich new elements are added at one end (the rear), and elements are removed fr om the other end 

(the fr ont).A queue is lik e a line of  people waiting f or a bank teller.The queue has a front and a rear.  A queue is a FIFO³f irst in, f irst out´ structur e

QUEUE ADT

$ $ 

Fr ontRear 

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 12/20

Queue O perations

Enqueue: New people must enter the queue at the

rear. The C++ queue class calls this a push, although 

it is usually called an enqueue operation

$ $ 

Fr ont

Rear 

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 13/20

Queue O perations

Dequeue :When an item is tak en fr om the queue, it

always comes fr om the fr ont. The C++ queue calls

this a pop, although it is usually called a dequeueoperation

$ $ 

Fr ont

Rear 

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 14/20

sub insertrear(rear,item,queue1,queuesize )

If(rear==queuesize-1)dis play ´queue overflow´

retur n

endif 

rear=rear+1queue1[rear]=item

endsub

Pseudocode f or Enqueue operation of Queue

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 15/20

Pseudocode f or Dequeue operation of Queue

sub deletefr ont(queue1,fr ont,rear)

If(fr ont>rear)

dis play ´queue underflow´retur n

Endif 

Dis play´element deleted is queue1[(fr ont)++)

If(fr ont>rear)

fr ont=0,rear=-1

endif 

Endsub

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 16/20

Types of queues

Or dinar y Queue

Cir cular Queue

Double Ended Queue

Priority Queue

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 17/20

Cir cular Queue

A cir cular Queue is used to over come

a disadvantage of an or dinar y queue

where we cannot insert elements in 

queue once its full even if free slotsare available

A cir cular Queue solves the pr o blem 

 by using this equation

Rear=rear+1%queuesize

rather than 

Rear=rear+1

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 18/20

A pplications of Queues

Direct applications

Waiting lists, bureaucracyAccess to shared resour ces (e.g., printer)

Multi pr ogramming

Indirect applications

Auxiliar y data structure f or algorithms

Component of other data structures

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 19/20

Implementation of Queue

Just as stack s can be implemented as arrays or 

link ed lists, so with queues.

Dynamic queues have the same advantages

over static queues as dynamic stack s have over 

static stack s

8/7/2019 finalinternalppt

http://slidepdf.com/reader/full/finalinternalppt 20/20

THANK YOUTHANK YOU