Cyclic queue C language implementation of data structure

Source: Internet
Author: User

A queue is a first-in, first-out structure in which data is out of the queue and the end of the team. In the Linux kernel, the process is dispatched, and the print buffers are useful to the queue.

Queues can be implemented in a variety of ways, using linked lists or arrays. Here, an array is used to implement a simple loop queue. First, create a queue of size 8, which points to the same piece of memory as the queue tail

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/89/CE/wKioL1gdp5_i4Uc1AAAUxpxwJzU955.png "title=" capture. PNG "width=" "height=" 135 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:135px; "alt=" wkiol1gdp5_ I4uc1aaauxpxwjzu955.png "/>

When you insert a data from the end of the queue into the following scenario, there is always a waste of space for easy handling

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/89/CE/wKioL1gdqFvxFBQKAAAUxj-vGmk813.png "title=" Capture 1. PNG "width=" "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:130px; "alt=" Wkiol1gdqfvxfbqkaaauxj-vgmk813.png "/>

Continue inserting the data until the following occurs, indicating that the queue is full,

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/89/CE/wKioL1gdtSzhXUSDAAAUvoN5qcg170.png "title=" Capture 2. PNG "width=" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:132px; "alt=" Wkiol1gdtszhxusdaaauvon5qcg170.png "/>

Next look at the situation of the team, from the queue head out of the team 3 elements,

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/89/CE/wKioL1gdthiCKvcZAAAUr5822YA708.png "title=" Capture 3. PNG "width=" "height=" 127 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:400PX;HEIGHT:127PX; "alt=" Wkiol1gdthickvczaaaur5822ya708.png "/>

After adding three additional elements, the queue becomes full again.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/89/D1/wKiom1gdt9WDO3LWAAAUBi7XxZQ814.png "title=" Capture 4. PNG "width=" "height=" 118 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:118px; "alt=" Wkiom1gdt9wdo3lwaaaubi7xxzq814.png "/>


In this case, if you add an element again, the elements of the queue header will be overwritten, as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/89/CE/wKioL1gduTvgpwNEAAAUSNLFeR0347.png "title=" Capture 5. PNG "width=" "height=" 122 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:122px; "alt=" Wkiol1gdutvgpwneaaausnlfer0347.png "/>


Loop Queue Implementation code:

#include  <stdio.h> #include  <stdlib.h>/*  loop Queue Size */#define  MAX 8      typedef struct queue {int *que_array;int front;//points to the team first Int rear ;//pointing at the end of the queue int valid_cnt;//the number of valid data in Queues     int total;       //Queue Total size} queue_t;typedef enum {    e_none      = 0,    E_NOMEMORY = 1,    E_FAIL      = 2,} error_t;void init_queue (queue_t *que) {que->que_array =   (int *) malloc (sizeof (int)  * max);if  (que->que_array == null)  { printf ("no mem\n"); exit (-e_nomemory);} que->front     = 0;    que->rear       = 0;que->valid_cnt = 0;    que->total      = max;} /*  determines whether the queue is full  */int is_full (Queue_t *que) {return  ((que->rear + 1)  %  que->total)  ==  (Que->front));} /*  determines whether the queue is empty  */int is_empty (queue_t *que) {return  (que->front == que-> rear);} /*  elements enqueued  */void entry_queue (queue_t *que, int data) {if  (Is_full (que))  { printf ("The queue is full, it is possible to overwrite the previous element  \n");que->front =  ((que->front+1)  % que->total) ;} que->que_array[que->rear % que->total] = data;que->rear =  (que-> rear+1)  % que->total;que->valid_cnt++;} /*  elements out of the team  */int del_queue (Queue_t *que) {    int tmp;         if  (Is_empty (que))  {         printf ("Queue is empty  \n"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBsp;-e_fail;    }        tmp = que-> que_array[que->front % que->total];que->front =  (que->front+1)  %  Que->total); que->valid_cnt--;        return tmp;} Int main (int argc, char *argv[]) {    int tem_data,i;queue_t  Que;    init_queue (&que), #if  0for  (i = 0; i < 7;  i++)  {entry_queue (&que, i + 1);}     printf ("All elements in the queue  \n");while  (!is_empty (&que))  {printf ("%d,",  del_queue (&que));} #endif     /* Next, add 10 elements to try */    for  (i = 0; i  < 10; i++)  {entry_queue (&que, i + 1);}     printf ("All elements in the queue  \n");while  (!is_empty (&que))  {printf ("%d,",  del_queue (&que));}     return 0;}

This article is from the "12128867" blog, please be sure to keep this source http://12138867.blog.51cto.com/12128867/1869846

Cyclic queue C language implementation of data structure

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.