数据结构—队列(Queue)

  • 队列的定义--Queue

   队列是只允许在表的队尾插入,在表的队头进行删除。队列具有先进先出的特性(FIFO, First In First Out)。

    

 

  • 队列提供了下面的操作
     q.empty()               如果队列为空返回true,否则返回false
     q.size()                返回队列中元素的个数
     q.pop()                 删除队列首元素但不返回其值
     q.front()               返回队首元素的值,但不删除该元素
     q.push()                在队尾压入新元素
     q.back()                返回队列尾元素的值,但不删除该元素
  • 队列的实现

     下面是用C++实现的一个队列结构的源码(链表)

         

  1 #pragma once
  2 #include<iostream>
  3 #include<assert.h>
  4 using namespace std; 
  5 template<typename T>
  6 class Queue
  7 {
  8 public:
  9     Queue()
 10         :pHead(NULL)
 11         , pTail(NULL)
 12     {}
 13     Queue(const QueueNode<T>& q)
 14     {
 15         QueueNode<T>*Node = q.pHead;
 16         while (Node)
 17         {
 18             Push(Node->data);
 19             Node = Node->next;
 20         }
 21     }
 22     ~Queue()
 23     {
 24         QueueNode<T> *Node = pHead;
 25         while (Node)
 26         {
 27             QueueNode<T>*Del = Node;
 28             Node = Node->next;
 29             delete Del;
 30         }
 31         pHead = NULL;
 32         size = 0;
 33     }
 34     QueueNode<T>& operator = (const QueueNode<T>& q)
 35     {
 36         if (this != &q)
 37         {
 38            Node<T> *pNode = other._pHead;
 39             while (NULL != pNode)
 40             {
 41                 Push(pNode->_data);
 42                 pNode = pNode->_pNext;
 43             }
 44         }
 45 
 46         return *this;
 47     }
 48     void Push(const T &data)
 49     {
 50         if (pHead == NULL)
 51         {
 52             pTail = pHead = new QueueNode<T>(data);
 53         }
 54         else
 55         {
 56             pTail ->next = new QueueNode<T>(data);
 57             pTail = pTail->next;
 58         }
 59     }
 60     void Pop()
 61     {
 62         assert(NULL != pHead);
 63         if (pHead == pTail)
 64         {
 65             delete pHead;
 66             pHead = pTail = NULL;
 67         }
 68         else
 69         {
 70             QueueNode<T>*Del = pHead;
 71             pHead = pHead->next;
 72             delete Del;
 73         }
 74     }
 75     bool Empty()
 76     {
 77         return pHead == NULL;
 78     }
 79     T& Fornt()
 80     {
 81         assert(pHead);
 82 
 83         return pHead->data;
 84     }
 85 
 86     T& Back()
 87     {
 88         assert(pTail);
 89 
 90         return pTail->data;
 91     }
 92     size_t Size()
 93     {
 94         while (pHead != NULL)
 95         {
 96             size++;
 97             pHead = pHead->next;
 98         }
 99         return size;
100     }
101     void Print()
102     {
103         QueueNode<T>*cur = pHead;
104         while (cur)
105         {
106             cout << cur->data << "->";
107             cur = cur->next;
108         }
109         cout << "NULL";
110     }
111 protected:
112     QueueNode<T>* _BuyNode(const T& data)
113     {
114         return new QueueNode<T>(data);
115     }
116 protected:
117     QueueNode<T>* pHead;
118     QueueNode<T>* pTail;
119     size_t size;
120 };
121 void main()
122 {
123     Queue<int> q;
124     q.Push(6);
125     q.Push(6);
126     q.Push(9);
127     q.Pop();
128     q.Print();
129     cout<<q.Size();
130     cout<<q.Fornt();
131     cout << q.Back();
132     system("pause");
133 
134 }

 

posted @ 2016-05-07 23:29  A_carat_tear  阅读(337)  评论(0编辑  收藏  举报