#include<stdio.h>
#define false 0
#define ok 1
//定义链表栈的结构
typedef struct sqstack
{
int data;
struct sqstack *next;
}sqstack;
typedef struct sqstack * linkstack;
//建立一个空的栈
void creatstack(linkstack *l)
{
(*l) = (linkstack)malloc(sizeof(sqstack));
(*l)->next = NULL;
}
//push
void pushstack(linkstack *l, int e)
{
linkstack p;
p = (linkstack)malloc(sizeof(sqstack));
p->next= (*l)->next;
(*l)->next = p;
p->data = e;
}
//pop
int popstack(linkstack *l, int *e)
{
if ((*l)->next == NULL)
return false;
linkstack q = NULL;
q = (*l)->next;
(*l)->next = q->next;
*e = q->data;
free(q);
return ok;
}
void seestack(linkstack l)
{
if (l->next == NULL)
printf("栈是空的\n");
linkstack p = NULL;
p = l->next;
while (p)
{
printf("%4d\n", p->data);
p = p->next;
}
}
int main(void)
{
int a=0,b=3;
linkstack s;
creatstack(&s);
seestack(s);
pushstack(&s, 5);
seestack(s);
popstack(&s, &a);
seestack(s);
printf("a=%d", a);
return 0;
}
创建新的栈就是创建一个头指针,头指针指向栈顶top