In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the use of C++ Squadron, the article is very detailed, has a certain reference value, interested friends must read it!
1. The concept and structure of queues
Queue: a special linear table that only allows inserting data at one end and deleting data at the other. The queue has first-in, first-out FIFO (First In First Out) into the queue: the end of the insert operation is called the end of the queue: the end of the delete operation is called the head of the queue.
two。 Implementation of queue 2.1 queue.h#include#include#include#includetypedef int QDataType;typedef struct QueueNode {struct QueueNode*next; QDataType data;} QueueNode;typedef struct Queue {QueueNode* head; QueueNode* tail;} Queue;void QueueInit (Queue * pq); void QueueDestory (Queue * pq); void QueuePush (Queue * pq,QDataType x); void QueuePop (Queue * pq); QDataType QueueFront (Queue * pq); QDataType QueueBack (Queue * pq); bool QueueEmpty (Queue * pq) Int QueueSize (Queue * pq); 2.2 queue.c#include "queue.h" void QueueInit (Queue * pq) {assert (pq); pq- > head = pq- > tail = NULL;} void QueueDestory (Queue * pq) {assert (pq); QueueNode * cur = pq- > head; while (cur) {QueueNode * next = cur- > next; free (cur) Cur = next;} pq- > head = pq- > tail = NULL;} void QueuePush (Queue * pq, QDataType x) {assert (pq); QueueNode* newnode = (QueueNode*) malloc (sizeof (QueueNode)); if (newnode = = NULL) {printf ("malloc fail\ n"); exit (- 1) } newnode- > data = x; newnode- > next = NULL; if (pq- > tail = = NULL) {pq- > head = pq- > tail = newnode;} else {pq- > tail- > next = newnode; pq- > tail = newnode;}} void QueuePop (Queue * pq) {assert (pq) Assert (! QueueEmpty (pq)); if (pq- > head- > next = = NULL) {free (pq- > head); pq- > head = pq- > tail = NULL;} else {QueueNode * next = pq- > head- > next; free (pq- > head); pq- > head = next } QDataType QueueFront (Queue * pq) {assert (pq); assert (! QueueEmpty (pq)); return pq- > head- > data;} QDataType QueueBack (Queue * pq) {assert (pq); assert (! QueueEmpty (pq)); return pq- > tail- > data;} bool QueueEmpty (Queue * pq) {assert (pq); return pq- > head = = NULL } int QueueSize (Queue * pq) {int size = 0; QueueNode * cur = pq- > head; while (cur) {QueueNode * next = cur- > next; + + size; cur = cur- > next;} return size;} 2.3test.c#include "queue.h" void TestOne () {Queue q; QueueInit (& Q) QueuePush (& Q, 1); QueuePush (& Q, 2); QueuePush (& Q, 3); QueuePush (& Q, 4); while (! QueueEmpty (& Q)) {printf ("% d", QueueFront (& Q)); QueuePop (& Q);} printf ("\ n"); QueueDestory (& Q) } int main () {TestOne (); return 0;} these are all the contents of this article entitled "what is the use of the C++ Squadron 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.