栈( stack )是限定仅在表尾进行插入和删除操作的线性表。
#include <stdio.h>
#include <stdlib.h>
//栈的顺序存储结构,用一维数组实现
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE 0
#define MAXSIZE 10
typedef int Status;
typedef int ElemType;
typedef struct {
ElemType data[MAXSIZE];
int top;//栈顶指针
}Stack;
//1. 初始化
Status InitStack(Stack *S){
int i;
for(i=0;i<MAXSIZE;i++)
S->data[i]=NULL;
S->top=-1;
return OK;
}
//2. 创建一个长度为n的堆栈
Status CreateStack(Stack *S,int n){
if(n>MAXSIZE || n<1){
printf("输入长度有误!\n");
return ERROR;
}
srand(time(0));
int i;
for(i=0;i<n;i++){
S->data[i]=rand()%100+1;
}
S->top=n-1;
return OK;
}
//3. 压栈操作
Status push(Stack *S,ElemType e){
if(MAXSIZE-1==S->top){
printf("栈已满\n");
return ERROR;
}
//栈顶指向的元素有值
++