C 链表卡游戏,由于致命错误而获得奇怪的结果



我对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 = %dn",val);
            printf("New list:  ");
            print_list(root);
            printf("Lets add val %d to the stack on the deskn",val);
            if (desk->data == 0) {
                printf("First time adding to desk stackn");
                desk->data = val;
                desk->next = NULL;
            }
            else {
                temp->data = val;
                printf("Adding val %d to desk stackn",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 deskn");
                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 endn");
                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添加卡时重复使用它。

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

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

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

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

else {
    temp->next = (*desk);
    (*desk) = temp;
    //*desk = temp;
}
try to understand this you will find your problem, as you are new so helping you in the

你可以学习的方式...:)它来自Addtodeskfunc还阅读了有关循环链接列表的信息,以获取更多帮助,因为您的代码提供输出,例如指向B和B指向A,因此永远不会发生空指针以结束打印....

这段代码可以正常工作,我刚刚删除了桌子前面的 * 指针。所以它会崩溃。

#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 = %dn",val);
        printf("New list:  ");
        print_list(root);
        printf("Lets add val %d to the stack on the deskn",val);
        if (desk->data == 0) {
            printf("First time adding to desk stackn");
            desk->data = val;
            desk->next = NULL;
        }
        else {
            temp->data = val;
            printf("Adding val %d to desk stackn",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 deskn");
            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 endn");
            printf("New hand order:  ");            
            print_list(root);
        }
    }
    return 1;
}
}

相关内容

最新更新