I use the class template to complete the operation of the Loop list implementation queue. First define a node class node to save the node information, and then define queue class queue, then we think: the 4 basic operations to complete the queue is
1. Determine if the queue is empty
2. Push into data at the end of the queue
3. Remove data from Queue head
4. Delete the elements of the queue header
What member variables we need for this queue class.
The answer is: (Maintenance) queue tail node, queue size is enough.
When we analyze the tail push data, we just need to insert the node between Myback and Myback->next, and then point the Myback to the node. Remove and delete the head data only need to operate on the Myback->next, the complexity is O (1), high efficiency.
The rest of the implementation details look at my following code implementation:
Circle_list.h #ifndef circle_list #define Circle_list #include <iostream> #include <string> #include <
Cstring> template <typename t> class node//node class {public:t data;
Node *next;
Node (T da = 0, node *n = NULL):d ata (DA), Next (n) {}};
Template <typename t> class Queue {public:node<t> *myback;
int size;
Queue (node<t> *begin = NULL, int s = 0): Myback (begin), size (s) {} bool empty ();
void Enqueue (t value);//back pressure into T front ();
void display ();
void Dequeue ();//Before deleting ~queue ();
Queue (const queue<t> &temp);
Queue<t> operator= (const queue<t>temp);
};
Template <typename t> bool Queue<t>::empty () {if (size = = 0) return true;
return false;
} template <typename t> queue<t>::queue (const queue<t> &temp) {size = 0;
node<t>* scan = (temp.myback)->next;
while (scan!= temp.myback) {enqueue (scan->data);
Scan = scan->next;
} enqueue (Temp.myback->data); } teMplate <typename t> queue<t> queue<t>::operator= (const queue<t> temp) {size = 0;
node<t>* scan = (temp.myback)->next;
while (scan!= temp.myback) {enqueue (scan->data);
Scan = scan->next;
} enqueue (Temp.myback->data);
return *this;
} template <typename t> void Queue<t>::enqueue (T value) {node<t>*last = new node<t>;
Last->data = value;
if (size = = 0) {myback = last;
Myback->next = Myback;
else {node<t> *temp = myback->next;
Myback->next = Last;
Last->next = temp;
Myback = Last;
Myback->next = temp;
} size++; } template <typename t> T Queue<t>::front () {return (Myback->next)->data;} template <typename T>
;
void Queue<t>::d isplay () {if (size = = 1) {cout << myback->data << Endl;
Return
else {node<t> *first = myback->next; while (the!= myback) {cout << first->dATA << "";
i = first->next;
} cout << myback->data;
cout << Endl;
} template <typename t> void queue<t>::d equeue () {node<t>*cur = myback->next;
Node <t>*now = cur->next;
Myback->next = Now;
Delete cur;
size--;
} template <typename t> Queue<t>::~queue () {if (size = = 0) {} else {node<t> *p = myback->next;
node<t> *nex = p->next;
while (P!= myback) {delete p;
p = NEX;
NEX = nex->next;
} Delete myback; }} #endif
To test:
Main.cpp
#include "circle_list.h"
using namespace std;
int main ()
{
queue<int> q;
cout << queue is empty. "<< q.empty () << Endl;
Q.enqueue (1);
Q.enqueue (2);
Q.enqueue (3);
cout << "Output data from the first queue:" << Endl;
Q.display ();
Queue<int> A (q);
cout << "Outputs the data in a queue created by a copy constructor:" << Endl;
A.display ();
Queue<int> b;
b = q; cout << "outputs data in a queue established by an assignment operator Overload: << Endl;
B.display ();
cout << "Output queue head elements:" << Endl;
cout << Q.front () << Endl;
Q.dequeue ();
cout << "Delete queue after the first element of the team:" << Endl;
Q.display ();
cout << q.empty () << Endl;
return 0;
}
Screenshot of the experimental results;