#环境 Windows7 sp1 x64,Visual Studio 2010 旗舰版
#语言 C
#功能 压栈测试
// stack.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "malloc.h"
#include "stdlib.h" //exit()要用到的头文件
#define ElemType int
//定义数据类型
typedef struct node {
ElemType data;
struct node *next;
}NODE;
NODE *top;
void InitStack()
{
top=NULL;
}
int StackEmpty()//判断栈空
{
if(NULL==top) return 1;
return 0;
}
void Push(ElemType x)
{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
p->data=x;
p->next=top;
top=p;
}
ElemType Pop()
{ NODE* p;
ElemType x;
if(p==NULL){printf("栈下溢错误!\n");
exit(1);
}
p=top;
x=p->data;
top=p->next;
free(p);
return x;
}
ElemType GetTop()
{
if(NULL==(top)){printf("栈下溢错误!\n");
exit(1);
}
return (top)->data;
}
int _tmain(int argc, _TCHAR* argv[])
{
InitStack();
Push(1);
Push(12);
Push(123);
NODE *p=top;
while(NULL!=p){printf("%d\t\7",p->data);p=p->next;};
printf("%d\n");
printf("%d\n",top->data);
return 0;
}
运行结果: