Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

QUEUES

Queue
• A queue is also known as a First-In-First-Out (FIFO) list.
• A queue is a linear list of elements in which deletions can
take place only at one end, called the front, and insertion
can take place only at the other end called the rear.
Queue Operations
• The queue supports three fundamental methods:
• new():ADT-Creates an empty queue..
• Enqueue(S:ADT,O:element):ADT-Inserts object O at the rear of the
queue.
• Dequeue(S:ADT):ADT-Removes the object from the front of the
queue; an error occurs if the queue is empty.
• Front(S:ADT):element-Returns, but does not remove, the front element;
an error occurs if the queue is empty.
Queue Operations
• These support methods should also be defines:
• Size(S:ADT):integer
• IsEmpty(S:ADT):boolean
Enqueue and Dequeue
• Primary queue operations: Enqueue and Dequeue
• Like check-out lines in a store, a queue has a front and a
rear.
• Enqueue
• Insert an element at the rear of the queue
• Dequeue
• Remove an element from the front of the queue

Remove Insert
(Dequeue) front rear (Enqueue)
Implementing a Queue using Array
Front:1, Rear:4 QUEUE

AAA BBB CCC DDD

1 2 3 4 5 6 7 8

Front:2, Rear:4 QUEUE

BBB CCC DDD

1 2 3 4 5 6 7 8
Implementing a Queue using Array
Front:2, Rear:6 QUEUE

BBB CCC DDD EEE FFF

1 2 3 4 5 6 7 8

Front:3, Rear:6 QUEUE

CCC DDD EEE FFF

1 2 3 4 5 6 7 8
Circular Queue
 Suppose we want to insert an element into the queue
after it occupies the last part of the array i.e. REAR=n.

 One way of doing it is simply move the entire queue to


the beginning of the array, changing FRONT and REAR,
and then inserting the element. This procedure may be
very expensive.

 This can be done assuming that the array QUEUE is


circular that is the QUEUE[1] comes after QUEUE[N] in
the array. Instead if increasing REAR to N+1 we reset
REAR=1.
Priority Queues
• A priority queue is a collection of elements such that each
element has been assigned a priority and such that the
order in which the elements are deleted and processed
comes from the following rules:
1. An element of higher priority is processed before any element
of lower priority.
2. Two elements of the same priority are processed according to
the order in which they were add to the queue.
Priority Queue
 Sometimes, we have queues that are not FIFO i.e. the
person who comes first may not leave first.
 We can develop such queues in which the condition for
leaving the queue is not to enter first. There may be
some priority.
 We will develop the queue in such a way that we will get
the event which is going to happen first of all in the
future. This data structure is known as priority queue.
 In a sense, FIFO is a special case of priority queue in
which priority is given to the time of arrival.
 In operating systems, we have queue of different
processes. If some process comes with higher priority, it
will be processed first.
QINSERT

• This procedure inserts an element ITEM into a queue.


QINSERT(QUEUE,N,FRONT,REAR,ITEM)
[Queue already fill]
1. If FRONT=1 and REAR=N, or if FRONT=REAR+1,then
Write: Overflow, and Return
2. [Find new value of REAR]
If FRONT=NULL, then [Queue initially empty]
Set FRONT=1 and REAR=1
Else if REAR =N then
Set REAR=1
Else
Set REAR=REAR+1
[End of if structure]
3. Set QUEUE[REAR]=ITEM [This inserts new element]
4. Return
QDELETE
• This procedure deletes an element from the queue and
assigns it to the variable ITEM.
QDELETE(QUEUE,N,FRONT,REAR,ITEM)
[Queue already empty]
1. If FRONT=NULL, then
Write: Underflow, and Return
2. Set ITEM=QUEUE[FRONT]
3. [Find new value of FRONT]
If FRONT=REAR, then [Queue has only one element to start]
Set FRONT=NULL and REAR=NULL
Else if FRONT =N then
Set FRONT =1
Else
Set FRONT = FRONT +1
[End of if structure]
4. Return
STACKS
Abstract Data Types (ADTs)
• In computing, an abstract data type is a
mathematical model for a certain class of
data structures that have similar behavior;
or for certain data types of one or more
programming languages that have similar
semantics.
• An abstract data type is defined indirectly,
only by the operations that may be
performed on it and by mathematical
constraints on the effects (and possibly
cost) of those operations
Abstract Data Types (ADTs)
• For example, an abstract stack data structure could
be defined by two operations: push, that inserts some
data item into the structure, and pop, that extracts an
item from it; with the constraint that each pop always
returns the most recently pushed item that has not
been popped yet.
• Abstract data types are purely theoretical entities,
used (among other things) to simplify the description
of abstract algorithms, to classify and evaluate data
structures, and to formally describe the type systems
of programming languages
Stack
• A stack is also known as a Last-In-First-Out (LIFO) list.
• A stack is a linear structure in which items are added or
removed only at one end the top.
• The depth of stack is the number of elements it contains.
• An empty stack has depth zero.
Stack Example
Stack Operations
• A stack is an ADT that supports four main methods:
• New():ADT-Creates a new stack
• Push(S:ADT,O:Element):ADT-Inserts object O onto top of stack S.
• Pop(S:ADT):ADT-Removes the top object of stack S; if the stack is
empty an error occur.
• Top/peek (S:ADT):element – Returns the top object of the stack,
without removing it; if the stack is empty an error occurs.
Stack Operations
• The following support methods could also be defined:
• Size(S:ADT):integer-Returns the number of objects in stack S
• IsEmpty(S:ADT):boolean-Indicates f stack S is empty(returns false)
Implementing Stack using Array

STACK

xxx yyy zzz

1 2 3 4 5 6 7 8

MAXSTK 8
TOP 3
PUSH()
• This procedure pushes an ITEM onto a stack

PUSH(STACK,TOP,MAXSTK,ITEM)
1. IF TOP = MAXSTK, then
Print : Overflow, and Return
2. Set TOP = TOP + 1 [Increase TOP by 1]
3. Set STACK[TOP]= ITEM [Insert ITEM in new TOP position]
4. Return
POP()
• This procedure deletes the top element of stack and
assigns it to the variable ITEM

POP(STACK,TOP,ITEM)
1. IF TOP = 0, then
Print : Underflow, and Return
2. Set ITEM = STACK[TOP] [Assigns TOP element to ITEM]
3. Set TOP= TOP - 1 [Decreases TOP by 1]
4. Return
Limitations
• Limitations

– The maximum size of the stack must be defined priority


and cannot be changed

– Trying to push a new element into a full stack causes an


implementation-specific exception

You might also like