#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>typedef struct Node{int data;struct Node * pNext;
}NODE,*PNODE;typedef struct Stack{PNODE pTop; //栈顶元素PNODE pBottom; //栈底部元素
}STACK,*PSTACK;void init(PSTACK);
void push(PSTACK, int);
void traverse(PSTACK);
int main(void){STACK s; //等价于struct Stackinit(&s); //初始化push(&s,1); //压栈 push(&s, 2);push(&s, 3);push(&s, 4);push(&s, 5);push(&s, 6);push(&s, 7);traverse(&s); //遍历输出while (true){ }return 0;
}//初始化造出一个空栈
void init(PSTACK ps) {ps->pTop = (PNODE)malloc(sizeof(NODE));if (NULL == ps->pTop) {printf("动态内存失败");exit(-1);}else {ps->pBottom= ps->pTop;ps->pTop->pNext = NULL;}
}//压栈
void push(PSTACK ps, int val) {//构造一个新的节点PNODE pNew=(PNODE)malloc(sizeof(NODE));pNew->data = val;pNew->pNext = ps->pTop;ps->pTop = pNew;
}//遍历
void traverse(PSTACK ps) {PNODE p = ps->pTop;while (p != ps->pBottom) {printf("%d ",p->data);p = p->pNext;}printf("\n");return;
}