简体   繁体   English

如何在C中打印队列[数据结构]?

[英]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? 我具有这些结构,我想编写一个主要函数来进行一些测试,但是:1-如何打印此队列? 2 - How can I enqueue more dinamically? 2-如何更深入地入队? Like, with a "for"? 喜欢,带有“ 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). 您应该在Enqueue函数中增加队列的大小(不是)。 To fix that add the following somewhere in the else condition: 要解决此问题,请在else条件的某处添加以下else

queue->size += 1;

... also you should be decreasing the size in your Dequeue function, done as follows: ...此外,您还应该减小Dequeue函数的大小,方法如下:

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. ...此外,根据实现队列的方式,当创建队列时,该队列应该为空,因此正反是NULL指针(如果使用链接列表),因此对于数组,应为-1,以表示一个空队列。 Your createQueue function should be as follows: 您的createQueue函数应如下所示:

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. ...进行更改将迫使您更改EnqueueDequeue函数以分别正确处理空队列。 Also, you should create a destroyQueue function to clean up all of the memory you allocated (in this case only once). 另外,您应该创建一个destroyQueue函数来清理您分配的所有内存(在这种情况下只有一次)。 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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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