stack
来源:互联网 发布:网络摄像机ip搜索软件 编辑:程序博客网 时间:2024/06/06 03:14
#include<iostream>#include<cstdio>#include<stdlib.h>#include<malloc.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2#define MAXSIZE 10#define INIT_SIZE 10//线性表初始长度 #define INCREMENT_SIZE 10//增量 using namespace std;typedef int ElemType;typedef int Status;typedef struct{ ElemType* base; ElemType* top; int stackSize;}Stack;Status initStack(Stack& s){ s.top=s.base=(ElemType*)malloc(sizeof(ElemType)*INIT_SIZE); if(!s.top)exit(OVERFLOW); s.stackSize=INIT_SIZE; return OK;}Status push(Stack& s,ElemType e){ if(s.top-s.base>=s.stackSize-1){ s.base=(ElemType*)realloc(s.base,sizeof(ElemType)*(s.stackSize+INCREMENT_SIZE)); if(!s.base)exit(OVERFLOW); s.stackSize=s.stackSize+INCREMENT_SIZE; } *(s.top)=e; s.top++; return OK;}Status getTop(Stack&s ,ElemType &e){ if(s.base==s.top)return ERROR; e=*(s.top-1); return OK;}Status pop(Stack& s,ElemType& e){ if(s.base==s.top)return ERROR; e=*(s.top-1); s.top--; return OK;} Status isEmpty(Stack& s){ if(s.base==s.top)return OK; return ERROR;}int main(){ Stack s; initStack(s); for(int i=0;i<15;i++){ push(s,i); } ElemType t; while(!isEmpty(s)){ pop(s,t); printf("%d ",t); } return 0;}
阅读全文
0 0
- stack
- stack
- stack
- stack
- Stack
- Stack
- Stack
- Stack
- stack
- Stack
- stack
- stack
- Stack
- stack
- Stack
- Stack
- stack
- stack.
- Python 3 教程
- 集合框架
- UNIX环境高级编程读书笔记(2)
- LeetCode 530 Minimum Absolute Difference in BST
- hive使用方式
- stack
- Qt 实现雷达图
- bubblesort and selectionsort
- C++之模板元编程
- css的几种垂直居中的方法
- 虚拟机和开发版文件传输之NFS篇
- JAVA学习49_Java生成和操作Excel文件
- HDOJ 1160 FatMouse's Speed (结构体排序+动态规划)详解
- scikit-learn学习笔记(二)load_files加载自己的文件