Summary queue feature: FIFO--advanced queue element first out queue. It comes from the queues in our lives (the first line is done).
There is a flaw, and the space utilization is not high, so we directly learn the loop queue (based on continuous memory).
(1) Design queue data structure
struct _QUEUE_NODE{ int* pData; intlength;//队列长度 int head ;//队头指针 int tail;//队尾指针 int count;//队列元素当前个数}QUEUE_NODE;
(2) Request queue memory
queue_node* Alloca_queue (intNumber) {queue_node* pqueuenode;if(0= = number)return NULL; Pqueuenode = (queue_node*) malloc (sizeof(Queue_node)); AssertNULL! = Pqueuenode); memset (Pqueuenode,0,sizeof(Queue_node)); Pqueuenode->pdata = (int*) malloc (sizeof(int) * number);if(NULL= = Pqueuenode->pdata) {free (pqueuenode); Pqueuenode =NULL;return NULL; } pqueuenode->length = number;//Queue Length returnPqueuenode;}
(3) releasing the queue memory
STATUS delete_queue(const QUEUE_NODE* pQueueNode){ if(NULL== pQueueNode) returnFALSE; assert(NULL!= pQueueNode->pData); free(pQueueNode->pData); free((void*)pQueueNode); returnTRUE;}
(4) Press the data into the queue
int value){ if(NULL == pQueueNode) returnFALSE; if(pQueueNode->length == pQueueNode->count)//判断是不是溢出 returnFALSE; pQueueNode->pData[pQueueNode->tail] = value; 1) % pQueueNode->length; //队尾指针位置 pQueueNode->count ++; returnTRUE;}
(5) Eject the data queue
STATUS Get_queue_data (queue_node* Pqueuenode,int* value) {if(NULL= = Pqueuenode | |NULL= = value)return FALSE;if(0= = Pqueuenode->count)return FALSE; *value = pqueuenode->pdata[pqueuenode->head]; pqueuenode-> Pdata[pqueuenode->head] =0;//The location assigned to the popup is 0pqueuenode-> Count--; Pqueuenode->head = (Pqueuenode->head +1)% pqueuenode->length;//Reposition the position of the team head pointer return TRUE;}
(6) Count how much data is in the current queue
int get_total_number(const QUEUE_NODE* pQueueNode){ if(NULL == pQueueNode) return0; return pQueueNode->count;}
(7) How long is the total length of the view when initializing in the queue?
int get_total_number(const QUEUE_NODE* pQueueNode){ if(NULL == pQueueNode) return0; return pQueueNode->length;}
Cyclic queue of data structure learning (sequential storage)