[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
The linear structure here actually refers to the meaning of continuous memory, but it seems more professional to use the word "linear. The previous blog introduced how to handle the symptom structure. Based on this, do we add some attributes to form a new data structure type? The answer is yes. queue is one of them.
The nature of a queue is simple:
(1) The queue has the header and tail
(2) queue pushing data from the end
(3) Data popped up from the queue Header
So how is the queue implemented in the continuous memory?
A) design the queue Data Structure
Typedef struct _ QUEUE_NODE
{
Int * pData;
Int length;
Int head;
Int tail;
Int count;
} QUEUE_NODE;
Typedef struct _ QUEUE_NODE
{
Int * pData;
Int length;
Int head;
Int tail;
Int count;
} QUEUE_NODE; B) apply for queue memory
QUEUE_NODE * alloca_queue (int number)
{
QUEUE_NODE * pQueueNode;
If (0 = number)
Return NULL;
PQueueNode = (QUEUE_NODE *) malloc (sizeof (QUEUE_NODE ));
Assert (NULL! = PQueueNode );
Memset (pQueueNode, 0, sizeof (QUEUE_NODE ));
PQueueNode-> pData = (int *) malloc (sizeof (int) * number );
If (NULL = pQueueNode-> pData ){
Free (pQueueNode );
Return NULL;
}
PQueueNode-> length = number;
Return pQueueNode;
}
QUEUE_NODE * alloca_queue (int number)
{
QUEUE_NODE * pQueueNode;
If (0 = number)
Return NULL;
PQueueNode = (QUEUE_NODE *) malloc (sizeof (QUEUE_NODE ));
Assert (NULL! = PQueueNode );
Memset (pQueueNode, 0, sizeof (QUEUE_NODE ));
PQueueNode-> pData = (int *) malloc (sizeof (int) * number );
If (NULL = pQueueNode-> pData ){
Free (pQueueNode );
Return NULL;
}
PQueueNode-> length = number;
Return pQueueNode;
} C) Release queue memory
STATUS delete_queue (const QUEUE_NODE * pQueueNode)
{
If (NULL = pQueueNode)
Return FALSE;
Assert (NULL! = PQueueNode-> pData );
Free (pQueueNode-> pData );
Free (void *) pQueueNode );
Return TRUE;
}
STATUS delete_queue (const QUEUE_NODE * pQueueNode)
{
If (NULL = pQueueNode)
Return FALSE;
Assert (NULL! = PQueueNode-> pData );
Free (pQueueNode-> pData );
Free (void *) pQueueNode );
Return TRUE;
} D) Push data into the queue
STATUS insert_queue (QUEUE_NODE * pQueueNode, int value)
{
If (NULL = pQueueNode)
Return FALSE;
If (pQueueNode-> length = pQueueNode-> count)
Return FALSE;
PQueueNode-> tail = (pQueueNode-> tail + 1) % pQueueNode-> length;
PQueueNode-> pData [pQueueNode-> tail] = value;
PQueueNode-> count ++;
Return TRUE;
}
STATUS insert_queue (QUEUE_NODE * pQueueNode, int value)
{
If (NULL = pQueueNode)
Return FALSE;
If (pQueueNode-> length = pQueueNode-> count)
Return FALSE;
PQueueNode-> tail = (pQueueNode-> tail + 1) % pQueueNode-> length;
PQueueNode-> pData [pQueueNode-> tail] = value;
PQueueNode-> count ++;
Return TRUE;
} E) Bring the data to the 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;
PQueueNode-> count --;
PQueueNode-> head = (pQueueNode-> head + 1) % pQueueNode-> length;
Return TRUE;
}
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;
PQueueNode-> count --;
PQueueNode-> head = (pQueueNode-> head + 1) % pQueueNode-> length;
Return TRUE;
} F) count the data in the current queue
Int get_total_number (const QUEUE_NODE * pQueueNode)
{
If (NULL = pQueueNode)
Return 0;
Return pQueueNode-> count;
}
Int get_total_number (const QUEUE_NODE * pQueueNode)
{
If (NULL = pQueueNode)
Return 0;
Return pQueueNode-> count;
} G) Check the total length of the queue during initialization.
Int get_total_number (const QUEUE_NODE * pQueueNode)
{
If (NULL = pQueueNode)
Return 0;
Return pQueueNode-> length;
}
Int get_total_number (const QUEUE_NODE * pQueueNode)
{
If (NULL = pQueueNode)
Return 0;
Return pQueueNode-> length;
}