In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to realize the circular queue in the C language. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Note:
1. Circular queue is the sequential representation and implementation of the queue. Because it is tail-in-head-out, unlike the sequence stack, the sequence queue needs to be imagined into a ring-shaped space so that it can be inserted from the head space after the tail is full. 2. You can also use array queues, that is, sequential queues that cannot grow dynamically, so that you do not need to take the maximum module each time to form a ring space. Each time a new queue tail element is inserted, the tail pointer increases by 1, and each time the queue head element is deleted, the head pointer increases by 1. 3. The tail pointer appears before the head pointer, which makes it unsuitable for circular queues to be used when the size of the loop queue cannot be predicted. 4. In every pointer increment expression, add a% MAXQUEUE to make each increment within the range.
# include#include#define MAXQUEUE 100typedef struct {int * base; int front; int rear;} SqQueue, * Sqqueue; Sqqueue Creat (Sqqueue Q); void Enqueue (Sqqueue Q, int e); void Dequeue (Sqqueue Q, int * e); void Traverse (Sqqueue Q); int main () {SqQueue q; int e; Sqqueue p = Creat (& Q); Traverse (p); Dequeue (p, & e); Traverse (p); printf ("the number that was deleted is:% d", e); return 0 } Sqqueue Creat (Sqqueue Q) {Sqqueue p = Q; p-> base = (int *) malloc (MAXQUEUE * sizeof (int)); / / different from the chain here, it opens up a whole piece of data space, with base as the base address p-> front = p-> rear = 0; for (int I = 1; I
< 10; i++) Enqueue(p, i); return p;}void Enqueue(Sqqueue q, int e){ if ((q->Rear + 1)% MAXQUEUE = = Q-> front) / / if the next tail pointer is a header pointer, treat it as a full queue (using less space). Otherwise, it will be ambiguous to see that the head pointer is equal to the tail pointer. Exit (1); Q-> front [Q-> rear] = e; Q-> rear = (Q-> rear + 1)% MAXQUEUE;} void Dequeue (Sqqueue Q, int * e) {if (Q-> front = = Q-> rear) exit (1); (* e) = Q-> Base[ Q-> front]; Q-> front = (Q-> front + 1)% MAXQUEUE;} void Traverse (Sqqueue Q) {for (int I = Q-> front; Q-> rear! = I I = (I + 1)% MAXQUEUE) printf ("% d->", Q-> BaseI]; printf ("NULL");} that's all about the article "how to implement circular queues in C". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.