I have the structs and I would like to write a main function to do some tests, but: 1 - How can I print this queue? 2 - How can I enqueue more dinamically? Like, with a "for"?
I'm not trying to have a answer for a homework here (before someone say that). I have a test, so I'm only trying to learn how to do it, and if someone can help me I'll be very grateful.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
typedef struct Queue{
int size;
int first;
int last;
int items[MAX_SIZE];
} Queue;
Queue* createQueue() {
Queue *queue = (Queue*)malloc(sizeof(Queue));
queue->size = 0;
queue->first = 0;
queue->last = MAX_SIZE - 1;
return queue;
}
Enqueue(Queue *queue, int item) {
if(queue->size >= MAX_SIZE) {
printf("Queue is full!");
}
else {
queue->last = (queue->last + 1) % MAX_SIZE;
queue->items[queue->last] = item;
queue->size++;
}
}
Dequeue(Queue *queue) {
if(queue->size <= 0) {
printf("Queue is empty!");
}
else {
queue->first = (queue->first + 1) % MAX_SIZE;
queue->size--;
}
}
int main {
int i;
Queue *queue = createQueue();
Enqueue(queue, 1);
Enqueue(queue, 5);
Enqueue(queue, 8);
Enqueue(queue, 9);
for(i = 0; i <= MAX_SIZE; i++) { // Is this "for" right?
printf("%d ", queue-> ????) // Don't know what to put here to print right
}
return 0;
}