0% found this document useful (0 votes)
18 views4 pages

Quee Using Arry Lab 5

This document outlines a lab session for implementing a queue using arrays as part of a BS Cyber Security and Digital Forensics course. It details the objectives, introduction to queues, algorithm for implementation, and provides a code example in C++. The lab aims to help students understand basic queue operations such as enqueue, dequeue, and displaying queue contents.

Uploaded by

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

Quee Using Arry Lab 5

This document outlines a lab session for implementing a queue using arrays as part of a BS Cyber Security and Digital Forensics course. It details the objectives, introduction to queues, algorithm for implementation, and provides a code example in C++. The lab aims to help students understand basic queue operations such as enqueue, dequeue, and displaying queue contents.

Uploaded by

alsamadlqp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

The Jslamia Unibersity of Bahamalpur

Faculty of Engineering

BS Cyber Security and Digital Forensics 3rd semester


Lab#5 Queue implementation using arrays
Course: Data Structures and Algorithmns lab Date:8 Jan,2025
Submitted by: M Sohaib Ali Roll No. 46

Objectives

The purpose of this lab session is to understand the implementation of a queue using an array with its basic
operations.

Introduction
A Queue is an ordered collection of itemns from which items may be deleted at one end (called the front of
the queue) and inserted at the other end (the rear of the queue). Queues remember things in first-in-first-
out (FIFO) order. The basic operations in a queue are: Enqueue- Adds an item to the end of queue.Dequeue
- Removes an item from the front. A queue is implemented using a one dimensional array. FRONT is an
integer value, which contains the array index of the front element of the array. REAR is an integer value,
which contains the array index of the rear element of the array. When an element is deleted from the queue,
the value of HEAD is increased by one, i.e. HEAD = HEAD + 1. When an element is inserted into the queue,
the value of TAIL is increased by one, i.e. TAIL=TAIL+1.

Figure 1: Representation of a Queue


Algorithm:
1. Initialize an array of size n, and the two variables associated with queue, front and rear to-1.
2. To insert an element into the queue:
a.First check whether the queue is full by the condition rear=max - 1; if it is, then print an error message
and exit.
b. Check whether the queue is initially empty by the condition front =-1 or front> rear: if it is, then increment
front by 1.
c.Increment rear by 1 and insert the element in the rear position.
3. To delete an element from the queue:
a.First check whether the queue is empty by the condition front=-1 or front > rear; if it is, then print an
error message and exit.
b.Increment front by 1 to delete the first inserted element.
4.Display the queue contents by printing the elements from front to rear.
Tasks:

1.Implement a queue using the algorithm provided above.

Code:

#include <iostream>
using namespace std;
#define MAX 100

class Queue{
private:
int arr[MAX];//Array to store the queue elements
int front;// Index of thefront element
int rear;// Index of the rear element

public:
//Constructor to initialize the queue
Queue(){
front=-1;
rear=-1;
}

//Function to insert an element into the queue


void enqueue(int element){
if(rear==MA
xx-1){
cout << "Error: Queue is full.""ndl;
return;
}
<(front=-1){
front = 0;//If queue is initially empty, set front to 0
}
rear++;
arctan[rear]=element;

//Function to delete an element from the queue


void dequeue(){
if(front=-1||front>rear){
cout << "Error: Queue is empty."<<endl;
return;
}
front++;
}

//Function to display the queue contents


void display(){
if(front=-1||front>ea){
cot∠ωtiωt<<<<<<<<<<<<<ueueisempty. <<endl;
return;
}

+λ{

cot ′ ⁡t < arcsin⁡t ′


}
cout<<endl;

int main(){
#define MAX 100

}
};

int main(){
Queue q;

// Insert elements into the queue


q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);

// Display the queue contents


cout <<"Queue contents:";
q.display();

//Delete an element from the queue


q.dequeue();

// Display the queue contents after deletion


cout <<"Queue contents after
deletion:";q.display();

return 0;


Output:

Output

Queue contents: 10 20 30 40
Queue contents after deletion: 20 30 40

You might also like