# Encapsulates a QueueIn this blog, I will try to encapsulate a queue.
# DefinitionsIn computer science, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence.
# FunctionThis queue contains these functions:
Getting the size of the queue. Checking the queue whether empty or not. Inserting element in anywhere in the queue. Deleting element in anywhere in the queue. Getting element in anywhere in the queue. Showing the whole queue. Clearing the whole queue. # MethodI define a class named order_team. In the class, it has a private class about DoubleLink , properties,and interface.
# DoubleLinkThe object of this class provide some interface to support the function of queue data structure.
let us look the code:
1 2 private : DoubleLink<T> *link;
# InterfaceI define these interfaces to realize the functions of the queue;
1 2 3 4 5 6 7 8 9 10 11 12 public : order_team(); ~order_team(); int size () ; void push (T x) ; void pop () ; T front () ; T back () ; bool IsEmpty () ; void clear () ; void show () ;
# ConstructorThrough the constructor, the object will difine a DoubleLink object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 template <class T >order_team <T>: :order_team(){ link=new DoubleLink<T>(); } ``` ###### Destructor Through the destructor ,we delete the Link. ```C++ template <class T >order_team <T>: :~order_team(){ delete link; }
# size()We can get the number of elements in the queue
1 2 3 4 5 template <class T >int order_team <T>: :size(){ return link->size(); }
# IsEmpty()We can check the queue whether empty or not
1 2 3 4 5 template <class T >bool order_team <T>: :IsEmpty(){ return link->IsEmpty(); }
# push()We use this function to insert the element to the queue tail.
1 2 3 4 5 template <class T >void order_team <T>: :push(T x){ link->pushBack(x); }
# front() ,back()This function can return data of elements.
1 2 3 4 5 6 7 8 9 10 11 template <class T >T order_team <T>: :front(){ return link->getHead(); } template <class T >T order_team <T>: :back(){ return link->getBack(); }
# pop()These functions can delete elements in queue.
1 2 3 4 5 template <class T >void order_team <T>: :pop(){ link->popHead(); }
# show()Showing the whole queue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 template <class T >void order_team <T>: :show(){ link->show(); } ``` ###### clear() Remove the queue . ```C++ template <class T >void order_team <T>: :clear(){ link->clear(); }
# The Whole Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 #include <iostream> #include "DoubleLink.h" using namespace std ; template <class T >class order_team { private : DoubleLink<T> *link; public : order_team(); ~order_team(); int size () ; void push (T x) ; void pop () ; T front () ; T back () ; bool IsEmpty () ; void clear () ; void show () ; }; template <class T >order_team <T>: :order_team(){ link=new DoubleLink<T>(); } template <class T >order_team <T>: :~order_team(){ delete link; } template <class T >int order_team <T>: :size(){ return link->size(); } template <class T >void order_team <T>: :push(T x){ link->pushBack(x); } template <class T >T order_team <T>: :front(){ return link->getHead(); } template <class T >T order_team <T>: :back(){ return link->getBack(); } template <class T >void order_team <T>: :pop(){ link->popHead(); } template <class T >bool order_team <T>: :IsEmpty(){ return link->IsEmpty(); } template <class T >void order_team <T>: :clear(){ link->clear(); } template <class T >void order_team <T>: :show(){ link->show(); } int main () { int n; order_team<int >* z=new order_team<int >(); cout <<"判断是否为空" <<endl ; if (z->IsEmpty()) { cout <<"空队列" <<endl ; } else { cout <<"非空" <<endl ; } cout <<"push元素" <<endl ; z->push(5 ); z->push(7 ); z->push(6 ); z->push(4 ); z->push(3 ); z->push(9 ); cout <<"判断是否为空" <<endl ; if (z->IsEmpty()) { cout <<"空队列" <<endl ; } else { cout <<"非空" <<endl ; } cout <<"展示队列的元素" <<endl ; z->show(); cout <<"获取队首元素" <<endl ; cout <<z->front()<<endl ; cout <<"获取队列大小" <<endl ; cout <<z->size()<<endl ; cout <<"删除队首元素" <<endl ; z->pop(); z->show(); cout <<"删除队首元素" <<endl ; z->pop(); z->show(); cout <<"插入队尾元素" <<endl ; cout <<"输入元素" <<endl ; cin >>n; z->push(n); z->show(); cout <<"清空队列" <<endl ; z->clear(); cout <<"判断是否为空" <<endl ; if (z->IsEmpty()) { cout <<"空队列" <<endl ; } else { cout <<"非空" <<endl ; } return 0 ; }