Cyclic queue of data structure learning (sequential storage)

Source: Internet
Author: User

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)

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.