c/c++栈与队的数组实现方法
来源:互联网 发布:数据交换平台etl 编辑:程序博客网 时间:2024/06/10 02:37
stack与queue均为容器适配器,下面使用数组容器实现栈和队的基本操作:
stack:
class stack {private: int * storage; int max_size; int top;public: stack() { this->storage = new int[MAX_SIZE](); this->max_size = MAX_SIZE; this->top = -1; } ~stack() { delete []this->storage; } void push(int); void pop(void); int peek(void); bool isEmpty(void); bool isFull(void); void clear(void);};
void stack::push(int data) { if (this->isFull()) { return; } this->top++; this->storage[top] = data;} void stack::pop(void) { if (this->isEmpty()) { return; } this->top--;}int stack::peek(void) { if (this->isEmpty()) { return 0; } return storage[top];} bool stack::isEmpty(void) { return (this->top == -1); } bool stack::isFull(void) { return (this->top + 1 >= this->max_size); } void stack::clear(void) { this->top = -1; }
queue:
class queue {private: int * storage; int max_size; int head; int rear;public: queue() { // notice the capability is still MAX_SIZE this->storage = new int[MAX_SIZE+1](); this->max_size = MAX_SIZE+1; this->head = 0; this->rear = 0; } ~queue() { delete []storage; } void push(int); void pop(void); int front(void); int back(void); bool isFull(void); bool isEmpty(void); void clear(void);};
void queue::push(int data) { if (this->isFull()) { return; } this->storage[rear] = data; this->rear = (this->rear + 1) % this->max_size;} void queue::pop(void) { if (this->isEmpty()) { return; } this->head = (this->head + 1) % this->max_size;} int queue::front(void) { if (this->isEmpty()) { return 0; } return this->storage[head];} int queue::back(void) { if (this->isEmpty()) { return 0; } return this->storage[(rear-1+max_size) % max_size];} bool queue::isFull(void) { return ((this->rear + 1) % this->max_size) == this->head;} bool queue::isEmpty(void) { return this->head == this->rear; } void queue::clear(void) { this->rear = this->head; }
0 0
- c/c++栈与队的数组实现方法
- 字符串动态数组的C实现方法
- 栈的C数组实现
- 纯c语言实现动态分配多维数组的方法
- 数组排序方法及C实现的总结
- 数组排序方法及C实现的总结
- 数组排序方法及C实现的总结
- 数组排序方法及C实现的总结
- 纯c语言实现动态分配多维数组的方法
- 数组排序方法及C实现的总结
- 栈的C语言 数组实现
- 栈的C语言实现(数组)
- 栈的数组实现(C语言版)
- C-026.二维数组的基本概念与初始化方法
- C语言 数组的顺序表示与实现 数据结构
- Java与C语言数组实现机理的一点对比
- 数组的实现---C语言
- 动态数组的 C 实现
- uva10271
- TCP/IP总结
- 【iOS--pod】使用初体验 --> libjingle_peerconnection
- 阿里云ECS在linux系统下安装mysql
- 解决Matplotlib绘制的图表不能正确显示中文
- c/c++栈与队的数组实现方法
- maven权威指南archetype创建简单项目报错解决
- 接口。。。。。。
- maven搭建mybatis--入门程序--001
- 移动互联
- MVP for Android
- [J2SE]s02e10.Set的实现类HashSet与TreeSet(课后练习)
- swift基本语法 (三)
- substance