0% found this document useful (0 votes)
23 views23 pages

Understanding Queue Data Structures

A queue is a first-in, first-out (FIFO) data structure where elements are inserted at the rear and deleted from the front. Elements are stored in an array with a front and rear pointer. Common operations on a queue include insert, which adds an element to the rear, and delete, which removes an element from the front. A queue can be implemented using an array or linked list. Circular queues improve on linear queues by overcoming the empty space issue when the queue is full.

Uploaded by

Shikhar Ashish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views23 pages

Understanding Queue Data Structures

A queue is a first-in, first-out (FIFO) data structure where elements are inserted at the rear and deleted from the front. Elements are stored in an array with a front and rear pointer. Common operations on a queue include insert, which adds an element to the rear, and delete, which removes an element from the front. A queue can be implemented using an array or linked list. Circular queues improve on linear queues by overcoming the empty space issue when the queue is full.

Uploaded by

Shikhar Ashish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Queue

• A queue is an ordered list in which insertions are done at one end (rear) and
deletions are done at other end (front).
• The first element to be inserted is the first one to be deleted.
• Hence, it is called First in First out (FIFO) or Last in Last out (LILO) list.
Queue
Struct queue
{
int front;
int rear;
int a[MAX];
}
typedef Struct queue Que
Queue
void create_empty_queue (Que *q)
{
rear =-1;
front =-1;
}
int is_full (Que *q) int is_empty (Que *q)
{
{
if(front == rear ==-1)
if(rear ==MAX-1) return 1;
return 1; else
else return 0;
}
return 0;
}
void insert_queue (Que *q, int item)
{
if (is_full(q))
{
printf(“Overflow”);
exit();
}
if (is_empty(q))
{
rear ++;
front ++;
}
else
rear++;
a[rear] = item;
}
Time complexity = O(1)
int delete_queue (Que *q)
{
if (is_empty(q))
{
printf(“Underflow”);
exit();
}
temp = a[front];
if (front == rear)
{
front =-1;
rear =-1;
}
else
front ++;
return temp;
Time complexity = O(1)
}
Write an algorithm to implement queue using two
stacks
void insert (int item)
{
push(S, item);
}
int delete()
{
int i, temp;
while (S1  top !=0)
{ i=pop(S1);
push(S2,i);
}
temp = pop(S1);
while(!is_empty(S2))
{ i = pop(S2);
push (S1,i);
}
return temp;
}
Linked List Representation of queue
Insertion in queue
void insert(struct node *ptr, int item)    if (front == NULL)  
        {  
{      
            front = ptr; 
    ptr = (struct node *) malloc (sizeof(struc             rear = ptr;   
t node));               front -> next = NULL;  
    if(ptr == NULL)               rear -> next = NULL;  
    {           }  
        printf("\nOVERFLOW\n");       else   
        {  
        return;               rear -> next = ptr;  
    }               rear = ptr;  
    else               rear->next = NULL;  
    {            }  
    }  
        ptr -> data = item;   }   
To do
Deletion in queue using linked list
Drawback of linear queue
In a normal Queue, we can insert elements until queue becomes full.
But once queue becomes full, we can not insert the next element even if
there is a space in front of queue.
Circular Queue
• A queue, in which the last node is connected back to the first node to
form a cycle, is called as circular queue.
• Circular queue are the queues implemented in circular form rather than
in a straight line.
Circular Queue
int isFull() {
if ((front == rear + 1) || (front ==0 && rear == SIZE -1))
return 1;
else
return 0;
}

int isEmpty() {
if ( front == -1 )
return 1;
else
return 0;
}
void enQueue(int element)
{
if ( isFull() )
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
exit();
}
else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
}
// Q has only one element, so we reset the queue after dequeuing it.
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
Priority Queue
• Priority Queue is similar to a queue, and every element has some priority value
associated with it.
• The priority of the elements in a priority queue determines the order in which
elements are served (i.e., the order in which they are removed).
• If in any case the elements have same priority, they are served as per their
ordering in the queue.
• A priority queue is a container of elements, each having an associated key.
• Insert (key, data): Inserts data with key to the priority queue. Elements are ordered
based on key.
• DeleteMin/DeleteMax: Remove and return the element with the smallest/largest
key.
• GetMinimum/GetMaximum: Return the element with the smallest/largest key
without deleting it.
Applications of Queues
• There are several algorithms that use queues to solve problems. For
example, BFS, traversing of a binary tree, etc.
• Round robin scheduling for process scheduling is implemented using
queues.
• Jobs sent to a printer are places on a queue.
• Every real-life line is a queue. For example, lines at ticket counters.
List, Set, Tuple and Dictionary
• Lists: are just like dynamic sized arrays, declared in other languages (vector in C++
and ArrayList in Java). Lists need not be homogeneous always which makes it the
most powerful tool in Python.
• Tuple: A Tuple is a collection of Python objects separated by commas. In some ways,
a tuple is similar to a list in terms of indexing, nested objects, and repetition but a
tuple is immutable, unlike lists that are mutable.
• Set: A Set is an unordered collection data type that is mutable, and has no duplicate
elements. Python’s set class represents the mathematical notion of a set.
• Dictionary: Dictionaries in Python is an ordered collection of data values, used to
store data values like a map, which, unlike other Data Types that hold only a single
value as an element, Dictionary holds key:value pair. Key-value is provided in the
dictionary to make it more optimized.
Collection VS
Features Mutable Ordered Indexing Duplicate Data

List ✔ ✔ ✔ ✔

Tuple X ✔ ✔ ✔

Set ✔ X X X

Dictionary ✔ ✔ ✔ X

Common questions

Powered by AI

Queue operations, such as insertion and deletion, in both array-based and circular queues typically have a time complexity of O(1), due to direct index access . Linked list-based queues also maintain O(1) complexity for enqueue and dequeue operations, since updates simply adjust pointers. However, the linked list overhead due to dynamic memory allocations can add slight computational overhead compared to static array implementations. Circular queues optimize space complexities in fixed-size environments, eliminating waste seen in simple arrays post-deletion .

In a simple queue, insertions are conducted at the rear and deletion at the front, without any wrapping around, which can result in wasted space once it reaches maximum capacity if not reset . Conversely, a circular queue treats the queue as circular rather than linear, allowing the rear to wrap around to the beginning of the array when it reaches the end, thus utilizing the available space more efficiently .

The primary drawback of a normal queue with arrays is that when the rear reaches the end of the queue, no further elements can be enqueued even if there is available space at the front due to prior deletions. A circular queue addresses this limitation by making the end of the array wrap around to the beginning, allowing for continuous enqueuing operations as long as the queue is not entirely filled, thereby optimizing space usage .

Implementing a queue using a linked list provides dynamic memory allocation which allows the queue to grow as needed without a predetermined size. This circumvents the issues of fixed capacity and potential overflow found in arrays. Moreover, linked lists do not suffer from the drawback of wasted space due to fixed positioning of front and rear, providing more efficient use of memory when the queue experiences a mix of insertions and deletions .

Priority queues are beneficial in scenarios where elements need to be processed according to their priority rather than their order of arrival. This is particularly useful in scheduling algorithms where processes are prioritized to optimize CPU usage, or in network routing where urgent packets need immediate transmission over less important ones. Unlike regular queues, which follow FIFO, priority queues can help in improving efficiency and service time by allowing high-priority tasks to be executed first .

A priority queue determines which elements are dequeued first based on an assigned priority level, rather than their order of insertion. This prioritization allows elements with higher urgency or importance to be processed before those with lower priority. In applications like task scheduling and resource allocation, this ensures optimal utilization of resources by attending to critical tasks first, potentially improving throughput and response times in time-sensitive environments .

Linked list queues handle the overflow issue by dynamically allocating memory for each new element, thereby theoretically avoiding overflow until system memory limits are reached. This contrasts with array-based queues, where overflow occurs once the fixed-size array is completely filled. Hence, linked lists provide a flexible alternative to manage varying queue sizes, while arrays demand pre-allocation and may lead to wasted space if pre-estimated sizes seldom match actual needs .

A circular queue is often preferred because it eliminates the wasted space problem in a linear queue. In a linear queue, once the rear pointer reaches the end of the queue, a full condition can occur even when space is available at the front due to prior deletions. The circular nature allows the queue to utilize this space effectively by wrapping the rear around to the beginning, thus ensuring all available slots are used before reporting a 'full' state .

In a linked list implementation of a queue, deletion is performed at the front of the list. The front pointer is advanced to the next node, effectively removing the front node from the queue. If the queue becomes empty after the deletion, both the front and rear pointers are reset. This operation efficiently manages memory since each node can be freed immediately after deletion .

Implementing a queue using two stacks allows for maintaining the queue's order by utilizing stack properties. The primary trade-off is an increase in time complexity for dequeue operations, as elements must be flipped between stacks to maintain the correct order (from first inserted to first removed), which can lead to up to O(n) time complexity. Despite efficient storage, this can lead to inefficiency for heavy dequeue operations compared to O(1) dequeues in standard queue implementations .

You might also like