#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
//【1】定义数据类型和两个指针
typedef struct node
{
int data;
struct node* pnest;
}NODE,*PNODE;
typedef struct stack {
PNODE ptop;
PNODE pbottom;
}STACK,*PSTACK;
void init(PSTACK ps);
void push(PSTACK ps, int val);
void traver(PSTACK ps);
int main(void)
{
//【1】初始化栈
STACK S;
init(&S);
//【2】压入栈
push(&S,12);
push(&S, 5);
push(&S, 8);
//【3】遍历栈
traver(&S);
return 0;
}
void init(PSTACK ps)
{
//【】新建一个节点
ps->ptop = (PNODE)malloc(sizeof(NODE));
//【】尾结点指向头节点
ps->pbottom = ps->ptop;
//【】新节点的指针域为空
ps->ptop->pnest = NULL;
}
void push(PSTACK ps,int val)
{
//【】新建一个指针指向新的节点
PNODE pnew = (PNODE)malloc(sizeof(NODE));
//【】val放到data
pnew->data = val;
//【】指向下一个节点
pnew->pnest = ps->ptop;
//【】首节点指向最前面
ps->ptop = pnew;
}
void traver(PSTACK ps)
{
//【】定义一个p指针
PNODE p ;//= (PNODE)malloc(sizeof(NODE))
//p->pnest = ps->ptop;
p= ps->ptop;//是给p 而不是,p指向的节点的指针域
//【】指针是否为空
while (p->pnest!=NULL)
{ //【】循环输出val
printf("%d",p->data);
//【】p指针指向下一个
p = p->pnest;
}
}