You are on page 1of 58

Unit 5: Linear Data Structures –Queue

Fundamentals of Data Structures

Mrs. Snehal Rathi


snehal.rathi@viit.ac.in
Department of Computer Engineering

BRACT’S, Vishwakarma Institute of Information Technology, Pune-48


(An Autonomous Institute affiliated to Savitribai Phule Pune University)
(NBA and NAAC accredited, ISO 9001:2015 certified)
Objective/s of this session

1. To impart the basic concepts of data structures and algorithms


2. To understand the different ways of data representation
3. To develop the ability to synthesize and analyze algorithms
4. To study linear data structures and their applications.
5. To enable students to write algorithms for solving problems with
the help of fundamental data structures.
6. To understand concepts about searching and sorting techniques.

Mrs. Snehal Rathi Department of Computer Engineering,


VIIT,Pune-48
Department of Computer Engineering, VIIT , Pune-48 2
Learning Outcome/Course Outcome

• After completion of the course, student will be able to


1. Understand arrays and their implementation with basics of algorithm analysis.
(Understand & Apply)
2. Analyze algorithms with respect to time and space complexity(Analyze)
3. Implement linear data structures using sequential and linked organization for
problem solving and programming.(Understand and Apply)
4. Solve the problems using advanced linear data structure stack (Apply)
5. Apply advanced linear data structure queue for problem solving and
programming. (Apply)
6. Select appropriate searching and/or sorting techniques in the application
development.(Evaluate)
Mrs. Snehal Rathi Department of Computer Engineering,
3
VIIT,Pune-48
Unit I :Introduction to Data Structures
• Concept of data,
• functions,
• difference between compiler and interpreter,
• Abstract Data Types (ADT),
• data structure and its types,
• Difference between individual variables against Data Structures,
• Array, Pointers,
• Analysis of algorithm(Frequency count method)

Mrs. Snehal Rathi Department of Computer Engineering,


4
VIIT,Pune-48
Unit II :Concept of sequential organization

• Concept of Linear data structures,


• Concept of ordered list,
• Storage representations of ordered list :row major, column major and
their address calculation.
• Representation of sparse matrix using arrays,
• application of array in polynomial representation
• Algorithm for sparse matrix addition, simple and fast transpose,
• String Manipulation.

Mrs. Snehal Rathi Department of Computer Engineering,


5
VIIT,Pune-48
Unit III :Linear Data Structure- Linked Lists

• Concept,
• Comparison of Sequential and linked organizations,
• Linked list as an ADT,
• Singly Linked List,
• Doubly Linked List
• Circular Linked Lists with all operations like Creations, insertion (all
positions), Deletion (all positions),
• Modify, Merge and Reverse(SLL).

Mrs. Snehal Rathi Department of Computer Engineering,


6
VIIT,Pune-48
Unit IV :Linear Data Structures –Stack
• Stacks: Concept of stack,
• Stack as an ADT,
• push and pop operation,
• Stack implementation using array and linked list,
• application of stack for expression conversion
• Recursion concept and use of internal stack.

Mrs. Snehal Rathi Department of Computer Engineering,


7
VIIT,Pune-48
Unit V :Linear Data Structures-Queue

• Concept of queue,
• Queue as an ADT,
• Linear queue implementation using array and linked list,
• Circular queue,
• Priority queue,
• Doubly ended queue,
• Multi-queues,
• Applications of Queue.

Mrs. Snehal Rathi Department of Computer Engineering,


8
VIIT,Pune-48
Unit VI :Searching, sorting and hashing techniques

• Linear & Binary search,


• Bubble sort,
• Selection sort,
• Insertion Sort,
• Merge Sort,
• Quick Sort & complexity Analysis of algorithms.
• Hash tables: Basic concepts, Hash function, collisions, Collision
resolution techniques, Applications.

Mrs. Snehal Rathi Department of Computer Engineering,


9
VIIT,Pune-48
Text Books :
1. Horowitz and Sahani, ―Fundamentals of Data Structures in C++,
University Press, ISBN 10: 0716782928 ISBN 13: 9780716782926.
2. R. Gillberg, B. Forouzan, ―Data Structures: A Pseudo code
approach with C, Cenage Learning, ISBN 9788131503140.
3. A. Tanenbaum, Y. Langsam, M. Augenstein: Fundamentals of Data
Structure, 2nd edition, Pearson Education, 2002, ISBN-81-7808-
670-0.

Mrs. Snehal Rathi Department of Computer Engineering,


10
VIIT,Pune-48
Unit V :Contents
• Concept of queue,
• Queue as an ADT,
• Linear queue implementation using array and linked list,
• Circular queue,
• Priority queue,
• Doubly ended queue,
• Multi-queues,
• Applications of Queue.

Mrs. Snehal Rathi Department of Computer Engineering,


11
VIIT,Pune-48
Introduction

A queue is an ordered collection of elements of same type.

A queue is an data structure in which insertion occurs at one end (the rear) and deletion occurs at the other end
(the front). 

Only the elements indicated by front and rear can be accessed. 

Thus a queue is a restricted access data structure similar to the stack.  Like a stack, the elements are related to
each other by the order in which they are placed on the queue

A queue is FIFO.
Queue as ADT
A queue is an abstract data type that supports five methods:

dequeue Remove and return the Object from the front of queue or return an error if the queue is empty.
Input: none Return: Object

enqueue Inserts an object at the rear of the queue.


Input: Object Return: none

isEmpty Return a boolean indicating if the queue is empty


Input: none Return: boolean

front Return the Object at the front of the queue without removing the Object. If the queue is empty, return an error.
Input: none Return: Object

size Return the number of Objects in the queue.


Input: none Return: int
e.g.:-
1) A Line of people waiting at a ticket counter to buy a ticket. 
Whoever gets in line first gets a ticket first. 

2)The storage structure that holds jobs for printing on a printer is a


queue. 

3)When an operating system (OS) runs in batch mode


(i.e. non-interactive), jobs to be processed are placed in a queue
awaiting execution. 

A queue is a First In, First Out (FIFO) data storage mechanism.

Sometimes the OS gives priority to smaller jobs/ important jobs in the queue, and
if so, it becomes a "priority" queue.
 
A queue of elements of some data type is finite sequence of
elements of that data type together with the following
operations:
1. Initialize a queue .
2. Determine if a queue is empty or not.
3. Determine if a queue is full or not.
4. Insert a new element after the last element in a queue, if it is
not full.
5. Retrieve the first element of a queue , if it is not empty.
6. Delete the first element in a queue, if it is not empty.
7. Display the queue.
• Insertion and deletion operations have special names in queues -
insertion is called "enqueue" and deletion is called "dequeue". 
• A queue, has a front and a rear.  Items are inserted at the rear of
the queue and removed from the front of the queue.
• Trying to remove an item off an empty queue is called
underflow.
• Trying to insert an item into full queue is called
Overflow.
Representation of a Queue as an array
• One of the major limitations of an array is that its size should be

fixed prior to using it.


•But the number of the elements of the queue keeps on changing as
the elements are either removed from the front end or added at the
rear end.

Front Rear
The Queue Operations
• A queue is like a line of people waiting for a bank teller. The queue has a
front and a rear.

$ $

Rear Front
The Queue Operations
• New people must enter the queue at the rear. it is usually called an
enqueue operation.

$ $

Front
Rear
The Queue Operations
• When an item is taken from the queue, it always comes from the front.
It is usually called a dequeue operation.

$ $

Front
Rear
Array Implementation
• A queue can be implemented with an array, as shown here. For
example, this queue contains the integers 4 (at the front), 8 and 6 (at
the rear).
Front Rear
[0] [1] [2] [3] [4] [5] ...
4 8 6
An array of integers
to implement a We don't care what's in
queue of integers this part of the array.
Array Implementation
• The easiest implementation also keeps track of the number of items in
the queue and the index of the first element (at the front of the queue),
the last element (at the rear).

3 size

0 first
[0] [1] [2] [3] [4] [5] ...
4 8 6 2 last
A Dequeue Operation
• When an element leaves the queue, size is decremented, and first
changes, too. 2 size

1 first

2 last

[0] [1] [2] [3] [4] [5] ...


4 8 6
An Enqueue Operation
• When an element enters the queue, size is incremented, and last
changes, too. 3 size

1 first

3 last

[0] [1] [2] [3] [4] [5] ...


8 6 2
At the End of the Array
• There is special behavior at the end of the array. For example, suppose
we want to add a new element to this queue, where the last index is [5]:

3 size

3 first

[0] [1] [2] [3] [4] [5] 5 last


2 6 1
Array Implementation

struct queue
{
int que[size];
int front,rear;
}q;
Q full()
qfull()
{
if(q.rear>=size-1)
return 1;
else
return 0;
}
Q empty():
int qempty()
{
if((q.front== -1)|| (q.front>q.rear))
return 1;
else
return 0;
}
Insert element to Q.
int insert(int item)
{
if(q.front == -1) //true only during first insertion
q.front++;
q.rear++;
q.que[q.rear]=item;
return q.rear;
}
Delete element from Q:
int delete_q()
{
int item;
item= q.que[q.front]; //Logical deletion
q.front++;
cout<<“The Deleted Item Is”<<item;
return q.front;
}
Display Q:
void display()
{
int i;
cout<<"Elements of queue are";
for(i=q.front;i<=q.rear;i++)
cout<<q.que[i];
}
 Implementation of Queue Using array.
Q using Link List:
Advantages of Link List
NO check for Q full
No wastage of memory.
Linked List Implementation

• A queue can also be implemented with a linked


list.
13
front

15

10

7
null

rear
Linked List Implementation

• Which end do you think is the front of the


queue? Why?
13

15

10

7
null
 Implementation of Queue Using Link List.
"Circular" Array implementation of a Queue
Draw backs of linear Q:

10 20 30 40 50 60

front rear
Formula’s used for setting front and Rear pointers for a
Circular Q.

rear = (rear+1) % size


front=(front + 1) % size
Ex. Size of C.Q. is 5
Rear=(4+1)%5
=0
So after fourth location we can store data at zero location
again.
front=(front+1)%size
=(3+1)%5
=4
Here u can delete data at 4 location after 3 location.
Circular array representation

A front index points to the item at the front of the queue; initially front = -1.
A rear index point to the last item on the queue;
initially rear = -1. (rear++ before enqueue)
When items are enqueued, the rear index is incremented modulo
QUEUE_SIZE.
When items are dequeued, the front index is incremented modulo
QUEUE_SIZE.
struct queue
{
int queue[size];
int front, rear;
}q;

Mrs. Snehal Rathi Department of Computer Engineering,


42
VIIT,Pune-48
int qfull()
{
if(q.front== (q.rear + 1)%size)
return 1;
else
return 0;
}

When rear is one position behind front then queue is full


int qempty()
{
if(q.front==-1)
return 1;
else
return 0;
}
void enqueue(int item)
{
if (qfull()) //queue is full
cout<<"the C.Q. is full";
else
{
if(q.front==-1) //queue is empty
q.front=q.rear=0;
else
q.rear=(q.rear+1)%size;
q.queue[q.rear]= item;
}
}
void dequeue( )
{
int item;
if(qempty()) //check for empty queue
cout<<"q is empty";
else {
item=q.queue[q.front]; //dequeue from front
if(q.front==q.rear) //last element of queue
{
q.front=q.rear = -1;
}
else
q.front=(q.front+1)%size;
cout<<"the deleted item is "<<item;
}
}
Display:

void displayQueue()
   else
{
{
if (q.front==-1)
for (int i=q.front;i<size;i++)
{
cout<<q.queue[i]<<endl;
cout<<"\n CQ is empty ";
for (int i=0;i<=q.rear;i++)
return;
cout<<q.queue[i]<<endl;
}
}
cout<<"\n Numbers are: \n";
}
if (q.rear>=q.front)
{
for(int i=q.front; i<=q.rear; i++)
cout<<q.queue[i]<<endl;
}

Mrs. Snehal Rathi Department of Computer Engineering,


47
VIIT,Pune-48
Priority Q:
The priority Q is a data structure in which the intrinsic ordering of the elements determine
the result of it’s basic operations.
There are two types:
Ascending priority Q:
Descending priority Q:
Sometimes we need to insert or delete element’s from other position than front or rear end.

Example of P.Q. is Job scheduling.


For example Real time jobs, hospitals where priorities are given for their execution.
Array Implementation of P.Q.:
Ascending priority Q:
It is a collection of items in which the items can be inserted
arbitrarily but only the smallest element can be removed
Descending Priority Q:
It is a collection of items in which insertion of element is in any order
but only the largest element can be removed.
Issues for Array Implementation of P.Q.
Examine all the elements of P.Q. and find out largest or smallest element for
deletion.
Removal of an element in middle of an array, which is complicated b’coz
It needs to shift all subsequent elements.
put some indicator like -1 to show empty status.
New insertion will lead to total unordering. Need to scan whole Q for
deletion.
Solution:
Elements are arranged in ascending or descending order after inserting it.
Linked list implementation of
Priority Queue
• Single list : Sorted nodes in single list
• Multiple lists: Each list represents same priority value.
• Insert nodes with priority 2 and 3

1 1 2 4 4

1 1

4 4
Multiple Queues using single array:
Suppose there is array of size n
If we divide it by 2 then it become n/2.
So first half of array is Queue1 and second half of array is
Queue2.
A simple way to implement k queues is to divide the array in k
slots of size n/k each, and fix the slots for different queues,
i.e., use arr[0] to arr[n/k-1] for the first queue, and arr[n/k]
to arr[2n/k-1] for queue2 where arr[] is the array to be
used to implement two queues and size of array be n.
Drawback:
• The problem with this method is an inefficient use of array space.
• An enqueue operation may result in overflow even if there is space
available in arr[].
• For example, consider k as 2 and array size n as 6. Let we enqueue 3
elements to first and do not enqueue anything to the second queue.
When we enqueue the 4th element to the first queue, there will be
overflow even if we have space for 3 more elements in the array.
• Solution: Need to use some extra arrays for keeping track of front,
rear and next element position.
Double Ended Queue (Deque)

• Double ended queue or simply called “Deque” is a generalized version


of Queue.
• The difference between Queue and Deque is that it does not follow
the FIFO (First In, First Out) approach. The second feature of Deque is
that we can insert and remove elements from either front or rear
ends.
Mrs. Snehal Rathi Department of Computer Engineering,
54
VIIT,Pune-48
Double Ended Queue Classification

• Input-restricted Deque: In input-restricted, deletion can be done


from both the ends but insertion can be done only at the rear end of
the queue.
• Output-restricted Deque: In the output-restricted queue, insertion
can be done from both the ends but deletion is done only at one end
i.e. the front end of the queue.

Mrs. Snehal Rathi Department of Computer Engineering,


55
VIIT,Pune-48
The following are the basic operations that
can be performed on deque.
• insert front: Insert or add an item at the front of the deque.
• insertLast: Insert or add an item at the rear of the deque.
• deleteFront: Delete or remove the item from the front of the queue.
• delete last: Delete or remove the item from the rear of the queue.
• getFront: Retrieves the front item in the deque.
• getLast: Retrieves the last item in the queue.
• isEmpty: Checks if the deque is empty.
• isFull: Checks if the deque is full.

Mrs. Snehal Rathi Department of Computer Engineering,


56
VIIT,Pune-48
Applications of Queue:
1. Job Scheduling:
O.S. use priority queue for scheduling
jobs(programs)
2. Categorizing Data:
Example of it is college library. Books are
arranged according to various departments.
3. Graph traversal ( BFS)
We learnt…
• Concept of queues as ADT, Implementation of linear and circular queue
using linked and sequential organization.
• Concept of multiqueues, double ended queue and priority queue.
• Applications of queues.

You might also like