Definition of a queue
only new elements are allowed to be appended at the end of the team, and elements are removed at the first team. is a FIFO model. Queue operations are much less than linked lists, so we can build a queue based on a linked list.
Diagram of the queue
Code
/***********file:linkqueue.hdate:2016/3/28***********/#ifndef _linkqueue_h_#define_linkqueue_h_#include<stdlib.h>typedefintDataType;//define the destination data type of the storetypedefstructnode{//define each of the node typesDataType data; structnode*Next; }node;typedefstruct{//Defining Queuesnode*Front; Node*tail; }linkqueue;voidInitqueue (linkqueue*queue);voidEnQueue (linkqueue*queue,datatype newdata);intDeQueue (linkqueue* queue,datatype*de);intQueuelength (Constlinkqueue*queue);intQueueisempty (Constlinkqueue*queue);#endif
/************file:linkqueue.cppdate:2016/3/28*************/#include"LinkQueue.h"//Initialize the queue, the head pointer, and the pointer are all emptyvoidInitqueue (linkqueue*queue) {Queue->front =NULL; Queue->tail =NULL; }//Join the queue and append an element to the tail of the list. //pay special attention to the first element in the queuevoidEnQueue (linkqueue*Queue,datatype NewData) {Node*p_new; P_new= (node*)malloc(sizeof(Node)); if(P_new==null) Exit (-1); P_new->data =NewData; P_new->next =NULL; if(Queueisempty (queue))//the first element of the queue is a case{Queue->front = Queue->tail =p_new; } Else{//section 2,3,4 the queue of elementsQueue->tail->next = p_new;//linksQueue->tail = p_new;//Reset Tail node } }//out of the team, delete the first element of the list//pay special attention to the case where the last element was deletedintDeQueue (linkqueue* queue,datatype*de) {Node*t = queue->front;//gets the address of the team head node is T if(Queueisempty (queue))//The queue is empty, no more teams. return 0; Queue->front = t->next;//the head node address of the queue is moved back if(queue->front==NULL) Queue->tail = NULL;//if the team head pointer is empty, this time the team is empty. if(De!=null)//to save the deleted element back*de = t->data; Free(t); return 1; }//the length of the queueintQueuelength (Constlinkqueue*queue) { intCount=0; Node*p = queue->Front; while(p!=NULL) {P= p->Next; Count++; } returncount;}//whether the queue is emptyintQueueisempty (Constlinkqueue*queue) { if(Queue->front = = NULL && Queue->tail = =NULL)return 1; return 0; }
The implementation of chain-type queue