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

What's the use of C++ Squadron?

2025-01-16 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.

Share To

Development

Wechat

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

12
Report