链队列介绍及其C++实现
2016-11-27 23:30
204 查看
介绍
队列的链接存储结构及实现:改造单链表实现队列的链接存储。
队头指针即为链表的头指针
类声明如下
———————————————————————————————————————————————————————————
实现代码
头文件LinkQueue.h#ifndef LinkQueue_byNim #define LinkQueue_byNim #include<iostream> using namespace std; template<class T> struct Node{ T data; //数据域 Node *next; //指针域 }; template<class T> class LinkQueue { private: Node<T> *front,*rear; public: LinkQueue(); ~LinkQueue(); void EnQueue(T e); T DeQueue(); T GetQueue(); bool empty(); }; template<class T> LinkQueue<T>::LinkQueue() //构造函数 { front=new Node<T>; front->next=NULL; rear=front; } template<class T> LinkQueue<T>::~LinkQueue() { delete front; } template<class T> void LinkQueue<T>::EnQueue(T e) { Node<T> *aNode; aNode=new Node<T>; aNode->data=e; aNode->next=NULL; rear->next=aNode; rear=aNode; } template<class T> T LinkQueue<T>::DeQueue() { if(rear==front) throw "下溢"; Node<T> *p; p=front->next; T x=p->data; front->next=p->next; if(p->next==NULL) rear=front; //边界情况队列中只有一个元素 delete p; //及时释放内存,避免内存泄漏 return x; } template<class T> T LinkQueue<T>::GetQueue() { if(rear==front) throw "下溢"; return front->next->data; } template<class T> bool LinkQueue<T>::empty() { if(rear==front) return true; return false; } #endif
相关文章推荐
- 循环顺序队列介绍及其C++实现
- 链队列的C++实现及其应用
- 数据结构学习笔记(四)队列及其C++实现
- 数据结构——队列——C++实现队列及其操作
- 数据结构:二项队列原理及其C++实现
- 栈和队列及其C++实现(一)
- 仿基因编程原理及其C++实现
- IOC介绍及IOC模式的类型及其实现
- 内存管理:算法及其c/c++实现 翻译五
- 内存管理:算法及其c/c++实现 翻译六
- 仿基因编程原理及其C++实现
- 仿基因编程原理及其C++实现
- 最小堆优先队列C++实现
- c++模板实现的队列
- SSE指令介绍及其C、C++应用
- Zeller公式及其c++实现
- SSE指令介绍及其C、C++应用 zz
- 内存管理:算法及其c/c++实现 翻译四
- [转载]SSE指令介绍及其C、C++应用
- SSE指令介绍及其C、C++应用