Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Queue Data structure, Study notes of Computer Programming

This is the notes of queue data structures

Typology: Study notes

2022/2023

Available from 09/02/2024

shubham-bisht-3
shubham-bisht-3 🇮🇳

1 document

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
QUEUE
1
QUEUE
Created
Reviewed
Unlike arrays, where insertion and deletion can happen at any end.
In Queues, insertion (Enqueue) and deletion (Dequeue) can happen at only one end each.
Insertion happens at the rear end and deletion happens at the front end.
Queues follow FIFO First in First out structure, i.e. the element added first (Enqueued) will go out of the queue
first(Dequeued)
Unlike stack, which follows, LIFO, last in first out, and stack where both insertion and deletion happens as one end.
For queue insertion(Enqueue) and deletion(Dequeue) happens at opposite ends.
Queue Operations
Enqueue: Adding a new item to the Queue Data Structure, in other words, enqueuing new item to Stack DS.
If the Queue is full, then it is said to be in an overflow condition
bool isFull()
{
// If the rear variable is equal to SIZE - 1 then the queue is full
if (rear == SIZE - 1)
return true;
// Otherwise
else
return false;
}
if(isFull() == true)
{
"Cannot insert element";
return;
}
// Function to check if the queue is full
if(front==-1)
{
front = 0;
}
rear = rear+1;
queue[rear] = new_element_value;
return;
The time complexity of this operation is O(1).
@October 1, 2023 12:57 PM
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Queue Data structure and more Study notes Computer Programming in PDF only on Docsity!

QUEUE

Created

Reviewed

Unlike arrays, where insertion and deletion can happen at any end.

In Queues, insertion (Enqueue) and deletion (Dequeue) can happen at only one end each.

Insertion happens at the rear end and deletion happens at the front end.

Queues follow FIFO First in First out structure, i.e. the element added first (Enqueued) will go out of the queue

first(Dequeued)

Unlike stack, which follows, LIFO, last in first out, and stack where both insertion and deletion happens as one end.

For queue insertion(Enqueue) and deletion(Dequeue) happens at opposite ends.

Queue Operations

Enqueue: Adding a new item to the Queue Data Structure, in other words, enqueuing new item to Stack DS.

If the Queue is full, then it is said to be in an overflow condition

bool isFull() { // If the rear variable is equal to SIZE - 1 then the queue is full if (rear == SIZE - 1) return true; // Otherwise else return false; } if(isFull() == true) { "Cannot insert element"; return; } // Function to check if the queue is full if(front==-1) { front = 0; } rear = rear+1; queue[rear] = new_element_value; return;

The time complexity of this operation is O (1).

@October 1, 2023 12:57 PM

Dequeue: Removing an item from the Queue, i.e. dequeuing an item out.

If a Queue is empty then it is said to be in an underflow condition

bool isEmpty() { // If both front and rear are -1 then the queue is empty if (front == -1 && rear ==-1) return true; // Otherwise else return false; } if(isEmpty() == true) { "Cannot delete element"; return; } queue[front] = 0; front = front + 1; // if the list is now empty if(front>rear) { front = -1; rear = -1; } return;

// Printing the element in the front of the queue printf("\nThe element in the front of the queue is %d\n", queue[front]); } } // Function to check if the queue is empty bool isEmpty() { // If both front and rear are -1 then the queue is empty if (front == -1 && rear ==-1) return true; // Otherwise else return false; } // Function to check if the queue is full bool isFull() { // If the rear variable is equal to SIZE - 1 then the queue is full if (rear == SIZE - 1) return true; // Otherwise else return false; } // Function to get the number of items in the queue int size() { // If the queue is empty, return 0 if(isEmpty() == true){ return 0; } // The number of items in the queue is the difference between front and rear return rear - front + 1; } // Function to enqueue or push an element in the queue void Enqueue(int val) { // If the queue is full then we cannot push an new element // We return that the overflow condition has been encountered if (isFull() == true) { printf("\nOverflow Condition Encountered\n"); } // Otherwise we push the new element else { // If the queue was initally empty // We add the first element in the queue and update front if (front == -1) { a front = 0; } // We update the rear pointing to the last element of the queue rear++; // Insert the element in the rear position queue[rear] = val; printf("%d was enqueued/pushed in the queue\n", Val initially } // Function to dequeue or pop an element from the queue void Dequeue() { // If the queue was empty we cannot pop any element out of it // We return that the underflow condition has been encountered if (isEmpty() == true)

printf("\nUnderflow Condition Encountered\n"); } // Otherwise we pop the element in the front else { // Print the popped element printf("Dequeued/Popped : %d\n", queue[front]); // Increase the index of the front front++; // Resetting the queue when the last item is popped from the queue if (front > rear) { // Assigning both the front and the rear - front = rear = -1; } } } // Function to print the queue void display() { // If the queue is already empty if (rear == -1) { printf("Queue is empty\n"); } // Otherwise else { // A variable to help in iteration through the queue. int i; printf("\nPrinting the Queue :\n"); // Printing all the elements of the queue for (i = front; i <= rear; i++) { printf("%d ", queue[i]); } printf("\n"); } } int main() { // Checking if the queue is empty if (isEmpty()) printf("The queue is empty\n"); // Assigning elements to the queue Enqueue(1); Enqueue(28); Enqueue(5); Enqueue(64); // Printing the first element of the queue peek(); // Printing the size of the queue printf("The size of the queue is %d\n", size()); // Printing the queue display(); // Popping the elements from the queue Dequeue(); Dequeue(); // Printing the size of the queue printf("The size of the queue is %d\n", size());

NEW_N->NEXT = NULL

IF((FRONT == NULL)&&(REAR == NULL))

FRONT = REAR = NEW_N

ELSE

REAR->NEXT = NEW_N

REAR = NEW_N

Algorithm for dequeue()

STRUCT NODE *TEMP

TEMP = FRONT

IF((FRONT == NULL)&&(REAR == NULL))

RETURN

ELSE

FRONT = FRONT->NEXT

FREE(TEMP)

print()

STRUCT NODE* TEMP

IF((FRONT == NULL)&&(REAR == NULL))

RETURN

ELSE

TEMP = FRONT

WHILE(TEMP)

RETURN TEMP->DATA

TEMP = TEMP->NEXT

IMPLEMENTATION OF QUEUE USING LINKED LIST #include <stdio.h> #include <stdlib.h> struct node { int data; struct node* next; };//defining linked list to implement queue struct node front = NULL; struct node rear = NULL; void enqueue(int data)//function to insert a node in queue { struct node new_n; new_n = (struct node)malloc(sizeof(struct node)); new_n->data = data;

new_n->next = NULL; if((front == NULL)&&(rear == NULL)){ front = rear = new_n; } else{ rear->next = new_n; rear = new_n; } } void dequeue()//function to delete an element from a queue { struct node temp; temp = front; if((front == NULL)&&(rear == NULL)){ printf("\nQueue is Empty"); } else{ front = front->next; free(temp); } } void display()//function to display the queue { struct node temp; if((front == NULL)&&(rear == NULL)){ printf("\nQueue is Empty"); } else{ temp = front; while(temp){ printf(" %d ",temp->data); temp = temp->next; } } } int main()//main function to use all our declared function { enqueue(5); enqueue(10); enqueue(15); enqueue(20); enqueue(25); printf("Queue:"); display(); printf("\nQueue After Dequeue:"); dequeue(); display(); } Output Queue: 5 10 15 20 25 Queue After Dequeue: 10 15 20 25

QUEUE USING STACK IN C

#include<stdio.h> #include<stdlib.h> #define N 20 //defining the size of queue int s[N], top = -1; int pop () //function to remove an element from stack

struct Queue { // Initializing the stacks stack stack1, stack2; // Function to implement enqueue void enqueue(int element) { // Pushing element in the stack stack1.push(element); } // Function to implement dequeue operation int dequeue() { // Condition where the queue is empty if (stack1.empty() && stack2.empty()) { cout << "the queue is empty"; } // Pushing all the elements of stack1 to stack if (stack2.empty()) { while (!stack1.empty()) { stack2.push(stack1.top()); stack1.pop(); } } // Popping the top element from the stack int element = stack2.top(); stack2.pop(); return element; } };

Time Complexity:

push operation: O(1) It is same as the push operation in the stack.

pop operation: O(N). In the worst case we have empty whole of stack 1 into stack 2.

Space Complexity: O(N) => N space for storing N elements in the stacks.

CIRCULAR QUEUE What is Circular Queue?

A circular queue is a linear queue that behaves as if the end position of the queue is connected to the start position,

forming a circular ring. It is also known as a Ring Buffer.

The queue pointers( Front pointer and Rear pointer) will start moving from 0 to N-1 (Maximum size of Queue = N) and

after reaching N-1, they come back to 0, again they start from 0,1.., upto N-1 and they continue moving around the Queue

round and round

1. Enqueue Operation in Circular Queue

The enqueue operation means inserting some element into the queue, with every valid enqueue operation the rear

pointer keeps incrementing.

Example 1: Insertion of element on empty Circular Queue.

If initially, the queue is empty then on the first insertion the front and rear will be set to the initial position and both will

point to the same element.

Example 2: Insertion of element when already there are some elements in Circular Queue.

In such a case, the rear pointer will keep incrementing for each insertion.

/// inn dono conditions ki jagah hum rear = (rear+1)%n bhi likh skte hai } // In the end insert the element at the rear position circularQueue[rear] = element; } // Function to delete element from the circular queue int deQueue() { int currentElement; // If the front is -1, means the circular queue haven't even been initialized till now // So there are no elements if (front == -1) { printf("Queue is Empty"); return -1; } // Return the deleted element currentElement = circularQueue[front]; circularQueue[front] = 0; // If the front and rear both were pointing to the same index, // Means there was only one element in the queue // Reset both pointers by assigning - if (front == rear && front!=-1) { front = -1; rear = -1; } // Else increase the front else { if (front == size - 1) front = 0; else front = front + 1; // in dono ki jagah hum front = (front+1)%n bhi likh skte hai // n is size of queue } return currentElement; } // Function to display the entire circular queue void display(int front, int rear) { // Return if queue is empty if (front == -1) { printf("Queue is empty\n"); return; } // Print the queue elements printf("Queue elements: "); if (front <= rear) { while (front <= rear) { printf("%d ", circularQueue[front]); front++; } } else { while (front <= size - 1)

printf("%d ", circularQueue[front]); front++; } front = 0; while (front <= rear) { printf("%d ", circularQueue[front]); front++; } } printf("\n"); } int main() { size = 6; // Let's create a queue of size 6 and store the base address of the array in the // variable named circularQueue circularQueue = (int *)malloc(sizeof(int) * size); info(front); info(rear); enQueue(8); info(front); info(rear); enQueue(5); info(front); info(rear); display(front, rear); enQueue(1); info(front); info(rear); enQueue(7); info(front); info(rear); display(front, rear); printf("Deleted Element: %d\n", deQueue()); info(front); info(rear); printf("Deleted Element: %d\n", deQueue()); info(front); info(rear); display(front, rear); return 0; } output front: - rear: - front: 0 rear: 0 front: 0 rear: 1 Queue elements: 8 5 front: 0 rear: 2 front: 0 rear: 3 Queue elements: 8 5 1 7 Deleted Element: 8

#include<stdio.h> #include<stdlib.h> struct node { int val; struct node *next; // to link the next node }; struct node *front=NULL; struct node *rear=NULL; void enqueue(int item); int dequeue(); int isEmpty(); void enqueue(int item) { struct node *newNode; newNode=(struct node *)malloc(sizeof(struct node)); if(rear->next == NULL){ printf("Memory Overflow\n"); return; } newNode -> val = item; newNode -> next = NULL; if(front == NULL) // checking whether the Queue is empty or not. { front = rear = newNode; } else { rear -> next = newNode; } rear = newNode; rear->next=front; } int dequeue() { struct node *frontNode; int deleted_item; if(isEmpty()) { printf("Queue Underflow\n"); exit(1); } frontNode = front; deleted_item = frontNode -> val; if(front == rear){ front = rear = NULL; } else{ front = front -> next; rear -> next = front; } free(frontNode); return deleted_item; } int isEmpty() { return ( (front == NULL)? 1 : 0); } void display() { struct node *ptr; if(isEmpty()) { printf("Queue is empty\n."); return;

ptr = front; printf("\nElements in the Queue are :"); while( ptr -> next != front ) { printf("%d, ", ptr -> val); ptr = ptr -> next; } printf("%d\n", ptr -> val); } int main() { // Insert elements : 42, 66, 35 enqueue(42); enqueue(66); enqueue(35); display(); // Delete all of them printf("Deleted Element is: %d\n",dequeue()); printf("Deleted Element is: %d\n",dequeue()); printf("Deleted Element is: %d\n",dequeue()); display(); // Insert 10 enqueue(10); display(); // Try deleting an element in an empty circular queue. printf("Deleted Element is: %d\n",dequeue()); return 0; } PRIORITY QUEUE

It is a special type of queue in which deletion and insertion perform on the basis of priority of data element. There can be

min priority queue and max priority queue.

Priority Queue can be implemented by three data Structure:-

Array

Linked List

Heap

More About Priority Queue in C Programming

Priority Queue is the special case of Queue.

In priority queue deletion and insertion base on priority of the data elements

In Queue there is priority for each element.

There is two types of Priority Queue min priority queue and max priority queue.