栈与队列-顺序队列与链队列类模板的实现(数据结构基础 第3周)
2016-06-30 21:43
1176 查看
这里Queue写的跟在STL中的使用方法基本上是一致的,经过了简单的测试。需要注意的是下面测试程序中并没有制定顺序列表(即数组)的大小,类模板中默认为10,当数据量超过指定大小时自动开辟一个更大的数组。
运行测试程序的结果:
源码
//Queue.h
//source.cpp
运行测试程序的结果:
源码
//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; }
相关文章推荐
- 剑指offer(二.1)数据结构篇之数组
- OJ_华南农业大学_数据结构_基数排序
- HDU 3584 树状数组
- 栈与队列-出栈序列统计(数据结构基础 第3周)
- HDU 2642 树状数组
- MySQL索引背后的数据结构及算法原理
- ATS缓存数据结构
- 数据结构(主席树):HDU 4729 An Easy Problem for Elfness
- 数据结构(王道)【线性表】【算法1.1-1.2】
- 数据结构——c语言描述 第五章 (1) 特殊矩阵的压缩存储
- OpenCV_(2):常用数据结构和函数
- 算法导论_第十章_基本数据结构
- 数据结构复习——线性表的链式存储实现(双向链表)
- 51NOD1766 树上的最远点对
- Redis内部数据结构详解(2)--sds
- 数据结构也不是那么没意思之双向循环链表
- 《数据结构》复习之线性表(顺序表和链表)
- 数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)
- 数据结构与算法简记:希尔排序
- 栈与队列-顺序栈与链栈类模板的实现(数据结构基础 第3周)