栈与队列-顺序队列与链队列类模板的实现(数据结构基础 第3周)
来源:互联网 发布:知乎 小知识 编辑:程序博客网 时间:2024/04/25 18:57
这里Queue写的跟在STL中的使用方法基本上是一致的,经过了简单的测试。需要注意的是下面测试程序中并没有制定顺序列表(即数组)的大小,类模板中默认为10,当数据量超过指定大小时自动开辟一个更大的数组。
运行测试程序的结果:
源码
//Queue.h
#pragma template <class T>class Queue {public: void clear(); bool push(const T& item); //inserts element at the end bool pop(); //removes the first element T front(); //access the first element bool empty(); //checks whether the underlying container is empty bool full(); ////checks whether the underlying container is full};template <class T>class arrQueue : public Queue<T> {private: int msize; //存放队列的数组的大小 int mfront; //表示队头所在位置的下标 int mrear; //表示队尾所在位置的下标 T* qu; //存放类型为T的队列元素的数组public: arrQueue(int size=10) { //创建队列的实例 msize = size+1;; //浪费一个存储空间,以区别队列空和队列满 qu=new T[msize]; mfront=mrear=0; } ~arrQueue() { //消除该实例,并释放其空间 delete [] qu; } void clear() { //清空队列 mfront = mrear; } bool push(const T& item) { //item入队,插入队尾 if (((mrear+1)%msize)==mfront) { //队列已满 int newsize = msize*2; T* newqu = new T[newsize]; int newfront=0; int newrear=0; while(mfront != mrear) { //老队列非空 newqu[newrear]=qu[mfront]; newrear = (newrear+1)%newsize; mfront = (mfront+1)%msize; } delete [] qu; qu = newqu; mfront = newfront; mrear = newrear; msize = newsize; } qu[mrear]=item; mrear = (mrear+1)%msize; //循环后继 return true; } T front() { //返回队头元素 if (mfront == mrear) { cout << "queue is empty" << endl;// return false; } return qu[mfront]; } bool pop() { //弹出队头元素 if (mfront == mrear) { cout << "queue is empty" << endl; return false; } mfront = (mfront+1)%msize; return true; } bool empty() { if (mfront == mrear) return true; else return false; } bool full() { if (((mrear+1)%msize)==mfront) return true; else return false; }};template <class T> class Link{public: T data; //用于保存结点元素的内容 Link<T> *next; //指向后继结点的指针 Link(const T info, Link<T>* nextValue=NULL) { data=info; next= nextValue; } Link():next(NULL) {}};template <class T>class lnkQueue:public Queue <T> {private: int msize; Link<T>* mfront; Link<T>* mrear;public: lnkQueue(int size=0) { msize = size; mfront = mrear = NULL; } ~lnkQueue() { clear(); } void clear() { while(mfront != NULL) { mrear = mfront; mfront = mfront->next; delete mrear; } mrear = NULL; msize = 0; } bool push(const T& item) { if (mrear == NULL) { mfront = mrear = new Link<T>(item); } else { mrear->next = new Link<T>(item); mrear = mrear->next; } msize++; return true; } bool pop() { if (msize==0) { cout << "queue is empty" << endl; return false; } Link<T>* tmp; tmp = mfront; mfront = mfront->next; delete tmp; if (mfront==NULL){ mrear = NULL; } msize--; return true; } T front() { if (msize==0) { cout << "queue is empty" << endl;// return false; } return mfront->data; } bool empty() { if (msize==0) return true; else return false; }};
//source.cpp
#include <iostream>#include"Queue.h"using namespace std;int main() { arrQueue<int> arrs; lnkQueue<int> lnks; for (int i=1; i<=11; i++){ arrs.push(i); lnks.push(i); } int topelem; cout << "arrQueue中元素依次为:"; while(!arrs.empty()) { topelem = arrs.front(); //返回栈顶内容但不弹出 cout << topelem << ' '; arrs.pop(); //弹出栈顶内容且返回其值 } cout << endl; cout << "lnkQueue中元素依次为:"; while(!lnks.empty()) { topelem = lnks.front(); //返回栈顶内容但不弹出 cout << topelem << ' '; lnks.pop(); //弹出栈顶内容且返回其值 } cout << endl; return 0;}
0 0
- 栈与队列-顺序队列与链队列类模板的实现(数据结构基础 第3周)
- 栈与队列-顺序栈与链栈类模板的实现(数据结构基础 第3周)
- 数据结构--队列实现(顺序队列和链队列)与C++模板
- 数据结构-队列:循环队列与链队列的C++模板类实现
- [数据结构]队列之顺序队列的类模板实现
- 数据结构——栈与队列(顺序栈、链栈、循环队列、链队列)
- 【数据结构】栈与队列基础
- 数据结构基础【07】队列的顺序实现
- 数据结构与算法系列-队列-顺序队列
- 栈与队列-出栈序列统计(数据结构基础 第3周)
- 栈与队列-密码翻译(数据结构基础 第3周)
- 栈与队列-等价表达式(数据结构基础 第3周)
- 数据结构-- 队列 循环与顺序
- 数据结构与算法-----队列-使用数组(顺序结构)实现
- 数据结构——循环队列(顺序队列)模板类实现
- 数据结构-->(循环)队列 【队列的顺序实现】ADT
- 数据结构的栈与队列
- 数据结构---队列的顺序实现
- Codeforces 687C The Values You Can Make(背包)
- java中如何写一个死锁例子
- 模版方法模式对hibernate的封装
- MySQL使用TEXT/BLOB类型的几点注意
- 自己动手之使用反射和泛型,动态读取XML创建类实例并赋值
- 栈与队列-顺序队列与链队列类模板的实现(数据结构基础 第3周)
- 实时应用系统监控平台
- 2016/6/30 校验问题--加班中
- JDK配置
- android 二维码 扫描与生成(内置)
- 设计模式从零开始之简单工厂
- Android studio 项目Gradle升级后报错Session 'app': Error Launching activity
- 浅谈AOP
- 【Struts2】基础总结