想请教一下,我在此感激不尽!
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define stacksize 100
typedef int Datetype;
//节点
typedef struct Node
{
Datetype date;// [stacksize] ;
struct stack *next;
}Node, *pNode;
//栈顶栈底
typedef struct stack
{
pNode base;
pNode top;
}stack,*pstack;
//初始化
void init(pstack s)
{
s->base = (Node*)malloc(sizeof(Node));
if (s->base == NULL)
{
printf("失败.\n");
exit(-1);
};
s->base->next = NULL;
s->top = s->base;
}
//进栈
void insert(pstack s, Datetype x)
{
pNode p = (Node*)malloc(sizeof(Node));
if (p == NULL)
{
printf("失败.\n");
exit(-1);
};
p->date = x;
p->next = s->top;
s->top = p;
}
//输出栈的数据
void Printf(pstack s)
{
pNode p = s->top;
printf("栈数据为:");
while (p != s->base)
{
printf(" %d", p->date);
p = p->next;
}
printf("\n");
}
//判空操作
int empty(pstack s)
{
if (s->top == s->base)
{
return 1;
}
else
{
return 0;
}
}
//销毁栈
int destroy(pstack s)
{
//int n = empty(s);
if (empty(s))
{
return 1;
};
pNode p = NULL;
while (s->top != s->base)
{
p = s->top;
s->top = s->top->next;
free(p);
p = NULL;
}
}
//主函数
void main(void)
{
pstack s = NULL;
int x = 0;
printf("请输入数据:");
scanf_s("%d", &x);
init(s);
insert(s, x);
insert(s, 2);
Printf(s);
destroy(s);
}