C for loop имеет другой эффект, чем развернутый цикл

Я пишу простую очередь в c, используя связанный список, который отслеживает последний элемент.

Это мой код:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>   

typedef struct node_s {
    uint32_t x, y;
} node;


// very cheap linked list queue that lives as long as its scope
typedef struct queue_s {
    node* node;
    struct queue_s* next;
} queue;

bool isEmpty(queue** queue)
{
    return !*queue;
}

node* dequeue(queue** head, queue** tail)
{
    if (isEmpty(head)) {
        printf("attempting to dequeue from an empty queue\n");
        return NULL;
    }
    node* ret = (**head).node;
    *head = (*head)->next;
    if (isEmpty(head)) *tail = NULL;
    printf("next %p\n", (void*) *head);
    // no need to free anything, the queue lives as long as its scope
    return ret;
}

void enqueue(queue** head, queue** tail, queue* new)
{
    if (isEmpty(head)) {
        // has no head -> make new head
        *head = new;
        *tail = new;
        printf("empty\n");
    } else {
        // has head -> make new tail // TODO does this work?
        (*tail)->next = new;
        *tail = (*tail)->next;
        printf("not empty\n");
    }
}

int main()
{
    queue* head = NULL;
    queue* tail = NULL;

    enqueue(&head, &tail, &(queue) {.node=&(node) {1, 1}, NULL});
    printf("h %i\n", head->node->x);
    printf("t %i\n", tail->node->x);

    enqueue(&head, &tail, &(queue) {.node=&(node) {2, 2}, NULL});
    printf("h %i\n", head->node->x);
    printf("t %i\n", tail->node->x);

    enqueue(&head, &tail, &(queue) {.node=&(node) {3, 3}, NULL});
    printf("h %i\n", head->node->x);
    printf("t %i\n", tail->node->x);

    // for (int i = 1; i <= 3; ++i) {
    //  enqueue(&head, &tail, &(queue) {.node=&(node) {i, i}, NULL});
    //  printf("h %i\n", head->node->x);
    //  printf("t %i\n", tail->node->x);
    // }

    return 0;
}

Теперь развернутый цикл for в main() работает правильно, так как элемент head остается 1, а новые элементы находятся в очереди:

empty
h 1
t 1
not empty
h 1
t 2
not empty
h 1
t 3

... но сам цикл for дает неправильный вывод, так как элемент head всегда равен элементу tail:

empty
h 1
t 1
not empty
h 2
t 2
not empty
h 3
t 3

Я новичок, и это сводит меня с ума, может кто-нибудь помочь мне?

1 ответ

Решение

Этот код:

for (int i = 1; i <= 3; ++i) {
      enqueue(&head, &tail, &(queue) {.node=&(node) {i, i}, NULL});
      printf("h %i\n", head->node->x);
      printf("t %i\n", tail->node->x);
}

эквивалентно этому:

for (int i = 1; i <= 3; ++i) 
      queue next = {.node=&(node) {i, i}, NULL};
      enqueue(&head, &tail, &next);
      printf("h %i\n", head->node->x);
      printf("t %i\n", tail->node->x);
} // End of scope

Новый элемент next перестанет существовать в конце области (см. комментарий). То, что вам нужно сделать, чтобы это работало в цикле, - это динамическое распределение памяти.

Другие вопросы по тегам