今天我试图在 C 中使用链表实现一个堆栈,一切都很好,直到我创建了一个使用 free 的 pop 函数,一旦我调用 free 函数,我的程序就会崩溃,然后 Visual Studio 在新窗口中抛出堆损坏错误消息。除了这个之外,所有其他功能都工作正常,我只是不知道发生了什么。感谢您的所有回答。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node *next;
};
void push(struct Node **top, char data) {
struct Node *temp = (struct Node *)malloc(sizeof(struct Node *));
if (temp == NULL) {
printf("Stack overflow.\n");
} else {
temp->data = data;
temp->next = *top;
(*top) = temp;
}
}
void pop(struct Node **top) {
struct Node *aux;
if (*top != NULL) {
printf("Popped element: %c\n", (*top)->data); …Run Code Online (Sandbox Code Playgroud)