Queues are linear FIFO s that can be described using linked lists or arrays. A queue should have the following functions:
1. Create a queue; 2. Check if the queue is not empty; 3. Get the length of the queue; 4. Get the elements of the new team; 5. Get the elements to prepare for the team; 6. Execute a queue operation; 7. Perform a team entry operation; 8. Clear a queue and queue its elements to another queue (hereinafter referred to as "clip operation"); 9. Queue all elements of a queue into another queue, but the queue elements remain unchanged (hereinafter referred to as "replication operation");
This program uses C++ to build such a queue, using Xcode compilation. It consists of three documents. Header file, function definition file and test file. This program also uses C++ exception checking, friend function and other methods. Among them, the header file is as follows:
// // queuedefine.hpp // queue // // Created by lq on 2019/9/10. // Copyright © 2019 Mr.liang. All rights reserved. // #ifndef queuedefine_hpp #define queuedefine_hpp //Define linked list nodes struct node { node* next; node* last; int data; }; //Define Queue classes class queuedefine { private: node* queue_in;//Entry (end of team) node* queue_out;//Out of the team (team leader) int length;//queue length public: queuedefine();//Constructor ~queuedefine();//Destructor int empty_check();//Check if the queue is not empty int get_size();//Get the queue length int & front();//Return the header element int & back();//Return the tail element void pop();//Execute a team trip void push(const int & element);//Entry element void show() const;//Traversal queue //All elements in a queue are queued to another queue, but the queue elements remain unchanged. queuedefine queuecopy(queuedefine & q); //Clear one queue and put its elements into another queue. friend queuedefine & clip_paste(queuedefine & target,queuedefine & q); }; #endif /* queuedefine_hpp */
The function definition file is as follows:
// // queuedefine.cpp // queue // // Created by lq on 2019/9/10. // Copyright © 2019 Mr.liang. All rights reserved. // #include "queuedefine.hpp" #include <iostream> using namespace std; queuedefine::queuedefine() { queue_in = new node; queue_out = new node; queue_in->next = queue_out; queue_in->last = nullptr; queue_out->next = nullptr; queue_out->last = queue_in; length = 0; } queuedefine::~queuedefine() { cout<<"bye"<<endl; } int queuedefine::empty_check() { if(length == 0) { return true; } else { return false; } } int queuedefine::get_size() { return length; } int & queuedefine::front() { node* temp = queue_out->last; return temp->data; } int& queuedefine::back() { node* temp = queue_in->next; return temp->data; } void queuedefine::pop() { node* temp = queue_out->last; node* p = temp->last; p->next = queue_out; queue_out->last = p; length--; delete temp; } void queuedefine::push(const int & element) { queue_in->last = new node; node* temp = queue_in; queue_in = queue_in->last; queue_in->next = temp; queue_in->last = nullptr; temp->data = element; length++; } void queuedefine::show() const { if(length == 0) { throw "empty queue";//throw method of C++ exception } node* temp = queue_out; while(temp->last != queue_in) { temp = temp->last; cout<<temp->data<<" "; } cout<<endl; } //Reference symbols before target cannot be lost, otherwise push will fail queuedefine & clip_paste(queuedefine & target, queuedefine & q) { while(q.get_size()) { cout<<"element: "<<q.front()<<" is operated"<<endl; target.push(q.front()); q.pop(); } return target; } queuedefine queuedefine::queuecopy(queuedefine & q) { int len = q.get_size(); while(!(len == 0)) { cout<<"element: "<<q.front()<<" is operated"<<endl; push(q.front()); q.push(q.front()); q.pop(); len--; } return *this; }
The test files are as follows: (clip and copy operations are performed)
// // main.cpp // queue // // Created by lq on 2019/9/10. // Copyright © 2019 Mr.liang. All rights reserved. // #include <iostream> #include "queuedefine.hpp" using namespace std; int main(int argc, const char * argv[]) { // insert code here... queuedefine myqueue,myqueue_copy; myqueue.push(1); myqueue.push(2); myqueue.push(3); myqueue.push(4); myqueue.push(5); myqueue.push(6); myqueue_copy.push(10); myqueue.show(); myqueue.pop(); myqueue.show(); clip_paste(myqueue_copy,myqueue); //myqueue_copy.queuecopy(myqueue); myqueue_copy.show(); //try-catch method of C++ exception try { myqueue.show(); } catch (const char *s) { cout<<s<<", myqueue is empty"<<endl; } return 0; }
The results are as follows:
1 2 3 4 5 6 2 3 4 5 6 element: 2 is operated element: 3 is operated element: 4 is operated element: 5 is operated element: 6 is operated 10 2 3 4 5 6 empty queue, myqueue is empty bye bye Program ended with exit code: 0
The test file is as follows: (copy operation, no clip operation)
// // main.cpp // queue // // Created by lq on 2019/9/10. // Copyright © 2019 Mr.liang. All rights reserved. // #include <iostream> #include "queuedefine.hpp" using namespace std; int main(int argc, const char * argv[]) { // insert code here... queuedefine myqueue,myqueue_copy; myqueue.push(1); myqueue.push(2); myqueue.push(3); myqueue.push(4); myqueue.push(5); myqueue.push(6); myqueue_copy.push(10); myqueue.show(); myqueue.pop(); myqueue.show(); //clip_paste(myqueue_copy,myqueue); myqueue_copy.queuecopy(myqueue); myqueue_copy.show(); //try-catch method of C++ exception try { myqueue.show(); } catch (const char *s) { cout<<s<<", myqueue is empty"<<endl; } return 0; }
The results are as follows:
1 2 3 4 5 6 2 3 4 5 6 element: 2 is operated element: 3 is operated element: 4 is operated element: 5 is operated element: 6 is operated bye 10 2 3 4 5 6 2 3 4 5 6 bye bye Program ended with exit code: 0
--------------------
If you want to know more about C++/Java/machine learning, you are welcome to scan the two-dimensional code below, pay attention to "Mr. Liang's Memorandum", and share related technology every day. We look forward to learning with you and making progress together.
--------------------