无法遍历C链表

时间:2018-06-13 14:32:57

标签: c loops linked-list

任务是创建一个由对象组成的链表。用户输入Node中每个main的数据,然后将对象传递给push,这会创建列表。

问题出现在printList函数中,从而无法满足break的条件。 由于某种原因,行head = head->next没有做任何事情,因为每次迭代的next地址都保持不变。

typedef struct Node {
    int a;
    char asd[30];
    struct Node *next;
}Node;

Node *head = NULL;

void push(Node**head, struct Node* object);
void printList(Node *head);

int main() {

    struct Node {
        int oA;
        char oAsd[30];
        struct Node *next;
    };

    struct Node *object = malloc(sizeof(struct Node));

    int c = 0;
    while (1) {
        printf("This int will be stored in Node %d.\n", ++c);
        scanf("%d", &object->oA);
        getchar();
        if (!object->oA) {
            break;
        }
        printf("This string will be stored in Node %d.\n", c);
        gets_s(object->oAsd, 30);
        if (!(strcmp(object->oAsd, "\0"))) {
            break;
        }
        push(&head, object);
    }
    printList(head);

    return 0;
}

void push(Node ** head,  struct Node* object)
{
    Node *tmp = malloc(sizeof(Node));
    tmp = object;
    tmp->next = (*head);
    (*head) = tmp;
}


void printList(Node *head) {
    if (head == NULL) {
        puts("No list exists.");
        exit(9);
    }

        while (1) {
            printf("-------------------------------\n");
            printf("|Int: <%d> |||| String: <%s>.|\n", head->a, head->asd);
            printf("-------------------------------\n");
            if (head->next) {
                printf("\n\n%p\n\n", head->next);

                head = head->next;
            }
            else {
                break;
            }
        }
}`

1 个答案:

答案 0 :(得分:3)

您的代码中存在两个主要问题:

  • 您在struct Node之内和main之内定义main

  • 这里tmp = object;你将指针的值复制到另一个指针,但你真的想将结构的值复制到另一个结构,即*tmp = *object;

除此之外 - 不要将head作为全局变量。

所以代码应该更像:

typedef struct Node {
    int a;
    char asd[30];
    struct Node *next;
}Node;

void push(Node**head, struct Node* object);
void printList(Node *head);

int main() {
    Node *head = NULL;

    struct Node *object = malloc(sizeof(struct Node));

    int c = 0;
    while (1) {
        printf("This int will be stored in Node %d.\n", ++c);
        scanf("%d", &object->a);
        getchar();
        if (!object->a) {
            break;
        }
        printf("This string will be stored in Node %d.\n", c);
        gets_s(object->asd, 30);
        if (!(strcmp(object->asd, "\0"))) {
            break;
        }
        push(&head, object);
    }
    printList(head);

    return 0;
}

void push(Node ** head,  struct Node* object)
{
    Node *tmp = malloc(sizeof(Node));
    *tmp = *object;                    // Copy the struct
    tmp->next = (*head);
    (*head) = tmp;
}


void printList(Node *head) {
    if (head == NULL) {
        puts("No list exists.");
        exit(9);
    }

        while (1) {
            printf("-------------------------------\n");
            printf("|Int: <%d> |||| String: <%s>.|\n", head->a, head->asd);
            printf("-------------------------------\n");
            if (head->next) {
                printf("\n\n%p\n\n", head->next);

                head = head->next;
            }
            else {
                break;
            }
        }
}