简体   繁体   中英

how to print a queue[data structure] in C?

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;
}

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!

/*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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM