小编Chr*_*101的帖子

为什么这个基本的C程序会导致无限循环?

我的任务是构建一个"代表堆栈"的基本链表.因此,只能根据后进先出主体访问数据.我必须在"堆栈"上应用某些功能.我的代码编译得很好,但是执行时,它只是无限地打印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");
    } …
Run Code Online (Sandbox Code Playgroud)

c linked-list infinite-loop

1
推荐指数
1
解决办法
86
查看次数

标签 统计

c ×1

infinite-loop ×1

linked-list ×1