栈的简单程序.docVIP

  • 5
  • 0
  • 约1.77千字
  • 约 4页
  • 2017-05-28 发布于河南
  • 举报
栈的简单程序

#includestdio.h #includemalloc.h #includestdlib.h #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define ERROR 0 #define OK 1; typedef int SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; }SqStack; // 初始化 int InitStack(SqStack S) { S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S.base) { printf(!\n); exit(ERROR); } S.top=S.base; S.stacksize=STACK_INIT_SIZE; printf(栈初始化完毕!\n); return OK; } // 入栈(插入元素) int Push(SqStack S,SElemType e) { if(S.top-S.base=S.stacksize) { S.base=(SElemType *)malloc(S.stacksize+STACKINCREMENT*sizeof(SElemType)); if(!S.base) { printf(存储分配失败!\n); exit(ERROR); } S.top=S.base+S.stacksize; S.stacksize=S.stacksize+STACKINCREMENT; } *S.top=e; S.top++; // printf(入栈成功!\n); return OK; } // 出栈(删除栈顶元素) int Pop(SqStack S,SElemType e) { if(S.base==S.top) { printf(栈空!\n); return ERROR; } S.top--; e=*S.top; printf(出栈成功!\n); return OK; } // 栈顶元素 int GetTop(SqStack S,SElemType e) { if(S.base==S.top) { printf(栈空!\n); return ERROR; } e=*(S.top-1); return OK; } // 判断空栈 int Empty_SqStack(SqStack S) { if(S.top==S.base) printf(栈空!\n); else printf(栈非空!\n); return OK; } // 置空 int ClearStack(SqStack S) { S.top=S.base; S.stacksize=STACK_INIT_SIZE; printf(置空!\n); return OK; } int main() { SqStack Ss; SElemType e; InitStack(Ss); Empty_SqStack(Ss); printf(Input S_elem:(0结束)\n); scanf(%d,e); while(e!=0) { Push(Ss,e); printf(Input S_elem:(0结束)\n); scanf(%d,e); } Empty_SqStack(Ss); // ClearStack(Ss); // Empty_SqStack(Ss); while(Ss.base!=Ss.top) { GetTop(Ss,e); printf(e=%d\n,e); Pop(Ss,e); } Empty_SqStack(Ss); return 0; }

文档评论(0)

1亿VIP精品文档

相关文档