Download presentation
Presentation is loading. Please wait.
1
Queue data structure
2
Introduction to Queue Queue ADT (Queue as Abstract Data Type)
Features and operations Logical or mathematical views Not implementation
3
Example of Queue Queue at the Grocery store
Queue while boarding a airplane First in First out - FIFO
4
Defining Queue ADT A list with the restriction that insertion can be performed at one end (tail or rear) and deletion can be performed at other end (front or head). FIFO
5
Operations 1. Enqueue (x) or Push(x) or Insert (x)
2. Dequeue () or Pop() or Remove () 3. Front () 4. IsEmpty() 5. IsFull() All these operations can be performed in constant time or O(1)
6
Operations Enqueue (2) Enqueue (10) Enqueue (5) Enqueue (7)
IsEmpty false Front() 2 2 10 5 7 8 13 Front Rear
7
Operations Enqueue (2) Enqueue (10) Enqueue (5) Enqueue (7)
IsEmpty false Front() 2 Dequeue() 10 5 7 8 13 Front Rear
8
Operations Enqueue (2) Enqueue (10) Enqueue (5) Enqueue (7)
IsEmpty false Front() 2 Dequeue() 5 7 8 13 Front Rear
9
Operations Enqueue (2) Enqueue (10) Enqueue (5) Enqueue (7)
IsEmpty false Front() 2 Dequeue() 7 8 13 Front Rear
10
Applications of Queue Printer shared in a network
Process scheduling in a processor Wait scenario simulation
11
Array based implementation of Queue data structure
12
// Pop operation to remove an element from Front of Queue.
void Dequeue() { if(Front == -1 && Rear == -1) { // If Queue is empty, pop should throw error. printf("Error: No element to pop\n"); return; } else if(Front==Rear){ Front = Rear = -1; else Front++; // Front operation to return element at Front of Queue. int FrontQ() return A[Front]; // This function will return 1 (true) if Queue is empty, 0 (false) otherwise int IsEmpty() if(Front == -1 && Rear == -1) return 1; return 0; // This will print all the elements in the Queue at any stage. void Print() { int i; int count = (Rear-Front)+ 1; printf("Queue: "); for(i = 0;i<count;i++){ int index = (Front+i); printf("%d ",A[index]); printf("\n"); #include<stdio.h> #define MAXSIZE 100 int A[MAXSIZE]; // integer array to store the Queue int Front = -1; // variable to mark Front of Queue in array int Rear = -1; // variable to mark Rear of Queue in array // Enqueue operation to insert an element at the end of Queue. void Enqueue(int x) { if(Rear == MAXSIZE -1) { // overflow case. printf("Error: Queue overflow\n"); return; } else if (IsEmpty()){ Front = Rear = 0; A[Rear] = x; else { Rear = Rear+1; int main() { // calling Print() after each Enqueue or pop to see the state of Queue. Enqueue(3);Print(); Enqueue(5);Print(); Enqueue(7);Print(); Dequeue();Print(); Enqueue(9);Print(); }
13
Linked list based implementation of Queue data structure
14
#include<stdio. h> #include<stdlib
#include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node* next; }; // Two glboal variables to store address of front and rear nodes. struct Node* front = NULL; struct Node* rear = NULL; // To Enqueue an integer void Enqueue(int x) { struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); temp->data =x; temp->next = NULL; if(front == NULL && rear == NULL){ front = rear = temp; return; } rear->next = temp; rear = temp; // To Dequeue an integer. void Dequeue() { struct Node* temp = front; if(front == NULL) { printf("Queue is Empty\n"); return; } if(front == rear) { front = rear = NULL; else { front = front->next; free(temp); int Front() { printf("Queue is empty\n"); return front->data; void Print() { while(temp != NULL) { printf("%d ",temp->data); temp = temp->next; printf("\n"); int main(){ Enqueue(3); Print(); Enqueue(5); Enqueue(7); Dequeue(); Enqueue(9); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.