Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use c language to realize circular queue

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use c language to achieve circular queue, the article is very detailed, has a certain reference value, interested friends must read it!

A queue is a first-in, first-out structure, with data coming out from the beginning of the queue and coming in at the end of the queue. In the linux kernel, process scheduling and print buffers are all useful to queues.

Queues can be implemented in a variety of ways, from linked lists to arrays. Here, an array is used to implement a simple circular queue. First, create a queue with a size of 8 as follows, with the header and tail pointers pointing to the same block of memory

When a data is inserted from the end of the queue into the following situation, a space is always wasted for ease of processing.

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

Next, let's take a look at the situation of coming out of the team. There are three elements coming out of the team at the head of the queue.

After adding three more elements, the queue becomes full again

In the above case, adding more elements will overwrite the elements in the queue header, as follows:

Loop queue implementation code:

# include # include / * Circular queue size * / # define MAX 8 typedef struct queue {int * que_array; int front; / / pointing to the head of the queue int rear; / / pointing to the number of valid data in the int valid_cnt; / / queue at the end of the queue int total; / / Total queue 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;} / * determine whether the queue is full * / int is_full (queue_t * que) {return ((que- > rear + 1)% que- > total) = (que- > front)) } / * determine whether the queue is empty * / int is_empty (queue_t * que) {return (que- > front = = que- > rear);} / * elements join the queue * / void entry_queue (queue_t * que, int data) {if (is_full (que)) {printf ("queue is full, adding may 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++;} / * element dequeue * / int del_queue (queue_t * que) {int tmp If (is_empty (que)) {printf ("queue is empty"); return-eBay 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 0 for (I = 0; I < 7; iqueue +) {entry_queue (& que, I + 1);} printf ("all elements in queue\ n"); while (! is_empty (& que)) {printf ("% d," del_queue (& que)) } # endif / * next, try adding ten elements * / for (I = 0; I < 10; ielements +) {entry_queue (& que, I + 1);} printf ("all elements in queue\ n") While (! is_empty (& que)) {printf ("% d,", del_queue (& que));} return 0;} above is all the content of the article "how to use c language to implement a circular queue". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report