0

我对用 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
4

3 回答 3

0
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 所以空指针永远不会出现以结束打印...。

于 2013-10-24T04:51:54.380 回答
0

问题是您只分配temp一次,然后每次将卡添加到desk.

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

第二次,desk 已被分配,因此您在 malloc'd 的节点中设置值并分配给 variable temp,并更新desk以使其指向该节点。

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

换句话说,temp最终desk指向内存中的同一个节点,也就是指向自身。试图打印它只是意味着一遍又一遍地访问同一张卡片,这也意味着你永远不会达到空值,所以你永远不会停止。

于 2013-10-24T04:56:38.757 回答
0

此代码将正常工作我刚刚删除了办公桌前的 * 指针。所以它会工作。

#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;
}
}
于 2013-10-24T06:17:00.920 回答