One-step write algorithm (linear Queue)

Source: Internet
Author: User

 

[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;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.