Bit crossing, because the queue operation is relatively simple, I say nothing, directly on the code, welcome to verify!!!
#pragma Mark--abstractThe //queue is a linear table that allows insertions at one end of the table, and deletes at the other end of the table, allowing the inserted end to be called the tail of the team (rear)//Allow deletion of one end called the team header (font), the queue without elements is called an empty queue//queue is characterized by first-in, in-out (FIFO linear table)#pragma mark--category//1. The sequential storage structure of a queue is called the sequential queue (sequential), which consists of a one-dimensional array that holds the queue and pointers to the team head and the end of the team. Usually the contract: the tail of the team indicates the current position of the tail element in a one-dimensional array, The team head pointer points to the previous position of the team head element in the current position in a one-dimensional array#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#pragma mark--Definition of sequential queues#if 0#Define maxsize 10//queue possible maximum lengthtypedefintElemtype;typedefstruct{Elemtype elem[maxsize];intFont,rear;} SEQUEUETP;#endif //Assume SA is a variable of SEQUEUETP//Queue Operation//sq.rear=sq.rear+1;//sq.elem[sq.rear]=x;//Out of team Operation//sq.font=sq.font+1//Empty queue Sq.font=sq.rear///Man sq.rear=maxsize, there is a false overflow phenomenon, there is//2. Loop queue///Loop table, imagine the sq.elem[0] after the sq.elem[maxsize-1], using the modulo operation can easily realize the circular meaning of the plus 1 operation//Queue//sq.rear= (sq.rear+1)%maxsize//sq.elem[sq.rear]=x;//OUT Team//sq.font= (sq.font+1)%maxsize//to the empty sq.rear=sq.font;//Team Full (sq.rear+1)%maxsize=sq.font;#pragma Mark-------------the definition of the cyclic queue--------------------#Define maxsize 11//(maximum length of circular queue + 1)typedefintElemtype;typedefstruct{Elemtype elem[maxsize];intFont,rear;} CQUEUETP;#pragma mark--Initialization of the queuevoidInitqueue (CQUEUETP*CQ) {cq->font=0; Cq->rear=0;}#pragma mark determines if the queue is emptyBOOLQueueempty (CQUEUETP*CQ) {return(*CQ). font== (*CQ). Rear?true:false;}#pragma Mark asks for the length of the queueintSize (CQUEUETP*CQ) {//Add maxsize to take into account the cq->rear-cq->font<0 situation return(Maxsize+cq->rear-cq->font)%maxsize;}#pragma mark reads the elements of the team headElemtype Head (CQUEUETP*CQ) {if(Cq->rear==cq->font) {//Circular queue is not empty returnNULL; }Else{returncq->elem[cq->font+1]%maxsize; }}#pragma Mark team OperationvoidEntrayqueue (Cqueuetp*cq,elemtype x) {if((cq->rear+1) {%maxsize==cq->font) {//Team fullprintf"overflow\n"); }Else{cq->rear= (cq->rear+1)%maxsize; cq->elem[cq->rear]=x; }}#pragma mark out of the squad operationElemtype Deletqueue (CQUEUETP*CQ) {if((*CQ). font== (*CQ). Rear) {returnNULL; }Else{cq->font= (cq->font+1)%maxsize;return(Cq->elem[cq->font]); }}#pragma mark------------------chain Queue---------------------as with the sequential storage of stacks, the sequential storage of queues can also overflow, so consider the chain-store structure of the queueThe chain storage structure of the queue becomes the chain queue (linked queue), which is actually a single-linked list with both head and tail pointers.The //head pointer points to the head node, the tail pointer points to the tail node, although the single-linked list can be uniquely determined with the head pointer, but the insert operation is always//At the end of the team, if there is no end-of-line pointer, the time complexity of the queue will have O (1) liters to O (n)//Lq.front=lq.rear The conditions of the empty sentence;#pragma mark--Definition of chain queuetypedefstructnode{elemtype data;structNode*next;;} Nodetype;typedefstruct{NodeType *front; NodeType *rear;} Lqueue;#pragma mark--InitializevoidInitlqueue (LQUEUE*LQ) {//Set an empty queueLq->front= (nodetype*) malloc (sizeof(NodeType)); lq->front->next=null; Lq->rear=lq->front;}#pragma mark--EmptyBOOLEmptylqueue (LQUEUE*LQ) {returnLq->front==lq->rear?true:false;}#pragma mark--captainintLqsize (LQUEUE*LQ) {intI=0; nodetype*p=lq->front->next; while(p) {i++; p=p->next; }returni;}#pragma mark-read the team head element operationElemtype GetHead (LQUEUE*LQ) {if(lq->front==lq->rear) {returnNULL; }Else{returnlq->front->next->data; }}#pragma Mark team OperationvoidEntryqueue (Lqueue*lq,elemtype x) {nodetype*s; S= (nodetype*) malloc (sizeof(NodeType)); s->data=x; s->next=null; lq->rear->next=s; Lq->rear=s;}#pragma mark out of the squad operationElemtype Delqueue (LQUEUE*LQ) {elemtype x; Nodetype*p;if(lq->front==lq->rear) {returnNULL; }Else{p=lq->front->next; lq->front->next=p->next;if(P->next==null)//When there is only one node in the chain queue, change the tail pointer when the team is out .lq->rear=lq->front; x=p->data; Free (p);returnX }}#pragma Mark summary//1> chain queue and chain stack, no need to judge the situation of full stack//2> in the queue, when the original queue has only one node, the node is both the team head and the end of the team, the deletion of this node must modify the tail pointer, and delete the node after the queue becomes empty//3> and chain stack are the same, for the chain queue, generally does not produce a team full, because the queue length is generally larger, so the chain storage structure is more advantageous than the sequential storage structure
Data Structure _ Linear Table _ Sequential Queue _ Loop Queue _ chain queue