队列和栈简单实现
来源:互联网 发布:java编码哪个是中文的 编辑:程序博客网 时间:2024/05/20 11:20
数据结构队列
队列是一种访问受限的线性表。队列的元素只能从表的一端插入,另一端删除(即队头插入,队尾删除)。
队列的实现中,主要涉及到初始化,入队,出队,判断队空,判断队满,遍历队列等操作。下面是对队列的简单实现。
/***环形队列实现*****/// 头文件class MyQueue{public: MyQueue(int queueCapacity); // 初始化队列,创建对垒 virtual ~MyQueue(); // 销毁队列 void ClearQueue(); // 清空队列 bool QueueEmpty() const; // 判空队列 bool QueueFull() const; // 判满队列 int QueueLength() const; // 队列长度 bool EnQueue(int element); // 入队操作 bool DeQueue(int &element); // 出队操作 void QueueTravese(); // 遍历队列private: int *m_pQueue; // 队列数组指针 int m_iQueueLen; // 队列元素个数 int m_iQueueCapacity; // 队列数组容量 int m_iHead; // 队列头指针 int m_iTail; // 队列尾指针 // 源文件#include "MyQueue.h"#include <iostream>using namespace std;MyQueue::MyQueue(int queueCapacity){ m_iQueueCapacity = queueCapacity; m_iHead = 0; m_iTail = 0; m_iQueueLen = 0; m_pQueue = new int[queueCapacity];}MyQueue::~MyQueue(){ delete [] m_pQueue; m_pQueue = NULL;}void MyQueue::ClearQueue(){ m_iHead = 0; m_iTail = 0; m_iQueueLen = 0;}bool MyQueue::QueueEmpty() const{ return m_iQueueLen == 0 ? true : false;}bool MyQueue::QueueFull() const{ return m_iQueueCapacity == m_iQueueLen ? true : false;}int MyQueue::QueueLength() const{ return m_iQueueLen;}bool MyQueue::EnQueue(int element){ if (QueueFull()) return false; m_pQueue[m_iTail] = element; m_iTail++; m_iTail = m_iTail % m_iQueueCapacity; m_iQueueLen++; return true;}bool MyQueue::DeQueue(int &element){ if (QueueEmpty()) return false; element = m_pQueue[m_iHead]; m_iHead++; m_iHead = m_iHead % m_iQueueCapacity; m_iQueueLen--; return true;}void MyQueue::QueueTravese(){ for (int i = m_iHead; i < m_iHead + m_iQueueLen; i++) { cout << m_pQueue[i % m_iQueueCapacity] << " "; } cout << endl;}// 测试文件#include "MyQueue.h"#include <iostream>using namespace std;int main(){ MyQueue *p = new MyQueue(4); p->EnQueue(10); p->EnQueue(12); p->EnQueue(8); p->EnQueue(3); p->EnQueue(5); p->QueueTravese(); int e = 0; p->DeQueue(e); cout << e << endl; p->DeQueue(e); cout << e << endl; p->QueueTravese(); p->ClearQueue(); p->QueueTravese(); p->EnQueue(20); p->EnQueue(5); int len = p->QueueLength(); cout << len << endl; p->QueueTravese(); delete p; p = NULL; return 0;}
栈也是操作受限的线性表。它只允许在一端进行插入和删除操作。栈的主要操作包括:判断栈是否为空,栈是否满,栈中元素个数,入栈操作,出栈操作等。
栈的应用:递归调用,表达式求值,进制转换,括号匹配判断,深度遍历等。
下面是栈的基本实现:
// 头文件#ifndef STACK_H#define STACK_Hclass MyStack{public: MyStack(int size); // 分配栈空间 virtual ~MyStack(); // 栈析构函数 bool stackEmpty(); // 判断栈空 bool stackFull(); // 判断栈满 void clearStack(); // 清空栈 int stackLength(); // 栈中元素长度 bool push(char elem); // 入栈操作 bool pop(char &elem); // 出栈操作 void stackTraverse(); // 栈遍历操作private: char *m_pBuffer; // 栈空间指针 int m_iSize; // 栈容量 int m_iTop; // 栈顶指针};#endif// 源文件#include"MyStack.h"#include <iostream>using namespace std;MyStack::MyStack(int size){ m_iSize = size; m_pBuffer = new char[size]; m_iTop = 0;}MyStack::~MyStack(){ delete []m_pBuffer; m_pBuffer = NULL;}bool MyStack::stackEmpty(){ return m_iTop == 0 ? true : false;}bool MyStack::stackFull(){ return m_iSize == m_iTop ? true : false;}void MyStack::clearStack(){ m_iTop = 0;}int MyStack::stackLength(){ return m_iTop;}bool MyStack::push(char elem){ if (stackFull()) return false; m_pBuffer[m_iTop++] = elem; return true;}bool MyStack::pop(char &elem){ if (stackEmpty()) return false; elem = m_pBuffer[--m_iTop]; return true;}void MyStack::stackTraverse(){ for (int i = 0; i < m_iTop; i++) { cout << m_pBuffer[i] << " "; } cout << endl;}// 测试文件#include "MyQueue.h"#include "MyStack.h"#include <iostream>using namespace std;int main(){ MyStack *pStack = new MyStack(5); int len = pStack->stackLength(); cout << len << endl; pStack->push('h'); pStack->push('m'); pStack->push('e'); pStack->push('l'); pStack->push('y'); pStack->push('r'); if (pStack->stackEmpty()) { cout << "栈为空" << endl; } if (pStack->stackFull()) { cout << "栈为满" << endl; } pStack->stackTraverse(); char t; pStack->pop(t); pStack->stackTraverse();}
0 0
- 队列和栈简单实现
- 栈和队列:栈的简单实现
- 栈和队列简单代码实现
- 简单的栈和队列经典实现
- java实现简单的队列和栈
- 栈和队列的简单实现
- c++ 栈,队列,循环队列 简单实现
- 简单题_用两个栈实现队列和用两个队列实现一个栈
- 用栈和队列实现简单的停车场管理系统
- 算法导论-栈和队列的简单实现
- 简单线性数据结构:栈和队列的实现
- 简单实现栈和队列以及其面试题
- 用链表实现栈和队列对简单数学表达式求值
- 简单队列和循环队列的实现--数组实现
- 简单使用栈实现队列
- 队列实现栈和栈实现队列
- spring+rabbitmq的简单实现和延迟队列实现
- 简单算法 - 两个队列实现一个栈
- 关于volatile关键字
- leetcode_c++:Missing Number(268)
- dagger2深入学习
- Greenplum或DeepGreen的管理数据
- 准备 LVM Volume Provider - 每天5分钟玩转 OpenStack(49)
- 队列和栈简单实现
- Error:(3) Error retrieving parent for item: No resource found that matches the given name 'android:T
- caption标签,为表格添加标题和摘要
- MFC使用按钮控件,满足条件下可用或者不可用
- 山东省第七届ACM大学生程序设计竞赛-Reversed Words
- ios计算函数的执行时长
- javaScript的三组成部分简介
- ExecutorService线程池讲解
- Arduino的程序烧录