2

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;
}
U23r
  • 1,653
  • 10
  • 28
  • 44
  • 2
    [Don't cast the return value of `malloc()`!](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) –  Aug 11 '13 at 22:38
  • `Enqueue(Queue *queue, int item) {` ... and don't rely on functions to return int (or void) – wildplasser Aug 11 '13 at 22:44

2 Answers2

5

Since you are using an array it could be done as follows:

for ( int i = 0; i < queue->size; i++ ) {
  printf( "item at position %d is %d\n", i, queue->items[ i ] );
}

Important note though, you actually have a few errors in your implementation of a queue. You should be increasing the size of the queue in your Enqueue function (which you are not). To fix that add the following somewhere in the else condition:

queue->size += 1;

... also you should be decreasing the size in your Dequeue function, done as follows:

queue->size -= 1;

... also, depending on how you implement your queue, when you create a queue it should be empty and hence front and back are NULL pointers (if using a linked list) and thus should be -1 for an array, to represent an empty queue. Your createQueue function should be as follows:

Queue* createQueue() {
    Queue *queue = (Queue*)malloc(sizeof(Queue));
    queue->size = 0;
    queue->first = -1;
    queue->last = -1;

    return queue;
}

... making that change will force you to change your Enqueue and Dequeue functions to respectively handle an empty queue properly. Also, you should create a destroyQueue function to clean up all of the memory you allocated (in this case only once). In summary, lot's needs to be changed for this queue to work properly. Since this is a homework assignment, I will leave it up to you.

Alternative Implements:

I recommend you explore how to implement a queue using a dynamic array or linked list. Using a fixed size array is practically useless... (but a fair introduction). Explore "dynamic" memory allocation and the memory pool, or more commonly known as heap.

Great job though, I hope you are learning!

Jacob Pollack
  • 3,703
  • 1
  • 17
  • 39
  • @Anne, no problem. If the answer helped you please identify this to others by up voting and selecting it as the answer to your question. This will help avoid duplicate threads. Best of luck. – Jacob Pollack Aug 11 '13 at 22:57
-1
/*Affichage*/
void affiche(struct file* f)
{
    int j;
    int taille=(f->ar-f->av)+1;
    for(j=f->av; j < taille; j++)
        printf("%d \n", f->tab[f->av]);
}