// stacklisk.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdio.h" #include "malloc.h" //定义链栈节点类型 typedef struct node { int value; struct node *next; }NODE; //定义链栈节点类型变量,栈顶,临时,栈底指针变量 NODE *top,*s,*bottom ; //定义空栈 void empty() { top=NULL; } //压栈 NODE *push(int element_value) { s=(NODE*)malloc(sizeof(NODE)); s->value=element_value; s->next=top; //返回栈底指针 if(top==NULL) { bottom=s; } top=s; return top; } //出栈 void pop() { int y; if(top==NULL) { printf("stack is empty\n"); return; } s=top; y=top->value; top=top->next; free(s); printf("pop element value is %d\n",y); } void main() { int b,n,i; empty(); printf("How many times do you want to push and to pop?\n"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter the %d time's push value:",i); scanf("%d",&b); push(b); } printf("\nThe value of bottom=%d\n",bottom->value); for(i=1;i<=n;i++) { pop(); } //测试空栈 pop() ; }