用C写的链栈,出栈的时候释放内存出错?请求帮助
这个程序主要的部分写完了,能初始化,能入栈,能释放所有内存,但是在出栈的时候,释放s->top的时候却不行,请问是为什么呢?还是因为我程序写错了,请指教。
Stack.h
程序代码:
#ifndef STACK_H #define STACK_H #include<stdio.h> #include<stdlib.h> #include<string.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int SElemType; typedef int Status; //初始化结构 typedef struct StackNode{ SElemType data; struct StackNode* next; }StackNode,*LinkStackPtr; typedef struct LinkStack { StackNode* top; int count; }LinkStack; //初始化操作,建立一个空栈 LinkStack* InitStack(); //将栈清空 void ClearStack(LinkStack *s); //销毁 void DestoryStack(LinkStack *s); //若栈为空,返回TRUE,否则返回false Status IsEmpty(LinkStack s); //若栈存在且非空,用e返回S的栈顶元素 void GetTop(LinkStack s, SElemType *e); //插入元素e为新的栈顶元素 Status Push(LinkStack *s, SElemType e); //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR Status Pop(LinkStack *s, SElemType *e); //返回栈S的元素个数 Status StackLength(LinkStack *s); //打印 void PrintfStack(LinkStack *s); #endif
Stack.c
程序代码:
#include"Stack.h" //初始化操作,建立一个空栈 LinkStack* InitStack() { LinkStack *s = (LinkStack*)malloc(sizeof(LinkStack)); s->count = 0; s->top = (StackNode*)malloc(sizeof(StackNode)); s->top->data = NULL; s->top->next = NULL; return s; } //将栈清空 void ClearStack(LinkStack *s) { } //销毁 void DestoryStack(LinkStack *s) { if (s == NULL) { return; } LinkStackPtr p; p = s->top;//将栈顶结点赋值给p while (p == NULL) { LinkStackPtr pNext = p->next; free(p);//释放结点p p = pNext; } s->count = 0; free(s); } //若栈为空,返回TRUE,否则返回false Status IsEmpty(LinkStack s) { if (s.top->next == NULL) { return TRUE; } return FALSE; } //若栈存在且非空,用e返回S的栈顶元素 void GetTop(LinkStack s, SElemType *e) { } //插入元素e为新的栈顶元素 Status Push(LinkStack *s, SElemType e) { LinkStackPtr a = (LinkStackPtr)malloc(sizeof(LinkStackPtr)); a->data = e; a->next = s->top;//把当前的栈顶元素赋给新结点的直接后继 s->top = a;//将新结点a赋值给栈顶指针 s->count++; return OK; } //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR Status Pop(LinkStack *s, SElemType *e) { StackNode* p; if (IsEmpty(*s)) { return ERROR; } *e = s->top->data; p = s->top;//将栈顶结点赋值给p printf("删除前s->top:%d\n", s->top->data); s->top = p->next;//使得栈顶指针下移一位,指向后一结点 free(p);//释放结点p--------------主要问题就是这里。这里注释后运行无问题,但内存没释放 printf("删除后结点释放后s->top:%d\n",s->top->data); s->count--; return OK; } //返回栈S的元素个数 Status StackLength(LinkStack *s) { int j = s->count; return j; } //打印 void PrintfStack(LinkStack *s) { if (s == NULL) { return; } LinkStackPtr pCurrent = s->top; for (int i = 0; i < StackLength(s);i++) { printf("%d ",pCurrent->data); pCurrent = pCurrent->next; } printf("\n"); }
main.c
程序代码:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include"Stack.h" int main() { int m[] = { 13,24,45,46,68,78,98 }; int n = 7; //初始化 LinkStack* b = InitStack(); SElemType e; printf("栈的长度为:%d \n", StackLength(b)); //入栈 for (int i = 0; i < n; i++) { Push(b, m[i]); } //打印 printf("栈的长度为:%d \n", StackLength(b)); PrintfStack(b); //出栈 Pop(b, &e);//-------------- //打印 printf("栈的长度为:%d \n", StackLength(b)); PrintfStack(b); //释放 DestoryStack(b); printf("\n"); system("pause"); return 0; }