Professional Documents
Culture Documents
• 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).
• 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.
A queue is an data structure in which insertion occurs at one end (the rear) and deletion occurs at the other end
(the front).
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
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
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
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
1 first
3 last
3 size
3 first
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
15
10
7
null
rear
Linked List Implementation
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.
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;
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;
}
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)