xxxxxxxxxx
// Circular Queue implementation in C
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
// Check if the queue is full
int isFull() {
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
return 0;
}
// Check if the queue is empty
int isEmpty() {
if (front == -1) return 1;
return 0;
}
// Adding an element
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
// Removing an element
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
}
// Q has only one element, so we reset the
// queue after dequeing it. ?
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
// Display the queue
void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}
int main() {
// Fails because front = -1
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
// Fails to enqueue because front == 0 && rear == SIZE - 1
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
// Fails to enqueue because front == rear + 1
enQueue(8);
return 0;
}
xxxxxxxxxx
// Circular Queue implementation in C
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
// Check if the queue is full
int isFull() {
if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
return 0;
}
// Check if the queue is empty
int isEmpty() {
if (front == -1) return 1;
return 0;
}
// Adding an element
void enQueue(int element) {
if (isFull())
printf("\n Queue is full!! \n");
else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
// Removing an element
int deQueue() {
int element;
if (isEmpty()) {
printf("\n Queue is empty !! \n");
return (-1);
} else {
element = items[front];
if (front == rear) {
front = -1;
rear = -1;
}
// Q has only one element, so we reset the
// queue after dequeing it. ?
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return (element);
}
}
// Display the queue
void display() {
int i;
if (isEmpty())
printf(" \n Empty Queue\n");
else {
printf("\n Front -> %d ", front);
printf("\n Items -> ");
for (i = front; i != rear; i = (i + 1) % SIZE) {
printf("%d ", items[i]);
}
printf("%d ", items[i]);
printf("\n Rear -> %d \n", rear);
}
}
int main() {
// Fails because front = -1
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
// Fails to enqueue because front == 0 && rear == SIZE - 1
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
// Fails to enqueue because front == rear + 1
enQueue(8);
return 0;
}
xxxxxxxxxx
#include <stdlib.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
/* a link in the queue, holds the info and point to the next Node*/
typedef struct {
int info;
} DATA;
typedef struct Node_t {
DATA data;
struct Node_t *prev;
} NODE;
/* the HEAD of the Queue, hold the amount of node's that are in the queue*/
typedef struct Queue {
NODE *head;
NODE *tail;
int size;
int limit;
} Queue;
Queue *ConstructQueue(int limit);
void DestructQueue(Queue *queue);
int Enqueue(Queue *pQueue, NODE *item);
NODE *Dequeue(Queue *pQueue);
int isEmpty(Queue* pQueue);
Queue *ConstructQueue(int limit) {
Queue *queue = (Queue*) malloc(sizeof (Queue));
if (queue == NULL) {
return NULL;
}
if (limit <= 0) {
limit = 65535;
}
queue->limit = limit;
queue->size = 0;
queue->head = NULL;
queue->tail = NULL;
return queue;
}
void DestructQueue(Queue *queue) {
NODE *pN;
while (!isEmpty(queue)) {
pN = Dequeue(queue);
free(pN);
}
free(queue);
}
int Enqueue(Queue *pQueue, NODE *item) {
/* Bad parameter */
if ((pQueue == NULL) || (item == NULL)) {
return FALSE;
}
// if(pQueue->limit != 0)
if (pQueue->size >= pQueue->limit) {
return FALSE;
}
/*the queue is empty*/
item->prev = NULL;
if (pQueue->size == 0) {
pQueue->head = item;
pQueue->tail = item;
} else {
/*adding item to the end of the queue*/
pQueue->tail->prev = item;
pQueue->tail = item;
}
pQueue->size++;
return TRUE;
}
NODE * Dequeue(Queue *pQueue) {
/*the queue is empty or bad param*/
NODE *item;
if (isEmpty(pQueue))
return NULL;
item = pQueue->head;
pQueue->head = (pQueue->head)->prev;
pQueue->size--;
return item;
}
int isEmpty(Queue* pQueue) {
if (pQueue == NULL) {
return FALSE;
}
if (pQueue->size == 0) {
return TRUE;
} else {
return FALSE;
}
}
int main() {
int i;
Queue *pQ = ConstructQueue(7);
NODE *pN;
for (i = 0; i < 9; i++) {
pN = (NODE*) malloc(sizeof (NODE));
pN->data.info = 100 + i;
Enqueue(pQ, pN);
}
while (!isEmpty(pQ)) {
pN = Dequeue(pQ);
printf("\nDequeued: %d", pN->data);
free(pN);
}
DestructQueue(pQ);
return (EXIT_SUCCESS);
}