zZ_小春 发表于 2013-8-24 20:46
楼主能把改好的代码发上来吗,我改了*base=(LinkStackPtr)malloc(sizeof(StackNode));还是不行 //预定义常量和类型
//函数结果状态代码
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
#include<windows.h>
#include<math.h>
//#include<unistd.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int ElemType;
//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
#define STACK_INIT_SIZE 10 //储空间的初始分配量
#define STACK_INCREMENT 2 //存储空间的分配增量
#define MAXSIZE 10
typedef char SElemType; //多型数据类型
typedef int ElemType;
typedef struct StackNode
{
ElemType data;
struct StackNode *next;
}StackNode,*LinkStackPtr;
typedef struct LinkStack
{
LinkStackPtr top;
int count;
}Stack;
//=================================栈链初始化
void InitStack(LinkStackPtr *base)
{
LinkStackPtr p=NULL;
*base=(LinkStackPtr)malloc(sizeof(ElemType));
(*base)->data=0;
(*base)->next=NULL;
}
//=======================================进栈操作
void Push(Stack *S,ElemType e)
{
LinkStackPtr p,q;
p=(LinkStackPtr)malloc(sizeof(StackNode));
//free(p); //这里要吧释放
q=p;
p->data=e;
p->next=S->top;
// free(q);
S->top=p;
S->count++;
//这里就不可以释放
}
//=======================================出栈操作
void Pop(LinkStackPtr top,LinkStackPtr base,Stack *S,ElemType *e)
{
if(top!=base)
{
LinkStackPtr p;
*e=top->data;
p=S->top;
S->top=top->next;
S->count--;
free(top);
}
else
{
printf("栈里已经没有元素可以打印了\n");
}
}
//========================================打印链栈
void print(LinkStackPtr base,LinkStackPtr top)
{
while(top!=base)
{
printf("%d\n",top->data);
top=top->next;
}
}
main()
{
LinkStackPtr base, p;
Stack S;
int i=1;
ElemType e=0;
InitStack(&base);
S.top=base;
S.count=0;
for(i=1;i<=MAXSIZE;i++)
{
Push(&S,i);
}
printf("栈里有%d个元素\n",S.count);
//free(p);
print(base,S.top);
for(i=1;i<=MAXSIZE;i++)
{
Pop(S.top,base,&S,&e);
printf("Pop出来的元素为%d\n",e);
printf("栈里有%d个元素\n",S.count);
print(base,S.top);
}
return 0;
}
|