# 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 pBottm; }STACK,* PSTACK; void initStack(PSTACK pS); void pushStack(PSTACK pS,int val); void traverseStack(PSTACK pS); int main(void) { STACK s; initStack(&s); pushStack(&s,1); pushStack(&s,2); pushStack(&s,3); pushStack(&s,4); pushStack(&s,5); traverseStack(&s); return 0; } void initStack(PSTACK pS) { pS->pTop=(PNODE)malloc(sizeof(NODE)); if(pS->pTop==NULL) { printf("动态内存分配失败! "); exit(-1); } else { pS->pBottm=pS->pTop;//pS->pBottm->pNext=pS->pTop->pNext ? pS->pTop->pNext=NULL; } } void pushStack(PSTACK pS,int val) { PNODE pNew=(PNODE)malloc(sizeof(NODE)); pNew->data=val; pNew->pNext=pS->pTop;//因为刚开始pTop和pBottom指向的都是同一个节点,所以pS->pTop=pS->pBottom pS->pTop=pNew; return; } void traverseStack(PSTACK pS) { PNODE p=pS->pTop; while(p !=pS->pBottm) { printf("%d ",p->data); p=p->pNext; } printf(" "); return; }