patterncMinor
Array-based queue implementation using C
Viewed 0 times
arrayusingbasedimplementationqueue
Problem
I wrote this header for generic use of a queue. The one thing I'm wondering is if I understood the usage of
void*. I hope that if somebody teach me some conventions when coding in C./*
* array-based queue implementation by using void*
* written by kidkkr
* May 6 '16
*/
#ifndef QUEUE_H
#define QUEUE_H
#define QUEUE_CAPACITY 10
#include
typedef struct {
void* data[QUEUE_CAPACITY];
int head;
int tail;
int size;
} Queue;
void initQueue(Queue* pQueue)
{
pQueue->head = 0;
pQueue->tail = -1;
pQueue->size = 0;
}
void enqueue(Queue* pQueue, void* item)
{
if (pQueue->size == QUEUE_CAPACITY) // when queue is full
{
printf("Queue is full\n");
return;
}
else
{
(pQueue->tail)++;
(pQueue->tail) %= QUEUE_CAPACITY;
(pQueue->data)[pQueue->tail] = item;
(pQueue->size)++;
}
}
void* dequeue(Queue* pQueue)
{
// Return NULL when queue is empty
// Return (void*)item at the head otherwise.
void* item = NULL;
if (isEmpty(&pQueue))
{
printf("Queue is empty\n");
}
else
{
item = (pQueue->data)[pQueue->head];
(pQueue->head)++;
(pQueue->head) %= QUEUE_CAPACITY;
(pQueue->size)--;
}
return item;
}
int isEmpty(Queue* pQueue)
{
return pQueue->size == 0;
}
#endifSolution
if (isEmpty(&pQueue)) is wrong. It should be pQueue. You also need to have the prototype in scope before you use it. Put int isEmpty(Queue* pQueue) at the top of your file.Context
StackExchange Code Review Q#128188, answer score: 2
Revisions (0)
No revisions yet.