为什么这个基本的 C 程序会导致无限循环?
Why Is this basic C program causing an infinite loop?
我的任务是构建一个 "represents a stack" 的基本链表。所以只能按照后进先出原则访问数据。我必须在 "stack" 上应用某些功能。
我的代码编译得很好,但在执行时,它只会无限地打印 1 。我不知道这是怎么回事,因为我实际上只使用了一个 while 循环。
有谁知道我的问题是什么?也许我将来如何防止此类错误。感谢您的任何帮助。提前道歉,我是初学者!
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack {
struct Stack *next;
int data;
} Stack;
Stack* push(Stack*head, int d) { //add a datapoint to the top of the stack
Stack *temp;
temp=malloc(sizeof(Stack));
temp->data=d;
temp->next=head;
return temp;
}
Stack* pop(Stack*head) { //read the top data point, and delete it
Stack* newHead=head->next;
printf("%i", head->data );
free(head);
return newHead;
}
int peek(Stack*head) { // return the top datapoint, without delete
return head->data;
}
void isempty(Stack*head) {
if (head==NULL) {
printf("Stack empty");
}
printf("Stack has data");
}
void print(Stack*head) { //print complete Stack
Stack* cursor;
cursor=head;
while (cursor!=NULL) {
printf("%i", cursor->data);
}
}
int main(int argc, const char * argv[]) {
Stack* head;
head=malloc(sizeof(Stack));
head=NULL;
head=push(head, 4);
head=push(head, 2);
head=push(head, 1);
printf("%i", peek(head));
print(head);
head=pop(head);
print(head);
isempty(head);
head=pop(head);
head=pop(head);
isempty(head);
return 0;
}
您没有在函数 print
中递增 cursor
:
while (cursor!=NULL) {
printf("%i", cursor->data);
}
代码也在这里泄漏内存:
head=malloc(sizeof(Stack));
head=NULL;
函数 print
中有一个无限循环。需要在循环内更新cursor
,否则会一直循环下去。以下是我的做法:
void print(Stack*head) { //print complete Stack
Stack* cursor;
for (cursor = head; cursor != NULL; cursor = cursor->next) {
printf("%i", cursor->data);
}
}
在此代码中:
while (cursor!=NULL) {
printf("%i", cursor->data);
}
您没有更改光标。所以改成
for (cursor = head; cursor!=NULL; cursor = cursor->next) {
printf("%i", cursor->data);
}
您必须在函数 print
:
的循环中向前迈出一步
void print( Stack*head )
{
Stack* cursor = head;
while (cursor != NULL) //
{
printf("%i", cursor->data);
cursor = cursor->next; // step one forward
}
}
此外还有内存泄漏。您为变量 head
分配了内存,但在
之后立即将其设置为 NULL
Stack* head;
// head=malloc(sizeof(Stack)); // delete this
head=NULL;
while循环有问题:
while (cursor!=NULL) {
printf("%i", cursor->data);
cursor = cursor->next; // you forgot this line
}
没有这一行,光标永远不会改变。
我的任务是构建一个 "represents a stack" 的基本链表。所以只能按照后进先出原则访问数据。我必须在 "stack" 上应用某些功能。 我的代码编译得很好,但在执行时,它只会无限地打印 1 。我不知道这是怎么回事,因为我实际上只使用了一个 while 循环。 有谁知道我的问题是什么?也许我将来如何防止此类错误。感谢您的任何帮助。提前道歉,我是初学者!
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack {
struct Stack *next;
int data;
} Stack;
Stack* push(Stack*head, int d) { //add a datapoint to the top of the stack
Stack *temp;
temp=malloc(sizeof(Stack));
temp->data=d;
temp->next=head;
return temp;
}
Stack* pop(Stack*head) { //read the top data point, and delete it
Stack* newHead=head->next;
printf("%i", head->data );
free(head);
return newHead;
}
int peek(Stack*head) { // return the top datapoint, without delete
return head->data;
}
void isempty(Stack*head) {
if (head==NULL) {
printf("Stack empty");
}
printf("Stack has data");
}
void print(Stack*head) { //print complete Stack
Stack* cursor;
cursor=head;
while (cursor!=NULL) {
printf("%i", cursor->data);
}
}
int main(int argc, const char * argv[]) {
Stack* head;
head=malloc(sizeof(Stack));
head=NULL;
head=push(head, 4);
head=push(head, 2);
head=push(head, 1);
printf("%i", peek(head));
print(head);
head=pop(head);
print(head);
isempty(head);
head=pop(head);
head=pop(head);
isempty(head);
return 0;
}
您没有在函数 print
中递增 cursor
:
while (cursor!=NULL) {
printf("%i", cursor->data);
}
代码也在这里泄漏内存:
head=malloc(sizeof(Stack));
head=NULL;
函数 print
中有一个无限循环。需要在循环内更新cursor
,否则会一直循环下去。以下是我的做法:
void print(Stack*head) { //print complete Stack
Stack* cursor;
for (cursor = head; cursor != NULL; cursor = cursor->next) {
printf("%i", cursor->data);
}
}
在此代码中:
while (cursor!=NULL) {
printf("%i", cursor->data);
}
您没有更改光标。所以改成
for (cursor = head; cursor!=NULL; cursor = cursor->next) {
printf("%i", cursor->data);
}
您必须在函数 print
:
void print( Stack*head )
{
Stack* cursor = head;
while (cursor != NULL) //
{
printf("%i", cursor->data);
cursor = cursor->next; // step one forward
}
}
此外还有内存泄漏。您为变量 head
分配了内存,但在
NULL
Stack* head;
// head=malloc(sizeof(Stack)); // delete this
head=NULL;
while循环有问题:
while (cursor!=NULL) {
printf("%i", cursor->data);
cursor = cursor->next; // you forgot this line
}
没有这一行,光标永远不会改变。