Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

3 - Linear Queue Represented Using A Linear Array

The document discusses implementing a queue using an array. It explains initializing front and rear pointers, and implementing enqueue, dequeue, front and display methods. Code is provided to demonstrate a queue class with these methods.

Uploaded by

vijipersonal2012
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

3 - Linear Queue Represented Using A Linear Array

The document discusses implementing a queue using an array. It explains initializing front and rear pointers, and implementing enqueue, dequeue, front and display methods. Code is provided to demonstrate a queue class with these methods.

Uploaded by

vijipersonal2012
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Write a program to demonstrate the implementation of various

operations on a linear queue represented using a linear array.


Implement Queue using Array
To implement a queue using an array,
 create an array arr of size n and
 take two variables front and rear both of which will be initialized to 0 which means the
queue is currently empty.
 Element
 rear is the index up to which the elements are stored in the array and
 front is the index of the first element of the array.
Now, some of the implementations of queue operations are as follows:
 Enqueue: Addition of an element to the queue. Adding an element will be performed after
checking whether the queue is full or not. If rear < n which indicates that the array is not
full then store the element at arr[rear] and increment rear by 1 but if rear == n then it is said
to be an Overflow condition as the array is full.
 Dequeue: Removal of an element from the queue. An element can only be deleted when
there is at least an element to delete i.e. rear > 0. Now, the element at arr[front] can be
deleted but all the remaining elements have to shift to the left by one position in order for
the dequeue operation to delete the second element from the left on another dequeue
operation.
 Front: Get the front element from the queue i.e. arr[front] if the queue is not empty.
 Display: Print all elements of the queue. If the queue is non-empty, traverse and print all
the elements from the index front to rear.
Below is the implementation of a queue using an array:
In the below code , we are initializing front and rear as 0, but in general we have to initialize it
with -1.
If we assign rear as 0, rear will always point to next block of the end element, in fact , rear
should point the index of last element,
eg. When we insert element in queue , it will add in the end i.e. after the current rear and then
point the rear to the new element ,
According to the following code:
IN the first dry run, front=rear = 0;
in void queueEnqueue(int data)
else part will be executed,
so arr[rear] =data;// rear =0, rear pointing to the latest element
rear++; //now rear = 1, rear pointing to the next block after end element not the end element
//that’s against the original definition of rear

Source Code
#include<iostream>
using namespace std;
#define MAX_SIZE 5
class Queue {
private:
int front, rear;
int arr[MAX_SIZE];
public:
Queue() : front(-1), rear(-1) {}
bool isEmpty() {
return front == -1 && rear == -1;
}
bool isFull() {
return (rear + 1) % MAX_SIZE == front;
}
void enqueue(int value) {
if (isFull()) {
cout << "Queue is full. Cannot enqueue " << value << endl;
return;
}
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX_SIZE;
}
arr[rear] = value;
cout << value << " enqueued to the queue." << endl;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue is empty. Cannot dequeue." << endl;
return;
}
cout << arr[front] << " dequeued from the queue." << endl;
if (front == rear) {
// Last element is dequeued, reset front and rear
front = rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
}
void display() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
return;
}
cout << "Queue elements: ";
int i = front;
do {
cout << arr[i] << " ";
i = (i + 1) % MAX_SIZE;
} while (i != (rear + 1) % MAX_SIZE);
cout << endl;
}
};
int main() {
Queue queue;
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.display();
queue.dequeue();
queue.display();
queue.enqueue(40);
queue.enqueue(50);
queue.enqueue(60); // Attempt to enqueue when the queue is full
queue.display();
return 0;
}
Output:
10 enqueued to the queue.
20 enqueued to the queue.
30 enqueued to the queue.
Queue elements: 10 20 30
10 dequeued from the queue.
Queue elements: 20 30
40 enqueued to the queue.
50 enqueued to the queue.
60 enqueued to the queue.
Queue elements: 20 30 40 50 60

You might also like