Conversation
| #define STARTSIZE 10 | ||
|
|
||
| typedef struct Queue { | ||
| int* array; |
There was a problem hiding this comment.
Используйте четыре пробела для отступов
| int count; | ||
| int size; |
There was a problem hiding this comment.
Не очень удачный выбор имён переменных, непонятно, чем они отличаются
| int first; | ||
| int back; |
There was a problem hiding this comment.
Оно обычно head и tail, либо front и back, но так тоже ок
| Queue* createQueue() { | ||
| Queue* queue = malloc(sizeof(Queue)); | ||
| if (queue == NULL) { | ||
| return; |
There was a problem hiding this comment.
return что? Функция обещала вернуть указатель. Тут наверняка компилятор выдаёт предупреждение, а надо, чтобы программа компилировалась без предупреждений
| } | ||
| queue->array = malloc(10 * sizeof(int)); | ||
| if (queue->array == NULL) { | ||
| return; |
There was a problem hiding this comment.
И потеряем указатель на уже выделенную под queue память
|
|
||
| int dequeue(Queue* queue) { | ||
| if (isEmpty(queue)) { | ||
| return NULL; |
There was a problem hiding this comment.
NULL — это указатель, а функция обещает вернуть int. Так что NULL скастается к нулю, функция вернёт 0, вызывающий подумает, что в очереди 0 лежал. Тоже надо было предусмотреть код ошибки.
| int element = 0; | ||
| element = queue->array[queue->first]; |
There was a problem hiding this comment.
Эм, зачем это в две фазы делается? :)
| queue->array = NULL; | ||
| queue->size = 0; | ||
| queue->count = 0; | ||
| queue->first = 0; | ||
| queue->back = 0; |
There was a problem hiding this comment.
А зачем это всё, и надо же ещё вернуть память, выделенную malloc на 18-й строчке?
|
|
||
| typedef struct Queue Queue; | ||
|
|
||
| //function to add to the tail |
| Queue* queue = createQueue(); | ||
| enqueue(queue, 1); | ||
| int res = dequeue(queue); | ||
| } No newline at end of file |
No description provided.