In fact, this is not a main problem, I encountered this issue while doing practice about BST traversal with circular-queue.
I tried to define inorder traversal recursively and so enqueue-ing data also defined recursively.
Here is some example which I tried as below :
typedef int element;
typedef struct {
element data[MAX_QUEUE_SIZE];
int front, rear;
} QueueType;
void init_queue(QueueType* q)
{
q->front = q->rear = 0;
}
int is_full(QueueType* q)
{
return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
}
void enqueue(QueueType* q, element item)
{
if (is_full(q))
error("Queue is full");
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
q->data[q->rear] = item;
}
void q_factorial_enqueue(QueueType* q, int data)
{
enqueue(q, data);
if (data != 0)
{
q_factorial_enqueue(q, data - 1);
}
}
void queue_print(QueueType* q)
{
printf("QUEUE(front=%d rear=%d) = ", q->front, q->rear);
if (!is_empty(q)) {
int i = q->front;
do {
i = (i + 1) % (MAX_QUEUE_SIZE);
printf("%d | ", q->data[i]);
} while (i == q->rear);
}
printf("\n");
}
And here is main function as below :
int main(void)
{
QueueType queue;
int element;
init_queue(&queue); //Initialization
q_factorial_enqueue(&queue, 5);
queue_print(&queue);
}
I want to enqueue datas '5 4 3 2 1' to circular-queue but If I print elements of circular-queue, there is no correct input also there is no output because there is no input.
What did I miss to define enqueue recursively?