简单数据结构的实现之链队列
2011-09-19 16:18
417 查看
#include <iostream> using namespace std; #ifndef LINKQUEUE_H_INCLUDED #define LINKQUEUE_H_INCLUDED template <class ElemType> struct QNode { ElemType data; QNode* next; }; template <class ElemType> class LinkQueue { private: QNode<ElemType>* front; QNode<ElemType>* rear; public: LinkQueue(); ~LinkQueue(); void ClearQueue(); bool QueueEmpty(){return front==rear;} int QueueLength(); QNode<ElemType>* Front(){return front;} QNode<ElemType>* Rear(){return rear;} bool GetHead(ElemType& e); bool EnQueue(ElemType e); bool DeQueue(ElemType& e); }; template <class ElemType> LinkQueue<ElemType>::LinkQueue() { front=rear=new QNode<ElemType>; front->next=NULL; } template <class ElemType> LinkQueue<ElemType>::~LinkQueue() { while(front) { rear=front->next; delete front; front=rear; } } template <class ElemType> void LinkQueue<ElemType>::ClearQueue() { QNode<ElemType>* p=front->next; while(p) { rear=p->next; delete p; p=rear; } rear=front; front->next=NULL; } template <class ElemType> int LinkQueue<ElemType>::QueueLength() { int i=0; QNode<ElemType>* p=front->next; while(p) { i++; p=p->next; } return i; } template <class ElemType> bool LinkQueue<ElemType>::GetHead(ElemType& e) { if(front==rear) { return false; } else { e=front->next->data; return true; } } template <class ElemType> bool LinkQueue<ElemType>::EnQueue(ElemType e) { QNode<ElemType>* p=new QNode<ElemType>; if(!p) { return false; } else { p->data=e; rear->next=p; rear=p; p->next=NULL; return true; } } template <class ElemType> bool LinkQueue<ElemType>::DeQueue(ElemType& e) { if(front==rear) { return false; } else { QNode<ElemType>* p=front->next; e=p->data; front->next=p->next; if(rear==p) { rear=front; } delete p; return true; } } #endif // LINKQUEUE_H_INCLUDED
相关文章推荐
- 数据结构 --静态队列的一个简单的C语言代码实现
- 请实现一个队列,既可以存放整数,又可以存放字符串。简单的说,队列是一种数据结构,按照先进先出的顺序管理进、出队列的元素
- 请实现一个队列,既可以存放整数,又可以存放字符串。简单的说,队列是一种数据结构,按照先进先出的顺序管理进、出队列的元素
- 请实现一个队列,既可以存放整数,又可以存放字符串。简单的说,队列是一种数据结构,按照先进先出的顺序管理进、出队列的元素
- 简单数据结构之队列(C++模板库实现)
- Java基础课程学习总结,使用LinkedList简单模拟队列数据结构和堆栈数据结构的实现
- 数据结构学习的简单问题(四):用队列自己实现String类
- 简单线性数据结构:栈和队列的实现
- 数据结构 - 队列简介 及 1个简单的c语言链式队列代码实现
- Java数据结构之队列的实现以及队列的应用之----简单生产者消费者应用
- 【数据结构+Java】Java实现简单队列、循环队列操作
- 数据结构学习三(一个简单的队列实现)
- 几种简单的数据结构的C++实现,树,图,链表,环形队列,栈等---环形队列篇
- 几种简单的数据结构的C++实现,树,图,链表,环形队列,栈等---栈篇
- 算法与数据结构基础6:C++队列的简单实现
- 数据结构——循环队列的简单实现
- 简单数据结构的实现之循环队列
- 数据结构之简单队列的实现
- java 数据结构 队列的简单实现
- 数据结构之队列的定义与简单实现