Chinaunix首页 | 论坛 | 博客
  • 博客访问: 76536
  • 博文数量: 35
  • 博客积分: 131
  • 博客等级: 入伍新兵
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-11 22:57
文章分类

全部博文(35)

文章存档

2016年(15)

2013年(1)

2012年(6)

2011年(13)

我的朋友

分类: C/C++

2012-04-07 22:09:25

队列这种数据结构与栈一样,是一个工具性的数据结构,通常被其它复杂数据结构所使用。比如实现二叉树的遍历的非递归算法(层次遍历)。下面就用C++模板实现队列数据结构的一个较完整代码!队列可以以用数组,也可以用链表实现,这里只用双向链表实现这一数据结构。

点击(此处)折叠或打开

  1. //Queue.h
  2. //双链表队列数据结构C++模块的实现
  3. //理论知识参考《数据结构(C语言版)--严慰明》
  4. #ifndef _QUEUE_H_
  5. #define _QUEUE_H_

  6. namespace _QUEUE_
  7. {
  8.     //队列中的数据元素
  9.     template <typename T>
  10.     class QueueNode
  11.     {
  12.     public:
  13.         QueueNode()
  14.         {
  15.             this->mElement    = 0;
  16.             this->mpNext = this->mpPrev    = NULL;
  17.         }
  18.         T                mElement;
  19.         QueueNode<T>*    mpNext;
  20.         QueueNode<T>* mpPrev;
  21.     };

  22.     template <typename T>
  23.     class Queue
  24.     {
  25.     public:
  26.         Queue()
  27.         {
  28.             QueueNode<T> * pNode = new QueueNode<T>;
  29.             pNode->mElement = T(-1);
  30.             pNode->mpNext = pNode->mpPrev = NULL;
  31.             this->mpHead = this->mpTail = pNode;
  32.         }

  33.         ~Queue()
  34.         {
  35.             this->clear();
  36.             delete this->mpHead;
  37.             this->mpHead = this->mpTail = NULL;
  38.         }

  39.         bool insert(T element);
  40.         T    front();
  41.         T    back();
  42.         bool isEmpty(void);
  43.         bool clear(void);
  44.         int size();

  45.         friend ostream& operator<< <>(ostream& ostr,const Queue<T>& q);
  46.     private:
  47.         QueueNode<T>* mpHead;
  48.         QueueNode<T>* mpTail;
  49.     };

  50.     template <typename T>
  51.     bool Queue<T>::insert(T element)
  52.     {
  53.         QueueNode<T> * pNode = new QueueNode<T>;
  54.         if (pNode == NULL)    return false;
  55.         pNode->mElement    = element;

  56.         this->mpTail->mpNext = pNode;
  57.         pNode->mpPrev = this->mpTail;
  58.         this->mpTail = this->mpTail->mpNext;

  59.         return true;
  60.     }

  61.     template <typename T>
  62.     T    Queue<T>::front()
  63.     {
  64.         T element = T();
  65.         QueueNode<T>* pNode = NULL;
  66.         if (!this->isEmpty())
  67.         {
  68.             pNode = this->mpHead->mpNext;
  69.             element = pNode->mElement;
  70.             this->mpHead ->mpNext = pNode->mpNext;
  71.             if (pNode->mpNext)
  72.                 pNode->mpNext->mpPrev = this->mpHead;
  73.             if (pNode == this->mpTail)
  74.                 this->mpTail = this->mpHead;
  75.             delete pNode;
  76.         }
  77.         return element;
  78.     }

  79.     template <typename T>
  80.     T    Queue<T>::back()
  81.     {
  82.         T element = T();
  83.         QueueNode<T>* pNode = NULL;
  84.         if (!this->isEmpty())
  85.         {
  86.             pNode = this->mpTail;
  87.             element = pNode->mElement;
  88.             this->mpTail = this->mpTail->mpPrev;
  89.             this->mpTail->mpNext = NULL;
  90.             delete pNode;
  91.         }
  92.         return element;
  93.     }

  94.     template <typename T>
  95.     bool Queue<T>::isEmpty(void)
  96.     {
  97.         return (this->mpTail == this->mpHead);
  98.     }

  99.     template <typename T>
  100.     bool Queue<T>::clear(void)
  101.     {
  102.         while (!this->isEmpty())
  103.             this->back();
  104.         return true;
  105.     }

  106.     template <typename T>
  107.     int Queue<T>::size()
  108.     {
  109.         int iCount = 0;
  110.         if (!this->isEmpty())
  111.         {
  112.             QueueNode<T>* pNode = this->mpTail;
  113.             while (pNode != this->mpHead)
  114.             {
  115.                 iCount++;
  116.                 pNode = pNode->mpPrev;
  117.             }
  118.         }
  119.         return iCount;
  120.     }

  121.     template <typename T>
  122.     ostream& operator<< <>(ostream& ostr,const Queue<T>& q)
  123.     {
  124.         QueueNode<T>* pNode = q.mpHead->mpNext;
  125.         while (pNode != NULL)
  126.         {
  127.             ostr << pNode->mElement << ",";
  128.             pNode = pNode->mpNext;
  129.         }
  130.         return ostr;
  131.     }
  132. }
  133. #endif
以上就是队列数据结构的实现。
阅读(1742) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~