0% found this document useful (0 votes)
20 views3 pages

C++ Queue Implementation Example

The document contains a C++ implementation of a circular queue data structure. It includes functions for creating a queue, checking if it is full or empty, enqueuing and dequeuing items, and retrieving the front and rear elements. The main function demonstrates the usage of these queue operations.

Uploaded by

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

C++ Queue Implementation Example

The document contains a C++ implementation of a circular queue data structure. It includes functions for creating a queue, checking if it is full or empty, enqueuing and dequeuing items, and retrieving the front and rear elements. The main function demonstrates the usage of these queue operations.

Uploaded by

hello12001000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <bits/stdc++.

h>

using namespace std;

class Queue {

public:

int front, rear, size;

unsigned capacity;

int* array;

};

Queue* createQueue(unsigned capacity)

Queue* queue = new Queue();

queue->capacity = capacity;

queue->front = queue->size = 0;

queue->rear = capacity - 1;

queue->array = new int[queue->capacity];

return queue;

int isFull(Queue* queue)

return (queue->size == queue->capacity);

int isEmpty(Queue* queue)

return (queue->size == 0);

void enqueue(Queue* queue, int item)

if (isFull(queue))

return;
queue->rear = (queue->rear + 1)

% queue->capacity;

queue->array[queue->rear] = item;

queue->size = queue->size + 1;

cout << item << " enqueued to queue\n";

int dequeue(Queue* queue)

if (isEmpty(queue))

return INT_MIN;

int item = queue->array[queue->front];

queue->front = (queue->front + 1)

% queue->capacity;

queue->size = queue->size - 1;

return item;

int front(Queue* queue)

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->front];

int rear(Queue* queue)

if (isEmpty(queue))

return INT_MIN;

return queue->array[queue->rear];

int main()
{

Queue* queue = createQueue(1000);

enqueue(queue, 10);

enqueue(queue, 20);

enqueue(queue, 30);

enqueue(queue, 40);

cout << dequeue(queue)

<< " dequeued from queue\n";

cout << "Front item is "

<< front(queue) << endl;

cout << "Rear item is "

<< rear(queue) << endl;

return 0;

You might also like