我对使用C编码非常陌生(因此我正在从事愚蠢的练习)。该代码应创建一个链表,该链表模拟手持的一副纸牌。

该代码旨在:(1)取出第一张卡片并将其放在桌子上,然后(2)取出新的第一张卡片并将其放在手拿的甲板末端。

非常奇怪的是,我的代码在前几次迭代中效果很好,然后突然开始无休止地在屏幕上打印。不幸的是,我对C的了解不足以理解错误是什么。我尝试了不同的代码变体,但似乎没有任何效果。

非常感谢您的投入。我已经附上了我的代码并打印了出来。我还突出显示了打印到屏幕上的功能。我猜问题出在函数add_to_desk()上。

码:

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

typedef struct Node {
    int data;
    struct Node* next;
} *Node_t;

void print_list(Node_t root) {
    while (root != NULL) {
        printf("%d ", root->data);
        root = root->next;
    }
    printf("\n");
}

Node_t first_to_last(Node_t root, Node_t head){
    Node_t next_root, last_root;
    // get the pointer to the second element
    next_root = root->next;
    // unlink first element
    root->next = NULL;
    // rename first element to last
    last_root = root;
    // reassign root to former second root
    root = next_root;
    // designate new root as head
    head = root;
    while (root != NULL) {
        if (root->next == NULL) {
            root->next = last_root;
            break;
        }
        root = root->next;
    }
    return head;
}

int delete_first(Node_t *head){
    Node_t temp = (*head);
    // get data to be returned
    int val = (*head)->data;
    // reassign head to the second element
    *head = (*head)->next;
    // unlink former first element
    temp->next = NULL;
    return val;
}

void add_to_desk(Node_t *desk, Node_t temp){
    // check if desk is empty
    if ((*desk) == NULL) {
        // insert first element
        (*desk) = temp;
    }
    else {
        temp->next = (*desk);
        (*desk) = temp;
        //*desk = temp;
    }
}

int main(){
    //initialize variables
    int val;
    int i;
    Node_t root, head;

    // set size of list of cards
    int n = 4;

    // allocate memory for temp list
    Node_t temp = malloc(sizeof(struct Node));
    temp->data = 0;
    temp->next = NULL;

    // allocate memory for desk cards
    Node_t desk = malloc(sizeof(struct Node));
    desk->data = 0;
    desk->next = NULL;

    // allocate memory for list of cards
    Node_t list = malloc(sizeof(struct Node) * n);

    // initalize list with n cards
    for (i=0; i < n; i++) {
        list[i].data = i+1;
        if (i == n-1) {
            list[i].next = NULL;
        }
        else {
            list[i].next = &list[i+1];
        }
    }

    printf("\nWe start with these cards in our hand:  ");
    root = &(list[0]);
    print_list(root);

    while (1){
        while (root != NULL){
            val = delete_first(&root);
            printf("We have removed the first card of our hand, val = %d\n",val);
            printf("New list:  ");
            print_list(root);
            printf("Lets add val %d to the stack on the desk\n",val);
            if (desk->data == 0) {
                printf("First time adding to desk stack\n");
                desk->data = val;
                desk->next = NULL;
            }
            else {
                temp->data = val;
                printf("Adding val %d to desk stack\n",val);
                add_to_desk(&desk,temp);
            }
            printf("New desk stack:  ");
            print_list(desk);

            if (root->next == NULL) {
                printf("We are at the last card of our hand, just throw it on the desk\n");
                temp->data = root->data;
                add_to_desk(&desk,temp);
                print_list(desk); // <--- HERE'S WHERE PRINTING GOES OUT OF CONTROL
                break;
            }
            else {
                head = first_to_last(root, head);
                root = head;
                printf("We have just moved the first card of our hand to the end\n");
                printf("New hand order:  ");
                print_list(root);
            }
        }
        return 1;
    }
}


打印:

Running…

We start with these cards in our hand:  1 2 3 4
We have removed the first card of our hand, val = 1
New list:  2 3 4
Lets add val 1 to the stack on the desk
First time adding to desk stack
New desk stack:  1
We have just moved the first card of our hand to the end
New hand order:  3 4 2
We have removed the first card of our hand, val = 3
New list:  4 2
Lets add val 3 to the stack on the desk
Adding val 3 to desk stack
New desk stack:  3 1
We have just moved the first card of our hand to the end
New hand order:  2 4
We have removed the first card of our hand, val = 2
New list:  4
Lets add val 2 to the stack on the desk
Adding val 2 to desk stack
New desk stack:  2 2 2 2 2 2 2 2 ...... 2's forever

最佳答案

问题是您只能分配一次temp,然后在每次向desk添加卡时都重复使用它。

第一次将卡片放在桌子上没问题-只需在您分配的节点中设置值并将其分配给变量desk

第二次,已经分配了办公桌,因此您可以在已分配并分配给变量temp的节点中设置值,并更新desk使其指向该节点。

第三次是麻烦开始的地方。您将temp的值设置为2,但是temp仍然是您上一次使用的同一对象(它仍然是桌面的顶部)。您还更新了其next指针以指向desk -同样,它仍然是您最初分配并分配给temp的同一节点。

换句话说,tempdesk最终指向内存中的同一节点,该节点指向自身。尝试打印此图像仅意味着一次又一次地访问同一张卡,这也意味着您永远不会达到零值,因此您永不会停止。

关于c - C链接列表纸牌游戏,由于致命错误而获得怪异结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19557048/

10-10 02:15