# 使用数组实现堆栈

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 10
#define OK    1
#define ERROR 0
typedef int Status;
typedef int SElemType;
struct SqStack
{
SElemType data[MAXSIZE];//数组实现堆栈
int top;                //栈元素序号
};

Status push(struct SqStack *S, SElemType value)
{
/*栈满*/
if(S->top == MAXSIZE - 1)
{
return ERROR;
}
S->top++;
S->data[S->top] = value;
return OK;
}

Status pop(struct SqStack *S, SElemType *value)
{
/*栈空*/
if(S->top == -1)
return ERROR;
*value = S->data[S->top];
S->top--;
return OK;
}

int is_full(struct SqStack *S)
{
return S->top == MAXSIZE- 1;
}

int is_empty(struct SqStack *S)
{
return S->top == -1;
}

int main(void)
{
struct SqStack *stack = (struct SqStack*)malloc(sizeof(struct SqStack));
int i = 0;
int value;
stack->top = -1;//这句话很重要，不然会出错
for(i=0; i<=10; i++)
{
if(is_full(stack))
{
printf("stack is full\n");
break;
}
push(stack, i);
printf("top is %d\n", stack->data[stack->top]);
}

for(i=0; i<=10; i++)
{
if(is_empty(stack))
{
printf("stack is empty\n");
break;
}
pop(stack, &value);
printf("%d pop\n", value);
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 10
#define OK    1
#define ERROR 0
typedef int Status;
typedef int SElemType;
struct SqStack
{
SElemType data[MAXSIZE];//数组实现堆栈
int top;                //栈元素序号
};

Status push(struct SqStack *S, SElemType value)
{
/*栈满*/
if(S->top == MAXSIZE - 1)
{
return ERROR;
}
S->top++;
S->data[S->top] = value;
return OK;
}

Status pop(struct SqStack *S, SElemType *value)
{
/*栈空*/
if(S->top == -1)
return ERROR;
*value = S->data[S->top];
S->top--;
return OK;
}

int is_full(struct SqStack *S)
{
return S->top == MAXSIZE- 1;
}

int is_empty(struct SqStack *S)
{
return S->top == -1;
}

int main(void)
{
struct SqStack *stack = (struct SqStack*)malloc(sizeof(struct SqStack));
int i = 0;
int value;
stack->top = -1;//这句话很重要，不然会出错
for(i=0; i<=10; i++)
{
if(is_full(stack))
{
printf("stack is full\n");
break;
}
push(stack, i);
printf("top is %d\n", stack->data[stack->top]);
}

for(i=0; i<=10; i++)
{
if(is_empty(stack))
{
printf("stack is empty\n");
break;
}
pop(stack, &value);
printf("%d pop\n", value);
}
return 0;
}

#### 数组实现堆栈操作

2017-03-20 16:14:46

#### 用数组实现堆栈

2010-08-13 13:38:00

#### JS数组实现队列和堆栈

2016-07-26 20:27:42

#### PHP使用数组实现堆栈和队列

2017-06-20 11:59:16

#### 4-7 在一个数组中实现两个堆栈 (20分)

2016-07-11 19:12:33

#### 数组模拟堆栈

2016-07-04 19:18:00

#### 用一个数组实现两个堆栈，最大地使用数组的空间，只要有空间就能入栈成功

2017-05-13 22:24:23

#### 数据结构：请用一个数组实现两个堆栈，要求最大地利用数组空间，使 数组只要有空间入栈操作就可以成功

2015-04-13 13:36:52

#### C基础：数组实现堆栈倒序打印

2013-08-08 19:17:37

#### java基础（三）方法／数组／堆栈／

2016-06-25 16:44:18