xxxxxxxxxx
#include<stdio.h>
#include<stdlib.h>
struct queue
{
int size;
int f;
int r;
int* arr;
};
int isEmpty(struct queue *q){
if(q->r==q->f){
return 1;
}
return 0;
}
int isFull(struct queue *q){
if(q->r==q->size-1){
return 1;
}
return 0;
}
void enqueue(struct queue *q, int val){
if(isFull(q)){
printf("This Queue is full\n");
}
else{
q->r++;
q->arr[q->r] = val;
printf("Enqued element: %d\n", val);
}
}
int dequeue(struct queue *q){
int a = -1;
if(isEmpty(q)){
printf("This Queue is empty\n");
}
else{
q->f++;
a = q->arr[q->f];
}
return a;
}
int main(){
struct queue q;
q.size = 4;
q.f = q.r = 0;
q.arr = (int*) malloc(q.size*sizeof(int));
// Enqueue few elements
enqueue(&q, 12);
enqueue(&q, 15);
enqueue(&q, 1);
printf("Dequeuing element %d\n", dequeue(&q));
printf("Dequeuing element %d\n", dequeue(&q));
printf("Dequeuing element %d\n", dequeue(&q));
enqueue(&q, 45);
enqueue(&q, 45);
enqueue(&q, 45);
if(isEmpty(&q)){
printf("Queue is empty\n");
}
if(isFull(&q)){
printf("Queue is full\n");
}
return 0;
}
xxxxxxxxxx
#include <iostream>
#include <queue>
using namespace std;
int main() {
// create a queue of string
queue<string> animals;
// push elements into the queue
animals.push("Cat");
animals.push("Dog");
cout << "Queue: ";
// print elements of queue
// loop until queue is empty
while(!animals.empty()) {
// print the element
cout << animals.front() << ", ";
// pop element from the queue
animals.pop();
}
cout << endl;
return 0;
}
xxxxxxxxxx
A queue is a linear list of elements in which deletion can take place only at
one end, called the front, and insertion can take place only at the other end,
called the rare.
// Example: FIFO (First In First Out).
// In other words, the order in which elements enter a queue, is the order in
// which they leave.
xxxxxxxxxx
#include <iostream>
#include <iomanip>
using namespace std;
/*
Complete Implementation of Queue using Doubly Linked List
*/
struct Node {
int data;
Node *next;
Node *prev;
};
class Queue {
private:
Node *front;
Node *rear;
int Size;
public:
Queue() {
this->Size = 0;
this->front = nullptr;
this->rear = nullptr;
}
~Queue() {
if (this->isEmpty()) {
cout << endl << "Nothing to delete!" << endl;
} else {
cout << "\n[Desctructor call]" << endl;
while (this->rear != nullptr) {
Node *current = this->rear;
this->rear = this->rear->next;
cout << " delete: [" << current->data << " | " << current->next << "]" << endl;
delete current;
}
}
}
int size() const {
return this->Size;
}
bool isEmpty() const {
return (this->front == nullptr);
}
int peek() const {
return (isEmpty()) ? 0 : this->front->data;
}
Queue &enqueue(const int &value) {
Node *node = new Node();
node->data = value;
node->next = nullptr;
node->prev = nullptr;
if (this->rear == nullptr) {
this->rear = this->front = node;
} else {
node->next = this->rear;
this->rear->prev = node;
this->rear = node;
}
this->Size++;
return *this;
}
Queue &dequeue() {
if (this->isEmpty()) {
cout << "Queue is empty" << endl;
} else {
if (this->rear == this->front) {
delete this->front;
this->front = this->rear = nullptr;
} else {
Node *temp = this->front;
this->front = this->front->prev;
this->front->next = nullptr;
delete temp;
}
this->Size--;
}
return *this;
}
void display() const {
if (this->rear == nullptr) {
cout << "Rear: [" << NULL << " | " << this->rear << "] " << endl;
cout << "Front: [" << NULL << " | " << this->front << "] " << endl;
} else {
Node *current = this->rear;
while (current != nullptr) {
if (current == this->rear) {
cout << "Rear: [" << current->data << " | " << current->next << ((current->next == nullptr) ? "] :Front\n" : "] -> ");
} else if (current->next == nullptr) {
cout << "[" << this->front->data << " | " << this->front->next << "] :Front" << endl;
} else {
cout << "[" << current->data << " | " << current << "] -> ";
}
current = current->next;
}
}
}
};
int main() {
/* Example usage: */
Queue queue = Queue();
cout << endl;
queue.enqueue(1).enqueue(2).enqueue(3);
queue.display();
queue.dequeue();
queue.display();
queue.dequeue().dequeue();
queue.display();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
cout << std::boolalpha;
cout << endl;
cout << "Size: " << queue.size() << endl;
cout << "Front: " << queue.peek() << endl;
queue.dequeue() ;
cout << "Is Empty: " << queue.isEmpty() << endl;
cout << "Front: " << queue.peek() << endl;
queue.dequeue();
cout << "Is Empty: " << queue.isEmpty() << endl;
cout << "Front: " << queue.peek() << endl;
queue.dequeue();
cout << "Is Empty: " << queue.isEmpty() << endl << endl;
queue.enqueue(4);
queue.enqueue(3);
queue.enqueue(2);
queue.enqueue(1);
queue.display();
/*
Output:
Rear: [3 | 0x7fa3a0f05cb0] -> [2 | 0x7fa3a0f05cb0] -> [1 | 0x0] :Front
Rear: [3 | 0x7fa3a0f05cb0] -> [2 | 0x0] :Front
Rear: [0 | 0x0]
Front: [0 | 0x0]
Size: 3
Front: 10
Is Empty: false
Front: 20
Is Empty: false
Front: 30
Is Empty: true
Rear: [1 | 0x7fa3a0f05d00] -> [2 | 0x7fa3a0f05d00] -> [3 | 0x7fa3a0f05c90] -> [4 | 0x0] :Front
[Desctructor call]
delete: [1 | 0x7fa3a0f05d00]
delete: [2 | 0x7fa3a0f05c90]
delete: [3 | 0x7fa3a0f05cb0]
delete: [4 | 0x0]
*/
return 0;
}