0% found this document useful (0 votes)
18 views

Liner Queue Copy. 1

Uploaded by

forg28826
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Liner Queue Copy. 1

Uploaded by

forg28826
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

lOMoARcPSD|21950806

CS3CRT09-Data Structures using C++ Unit 2

this method is inefficient use of array space. A stack push operation may result in stack overflow even
if there is space available in the array.

Method 2 (A space efficient implementation)


This method efficiently utilizes the available space. It doesn’t cause an overflow if there is
space available in arr[]. The idea is to start two stacks from two extreme corners of arr[]. stack1 starts
from the leftmost element, the first element in stack1 is pushed at index 0. The stack2 starts from the
rightmost corner, the first element in stack2 is pushed at index (n-1). Both stacks grow (or shrink) in
opposite direction.

QUEUES

Queue is a linear data structure in which the insertion and deletion operations are performed at
two different ends. It is a collection of similar data items in which insertion and deletion operations
are performed based on FIFO (First In First Out) principle. In a queue data structure, adding and
removing of elements are performed at two different positions. The insertion is performed at one end
and deletion is performed at other end. In a queue data structure, the insertion operation is performed
at a position which is known as REAR and the deletion operation is performed at a position which is
known as FRONT.
Example
Queue after inserting 25, 30, 51, 60 and 85.

Operations on Queues

The following operations are performed on a queue data structure...


 enQueue(value) - (To insert an element into the queue)
 deQueue() - (To delete an element from the queue)
 display() - (To display the elements of the queue)
Queue data structure can be implemented in two ways. They are as follows...
1) Using Array
2) Using Linked List
When a queue is implemented using array, that queue can organize only limited number of elements.
When a queue is implemented using linked list, that queue can organize unlimited number of elements.
Queue Using Array
A queue data structure can be implemented using one dimensional array. But, queue
implemented using array can store only fixed number of data values. The implementation of queue
data structure using array is very simple, just define a one dimensional array of specific size and insert
or delete the values into that array by using FIFO (First In First Out) principle with the help of
variables 'front' and 'rear'. Initially both 'front' and 'rear' are set to -1. Whenever, we want to insert a
new value into the queue, increment 'rear' value by one and then insert at that position. Whenever we
want to delete a value from the queue, then increment 'front' value by one and then display the value at
'front' position as deleted element.

8
Downloaded by Sreya S ([email protected])
lOMoARcPSD|21950806

CS3CRT09-Data Structures using C++ Unit 2

enQueue(value) - Inserting value into the queue


In a queue data structure, enQueue() is a function used to insert a new element into the queue.
In a queue, the new element is always inserted at rear position. The enQueue() function takes one
integer value as parameter and inserts that value into the queue. We can use the following steps to
insert an element into the queue...

Step 1: [Check whether queue is FULL]


If (REAR == SIZE-1) then
Display "Queue is FULL. Insertion is not possible."
Exit.
Step 2: set REAR = REAR + 1
set QUEUE[REAR] = value.
Step 3: Exit

deQueue() - Deleting a value from the Queue


In a queue data structure, deQueue() is a function used to delete an element from the queue. In a
queue, the element is always deleted from front position. The deQueue() function does not take any
value as parameter. We can use the following steps to delete an element from the queue...

Step 1: [Check whether queue is EMPTY]


If (FRONT == REAR)then
Display "Queue is EMPTY. Deletion is not possible."
Exit.
Step 2: set value = QUEUE[FRONT]
Step 3: If (FRONT == REAR)then
set FRONT = -1
set REAR = -1
else
set FRONT = FRONT + 1
step 4. Exit.

display() - Displays the elements of a Queue


We can use the following steps to display the elements of a queue.

Step 1: [Check whether queue is EMPTY]


If (FRONT == REAR)then
Display "Queue is EMPTY"
Exit.
Step 2: set i = FRONT+1.
Step 3: Repeat step 4 until i <= REAR
Step 4: Display 'queue[i]'
set i=i+1
step 5. Exit.

CIRCULAR QUEUE
Circular Queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make a
circle.
Graphical representation of a circular queue is as follows.

9
Downloaded by Sreya S ([email protected])
lOMoARcPSD|21950806

CS3CRT09-Data Structures using C++ Unit 2

Limitations of Linear Queue


In a normal Queue Data Structure, we can insert elements until queue becomes full. But once if
queue becomes full, we cannot insert the next element until all the elements are deleted from the
queue. For example consider the queue below.
After inserting all the elements into the queue.

Now consider the following situation after deleting three elements from the queue.

In this case even if the queue is not full we cannot inset elements into that queue.
Operations on Circular Queue
enQueue(value) - Inserting value into the Circular Queue
In a circular queue, enQueue() is a function which is used to insert an element into the circular
queue. In a circular queue, the new element is always inserted at rear position. The enQueue() function
takes one integer value as parameter and inserts that value into the circular queue. We can use the
following steps to insert an element into the circular queue.

Step 1: [Check whether queue is FULL]


If (FRONT = REAR+1 % SIZE) then
Display "Queue is FULL. Insertion is not possible."
Exit.
Step 2: If (FRONT = -1)
Set FRONT = 0
Set REAR = 0
Else
REAR = (REAR+1) % SIZE
CQUEUE[REAR] = value
Step 3: Exit.

10
Downloaded by Sreya S ([email protected])
lOMoARcPSD|21950806

CS3CRT09-Data Structures using C++ Unit 2

deQueue()- Deleting a value from the Circular Queue


In a circular queue, deQueue() is a function used to delete an element from the circular queue.
In a circular queue, the element is always deleted from front position. The deQueue() function doesn't
take any value as parameter. We can use the following steps to delete an element from the circular
queue.

Step 1: [Check whether queue is EMPTY]


If (FRONT = -1) then
Display "Queue is EMPTY. Deletion is not possible."
Exit.
Step 2: value=CQUEUE[FRONT]
Step 3: If (FRONT = REAR) then
Set FRONT = -1
Set REAR = -1
Else
FRONT = (FRONT +1) % SIZE
Step 4: Exit.

DOUBLE ENDED QUEUE (DEQUEUE)


Double Ended Queue (Dequeue) is also a Queue data structure in which the insertion and
deletion operations are performed at both the ends (front and rear). That means, we can insert at both
front and rear positions and can delete from both front and rear positions.

Double Ended Queue can be represented in TWO ways, those are as follows.
 Input Restricted Double Ended Queue
 Output Restricted Double Ended Queue
Input Restricted Double Ended Queue
In input restricted double ended queue, the insertion operation is performed at only one end and
deletion operation is performed at both the ends.

Output Restricted Double Ended Queue


In output restricted double ended queue, the deletion operation is performed at only one end and
insertion operation is performed at both the ends.

11
Downloaded by Sreya S ([email protected])
lOMoARcPSD|21950806

CS3CRT09-Data Structures using C++ Unit 2

PRIORITY QUEUE
Priority queue is a variant of queue data structure in which insertion is performed in the order
of arrival and deletion is performed based on the priority.
There are two types of priority queues they are as follows.
1) Max Priority Queue
2) Min Priority Queue
1. Max Priority Queue
In max priority queue, elements are inserted in the order in which they arrive the queue and
always maximum value is removed first from the queue. For example assume that we insert in order 8,
3, 2, 5 and they are removed in the order 8, 5, 3, 2.
2. Min Priority Queue
Min Priority Queue is similar to max priority queue except removing maximum element first,
we remove minimum element first in min priority queue.

12
Downloaded by Sreya S ([email protected])

You might also like