栈的顺序存储结构(数组)

来源:互联网 发布:计算一组数据的标准差 编辑:程序博客网 时间:2024/05/21 09:15
# include<iostream># include<cstdlib>using namespace std;# define OK 0# define RERRO 1# define MAXSIZE 20# define TURE 1# define FALSE 0typedef int Status;typedef int ElemType;typedef struct{ElemType data[MAXSIZE];int top;}sqStack, * psqStack;Status InitStack(psqStack *);Status DestroyStack(sqStack *);Status ClearStack(sqStack *);Status PushStack(psqStack, ElemType);Status PopStack(psqStack, ElemType *);int main(){sqStack L;psqStack S = &L;ElemType e;InitStack(&S);for (ElemType i = 0; i < MAXSIZE; i++)PushStack(S, i);for (ElemType i = 0; i <= S->top; i++)cout << S->data[i] << endl;PopStack(S, &e);for (ElemType i = 0; i <= S->top; i++)cout << S->data[i] << endl;ClearStack(S);for (ElemType i = 0; i <= S->top; i++)cout << S->data[i] << endl;DestroyStack(S);system("pause");return 0;}//创建栈Status InitStack(psqStack * S){*S = (sqStack*)malloc(sizeof(sqStack));(*S)->top = -1;return OK;}//销毁栈Status DestroyStack(sqStack * S){sqStack * q;q = S;free(q);q = NULL;return OK;}//清空栈Status ClearStack(sqStack * S){sqStack * q = S;for (int i = 0; i < MAXSIZE; i++){q->data[i] = NULL;}q->top = -1;return OK;}//判断栈是否为空Status StackEmpty(sqStack S){if (S.top = -1)return TURE;elsereturn FALSE;}//压栈Status PushStack(psqStack S, ElemType e){if (S->top >= 19)return FALSE;S->top++;S->data[S->top] = e;return OK;}//出栈Status PopStack(psqStack S, ElemType *e){if (S->top <= -1)return FALSE;*e = S->data[S->top];S->top--;return OK;}


 

0 0
原创粉丝点击