Docsity

Queue Data Structure: Array Implementation and Applications, Lecture notes of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures | Level 2 | Lab 4
1 | P a g e
Queue Using Array
1- What is the Queue?
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is
open at both its ends. One end is always used to insert data (enqueue) and the other is used
to remove data (dequeue). Queue follows First-In-First-Out methodology (FIFO), i.e., the data
item stored first will be accessed first.
1- Applications of Queue data Structure:
- Queue is useful in CPU scheduling, Disk Scheduling.
- Queue is used for synchronization. Examples: IO Buffers, pipes, file IO, etc.
Queue Using Dynamic Array Code:
#include <iostream>
using namespace std;
int front,rear;
int size;
int *Queue;
bool isFull()
{
if (rear==size-1)
return true;
else
return false;
}
bool isEmpty()
{
if (front==-1 || front > rear)
return true;
else
return false;
}
void enqueue(int item)
{
if (isFull())
cout<<"Queue Overflow \n";
else
pf3

Partial preview of the text

Download Queue Data Structure: Array Implementation and Applications and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Queue Using Array

1- What is the Queue?

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is

open at both its ends. One end is always used to insert data (enqueue) and the other is used

to remove data (dequeue). Queue follows First-In-First-Out methodology (FIFO), i.e., the data

item stored first will be accessed first.

1 - Applications of Queue data Structure :

  • Queue is useful in CPU scheduling, Disk Scheduling.
  • Queue is used for synchronization. Examples: IO Buffers, pipes, file IO, etc.

Queue Using Dynamic Array Code:

#include using namespace std; int front,rear; int size; int *Queue; bool isFull() { if (rear==size-1) return true; else return false; } bool isEmpty() { if (front==-1 || front > rear) return true; else return false; } void enqueue(int item) { if (isFull()) cout<<"Queue Overflow \n"; else

if (front==-1) front=0; rear++; Queue[rear]=item; } } int dequeue() { if(isEmpty()) { cout<<"Queue Underflow \n"; return - 1; } else { int item=Queue[front]; front++; return item; } } void traverse() { if(isEmpty()) cout<<"Queue is Empty \n"; else { for(int i=front;i<=rear;i++) cout<<Queue[i]<<" "; } cout<<endl; } void main() { int ch,item; front=rear=-1; cout<<"Enter Queue Size"; cin>>size; Queue=new int [size]; while(true) { cout<<"***************************************\n"; cout<<"1) Insert element to Queue \n"; cout<<"2) Delete element from Queue \n"; cout<<"3) Display all the elements of queue \n"; cout<<"4) Exit \n"; cout<<"***************************************\n"; cin>>ch; switch (ch) { case 1 : cout<<"Enter the item:"; cin>>item; enqueue(item); break; case 2 : cout<<dequeue()<<" Deleted from the Queue \n"; break;