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