2019独角兽企业重金招聘Python工程师标准>>>
/*
* stack.c
*
* Created on: Dec 4, 2012
* Author: fsxchen
* FILO的线性表,线性表(数组实现的简单的栈)
*/
#include
#define MAXSIZE 10
typedef struct STACK
{
int data[MAXSIZE]; /*数据*/
int top; /*栈顶元素*/
}SqStack; /*堆栈结构体*/
void InitStack(SqStack *space) /*初始化*/
{
space->top = -1;
}
void Push(SqStack *s) /*入栈*/
{
int val;
printf("请输入你要入栈的值");
scanf("%d",&val);
if(s->top == MAXSIZE)
printf("Error");
s->top++;
s->data[s->top] = val;
}
int Pop(SqStack *s)
{
int i;
if(s->top == -1)
{
printf("ERROR");
return -1;
}
i = s->data[s->top];
s->top--;
return i;
}
int main()
{
SqStack sta;
InitStack(&sta);
printf("%d", sta.top);
Push(&sta);
printf("%d", sta.top);
return 0;
}
注意,用数组实现堆栈,还是比较简单的,堆栈是一种非常有用的数据结构,系统中递归调用的参数存放、逆波兰式计算四则运算等等,都是非常重要的!