C + + implementation of chained queues
First, the data structure
struct Qnode //Defines the data structure of the queue node {Qnode *next;//Pointer field, pointing to the next node double data; Data fields, storing queue information};struct linkqueue //define the data structure of the queue {Qnode *front; Team first pointer, pointing to the Qnode type of pointer qnode *rear; End-of-team pointer};
void Initqueue (Linkqueue &q) //constructs an empty queue int IsEmpty (linkqueue &q) //Determines if the queue is empty void EnQueue (linkqueue &q,double e) //Insert a node from the tail of the queue void DeQueue (Linkqueue &q, double &e) //delete a node from the queue header void Destoryqueue (Linkqueue &q) Destroying a queue
Second, the complete code
#include "stdafx.h" #include <iostream> #include <cstdlib>using namespace std;struct qnode//define the data structure of the queue node { Qnode *next; Pointer field, which points to the next node, double data; Data fields, storing queue information};struct linkqueue//define the data structure of the queue {Qnode *front; Team first pointer, pointing to the Qnode type of pointer qnode *rear; Tail pointer};void initqueue (linkqueue &q)//construct an empty queue {Qnode *q;q = new Qnode; Request a node's space q->next = NULL; As the head node//team head and the tail pointer all point to this node, the pointer field is Nullq.front = q; Q.rear = q;} int IsEmpty (Linkqueue &q)//Determine if the queue is empty {if (q.rear = = Q.front) return 0;elsereturn 1;} void EnQueue (Linkqueue &q,double e)//Insert element {Qnode *p from the tail of the queue; A new node is created p = new Qnode;p->next = Null;p->data = e; Enter data information//Insert new node into queue tail q.rear->next = p; Q.rear = p; Set new tail node}void DeQueue (Linkqueue &q, double &e)//delete a node from the queue header {Qnode *p;p = Q.front->next;e = p->data; Save data to be out of queue Q.front->next = p->next; The next node is treated as the first node of the link after the head node if (q.rear = = p)///If the element to be deleted is a tail node, the head pointer is assigned to the tail pointer, together with the head node, indicating that the queue is empty q.rear = Q.front;delete p;} void DeStoryqueue (Linkqueue &q)//destroys a queue {while (Q.front) {q.rear = Q.front; Starting from the head node, one delete queue node, freeing the space delete q.front; Q.front = Q.rear;}} int _tmain (int argc, _tchar* argv[]) {linkqueue *q; Define a queue QQ = new Linkqueue; Initqueue (*q); cout << "Start entering data into the queue with 1 as the Terminator" << endl;cout << "Enter a number:" << endl;double A, x;cin ;> A;while (A! =-1) {EnQueue (*q, a); cout << "Please enter a number:" << endl;cin >> A;} Output queue element, team head-and-tail Qnode *p;p = q->front->next;if (p = = NULL)//If empty table, exit {cout << queue empty! "<< Endl;return 0;} cout << Queue data in order: << Endl;while (p!=null) {cout << p->data << "";p = P->next;} cout << endl;//Delete queue element while (! IsEmpty (*q)) {DeQueue (*q, x); cout << x << "";} Free memory Space Delete q->front;delete Q;return 0;}
third, the experiment
V. Summary
The reason why I write so detailed, I am because of the suffering of the network, in the teacher's mind my IQ is quite high, but every time I go online for help, see some other people write blog, especially depressed and powerless, some basic no comments, some have comments, but too perfunctory, some ideas too chaotic, they do not understand, write to harm, Look at the no interest, not to show people, I believe that in addition to the author, no one can read. Therefore, I am in the conscience, since the blog, insist on writing can let most people understand, I would rather spend some time to write comments, also do not want to netizens read my blog and swept the mood. Through this algorithm, I have a better understanding of the queue, because it to define two structures, the general single-linked list is a struct, so, I just started to take some time to understand why, let yourself a function to implement the function, and finally through the main test to get the final desired results. I believe that the practice is the most able to improve the level of programming, we have to think about their own private, really do not understand can ask, check, but in the end, they must realize it again, or forget the will soon ... will continue to update the Min data structure (C language version) of the various algorithm implementation, I am now learning to sell, I hope you can criticize my friends.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + implementation of chained queues