繁体   English   中英

无法遍历C链表

[英]Can't traverse through a C linked list

任务是创建一个由对象组成的链表。 用户输入main Node中每个Node数据,然后将对象传递给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;
            }
        }
}`

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

  • 您可以定义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;
            }
        }
}

暂无
暂无

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

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