#include <stdio.h>
#include <stdlib.h>
#define bool char
#define false 0
#define true 1
//栈 只在表尾进出
typedef struct{
int data;
struct Node* next;
}Node;
Node* initList(){
Node* head=(Node*)malloc(sizeof(Node));
head->data=0;
head->next=NULL;
}
bool isEmpty(Node* p){
if(p->next==NULL){
return 1;//1是空栈
}
else{
return 0;//0不是空栈
}
}
Node* inStack(Node* p,int data){
Node* new=(Node*)malloc(sizeof(Node));
new->data=data;
new->next=p->next;
p->next=new;
return p->next;
}
int top(Node* p){ //出栈
if(p->next==NULL){
printf("栈已空");
}
Node* temp=p->next;
Node* next=temp->next;
p->next=next;
return temp->data;
free(temp);
}
void* Free(Node* p){
free(p);
}
void printList(Node* p){
if(p==NULL){
printf("已清空");
}
else{
Node* temp=p->next;
while(temp){
printf("%d",temp->data);
temp=temp->next;
}
}
}
int main()
{
Node* stack=initList();
inStack(stack,1);
inStack(stack,2);
inStack(stack,3);
inStack(stack,4);
inStack(stack,5);
printf("当前栈:");
printList(stack);
int outdata=top(stack);
printf("\n");
printf("当前出栈:");
printf("%d",outdata);
printf("\n");
printf("当前栈:");
printList(stack);
printf("\n");
printf("清空:");
Free(stack);
printList(stack);
return 0;
}
首先,你在Free
函数中释放了栈的内存,但是在释放后没有将栈的头部指针设置为NULL,这可能导致野指针问题。所以你应该在Free
函数中将栈的头部指针设置为NULL。
修改后的Free
函数如下:
void Free(Node* p){
free(p);
p = NULL; // 添加这一行
}
然后,在main
函数中,你调用了Free
函数来释放栈的内存,但是在释放后你又试图打印栈的内容。这是不正确的,因为栈的内存已经被释放了,再次访问它的内容可能会导致未定义的行为。你应该在调用Free
函数后立即将栈的头部指针设置为NULL,然后再打印栈的内容。
修改后的main
函数如下:
int main()
{
Node* stack=initList();
inStack(stack,1);
inStack(stack,2);
inStack(stack,3);
inStack(stack,4);
inStack(stack,5);
printf("当前栈:");
printList(stack);
int outdata=top(stack);
printf("\n");
printf("当前出栈:");
printf("%d",outdata);
printf("\n");
printf("当前栈:");
printList(stack);
printf("\n");
printf("清空:");
Free(stack);
stack = NULL; // 添加这一行
printList(stack); // 现在这一行不会有问题,因为stack已经被设置为NULL了
return 0;
}
这样修改后,你的程序应该能够正确地输出预期的结果。
【以下回答由 GPT 生成】
问题出在clear函数,此函数在清空栈后没有正确释放内存。同时,在入栈和出栈操作后,应该更新栈顶指针。这里给出修改后的代码:
#include <stdio.h>
#include <stdlib.h>
#define bool char
#define false 0
#define true 1
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* initList() {
Node* head = (Node*)malloc(sizeof(Node));
head->data = 0;
head->next = NULL;
return head;
}
bool isEmpty(Node* p) {
if (p->next == NULL){
return 1; // 1是空栈
}
else {
return 0; // 0不是空栈
}
}
Node* push(Node* p, int data) {
Node* new = (Node*)malloc(sizeof(Node));
new->data = data;
new->next = p->next;
p->next = new;
return p;
}
int pop(Node** p) {
if (isEmpty(*p)){
printf("栈已空");
return -1;
}
int data = (*p)->next->data;
Node* temp = (*p)->next;
(*p)->next = temp->next;
free(temp);
return data;
}
void clear(Node** p) {
Node* temp;
while ((*p)->next != NULL) {
temp = (*p)->next;
(*p)->next = temp->next;
free(temp);
}
}
void printList(Node* p) {
if (isEmpty(p)) {
printf("已清空");
}
else {
Node* temp = p->next;
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
}
}
int main() {
Node* stack = initList();
push(stack, 1);
push(stack, 2);
push(stack, 3);
printf("栈中的元素:");
printList(stack);
printf("\n出栈的元素:%d\n", pop(&stack));
printf("栈中的元素:");
printList(stack);
clear(&stack);
printf("\n清空栈后的元素:");
printList(stack);
return 0;
}
在修改后的代码中,新加了一个clear函数,用于清空栈并释放内存。同时修改了push函数和pop函数,修复了更新栈顶指针的问题。通过调用printList函数,可以正确输出栈中的元素。
【相关推荐】