数据结构之栈的定义
来源:互联网 发布:淘宝卖家扣分规则 编辑:程序博客网 时间:2024/05/18 17:04
#include "stdafx.h"#include<iostream>using namespace std;#include<malloc.h>typedef int ElemType;#define OK 1 //正确#define ERROR 0 //失败#define STACK_INIT_SIZE 5 //栈的初始化容量#define STACK_INCREAMENT 2 //栈满之后,增加的容量typedef struct Stack{ ElemType *base; //栈低指针 ElemType *top; //栈顶指针 int stacksize; //栈的最大容量}SqStack;//初始化void InitStack(SqStack &S){ S.base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if(!S.base) return; S.top=S.base; S.stacksize = STACK_INIT_SIZE;}//获取栈顶元素void GetTop(SqStack &S){ if(S.top==S.base) { cout<<"栈内没有元素"<<endl; return; } cout<<"栈顶元素是:"<<*(S.top-1)<<endl;}//进栈void Push(SqStack &S,ElemType e){ if(S.top-S.base>=S.stacksize) { cout<<"栈满了,继续为其分配空间 ... "<<endl; //realloc 函数 S.base=(ElemType *)realloc(S.base,(S.stacksize+STACK_INCREAMENT) * sizeof(ElemType)); if(!S.base) return; S.top=S.base+S.stacksize; S.stacksize += STACK_INCREAMENT; } *S.top++=e; //S.top++;}//出栈void Pop(SqStack &S){ if(S.top==S.base) { cout<<"栈内没有元素"<<endl; return; } cout<<"出栈的元素是:"<<*--S.top<<endl; //先使指针减1,然后取值,相当于*(S.top-1);S.top--;}int main(int argc,char* argv[]){ SqStack s; int e; InitStack(s); Push(s,1); Push(s,2); Push(s,3); Pop(s); Pop(s); Pop(s); GetTop(s); Push(s,4); Push(s,5); Pop(s); Push(s,6); Push(s,7); GetTop(s); Push(s,8); Push(s,9); Push(s,10); Pop(s); Pop(s); Pop(s); Pop(s); Pop(s); Pop(s); Pop(s); Pop(s); cin>>e; return 0;}
0 0
- 数据结构之栈的定义
- 数据结构之堆的定义
- 数据结构---栈的定义
- 定义栈的数据结构
- 数据结构之栈的定义及python实现
- 数据结构之堆栈8定义的实现
- 数据结构之图的定义与术语
- 数据结构之结构定义
- 数据结构(15)栈的定义和抽象定义
- 栈的数据结构定义及算法
- c++ 数据结构 顺序栈的类定义
- 【Java笔试题】定义栈的数据结构
- 数据结构栈的定义(java)
- 数据结构的形式定义
- 数据结构 - 图的定义
- 1.数据结构的定义
- 数据结构的定义
- 数据结构的基本定义
- 循环单链表的结构和定义
- 数据结构之循环双链表的结构和实现
- Java 的 FileFilter文件过滤,readline读行操作
- proxool数据库连接池框架
- bat 批处理变量
- 数据结构之栈的定义
- 数据结构之队列
- 数据结构之二叉树
- 用java语言编写的简单二叉树
- NYOJ513 A+B Problem IV
- 根据GB2312编码表求汉字字节
- effective java
- 回调机制和模版方法
- eclipse 安装/卸载插件