栈的基本操作
来源:互联网 发布:淘宝商城女棉鞋 编辑:程序博客网 时间:2024/06/06 00:43
包括构造空栈,销毁栈,把栈置空,检验栈是否为空,求栈的长度,取出栈顶元素,入栈,出栈
#include<stdio.h>#include<stdlib.h>#define STACKMAX 100#define STACKADD 10#define datatype inttypedef struct{ datatype *base; datatype *top; int stacksize;}SqStack;int InitStack(SqStack* S);//初始化,成功返回1int Destroy(SqStack* S);//销毁栈,成功返回1int ClearStack(SqStack* S);//把栈置空int StackEmpty(SqStack* S);//若栈为空返回1,否则返回0int StackLenth(SqStack* S);//返回栈的元素个数int GetTop(SqStack* S,datatype* e);//若栈不为空,保存栈顶的元素并返回1;否则返回0int Push(SqStack* S,datatype e);//插入栈顶元素int Pop(SqStack* S,datatype* e);//若栈不为空,则删除栈顶元素并保存返回1,否则返回0int InitStack(SqStack *S){ S->base = (datatype*)malloc(STACKMAX*sizeof(datatype)); S->top = S->base; S->stacksize = STACKMAX; return 1;}int Destroy(SqStack* S){ free(S->base); S->base = NULL; S->top = NULL; S->stacksize = 0; return 1;}int ClearStack(SqStack* S){ S->base = S->top; return 1;}int StackLenth(SqStack* S){ return S->top - S->base;}int StackEmpty(SqStack* S){ if(S->base == S->top) return 1; return 0;}int GetTop(SqStack* S,datatype* e){ if(StackEmpty(S)) return 0; *e = *(S->top-1); return 1;}int Push(SqStack* S,datatype e){ if(S->top - S->base >= S->stacksize) { S->base = (datatype*)realloc(S->base,(S->stacksize+STACKADD)*sizeof(datatype)); if(!S->base) return 0; S->top = S->base + S->stacksize; S->stacksize += STACKADD; } *S->top++ = e; return 1;}int Pop(SqStack* S,datatype* e){ if(StackEmpty(S)) return 0; *e = *--S->top; return 1;}
阅读全文
1 0
- 栈的基本操作
- 栈的基本操作
- 栈 的基本操作。
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- ACPI 热拔插
- myeclipse 点击部署按钮不起作用 部署不了
- Android微信相机 高仿
- context-param与init-param的区别与作用
- 移动开发webapp开发常用meta设置(手机浏览器全屏模式 html5)
- 栈的基本操作
- Socket 通信流程
- C# 匿名对象(匿名类型)、var、动态类型 dynamic
- git 拉新分支、分支关联、合并分支、分支删除
- 【PAT】【Advanced Level】1092. To Buy or Not to Buy (20)
- RxJava的一些标志符号学习(一)
- Python学习笔记 9
- butterKnife注解不起作用问题
- MD5加密String