










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is the notes of queue data structures
Typology: Study notes
1 / 18
This page cannot be seen from the preview
Don't miss anything!











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;
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());
Algorithm for dequeue()
print()
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
#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
CIRCULAR QUEUE What is Circular Queue?
1. Enqueue Operation in Circular Queue
/// 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
More About Priority Queue in C Programming