In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how C++ realizes circular sequence queue". In daily operation, I believe many people have doubts about how C++ realizes circular sequence queue. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how C++ can achieve circular sequence queue". Next, please follow the editor to study!
Data structure-implementing circular sequential queues with C++
Operational characteristics of queues: first in, first out
The elements in the queue have the same type
Adjacent elements have precursors and successors
Set two pointers at the beginning and end of the team to improve the time performance of leaving the team.
Convention: the header pointer front points to the previous position of the header element, and the tail pointer rear points to the tail element
In order to solve the false overflow, we connect the array that stores the queue from beginning to end, resulting in a circular queue.
How to judge that the round robin queue is empty?
Team empty: front=rear
How to pile up the disk to the circular queue?
Team full: front=rear
Then the problem arises. The judgment conditions of empty team and full team are the same. In order to avoid the judgment of empty team when the team is full or vice versa, we need to modify the condition of full team to separate the condition of empty team and full team.
Method: waste one element space, and the array has only one free unit when the queue is full. Team full condition: (rear+1)% QueueSize==front
Here is the implementation code:
File CirQueue.h
# ifndef CirQueue_byNim#define CirQueue_byNim#includeusing namespace std;const int QueueSize=100; / / maximum storage space of circular queue template class CirQueue {private: t * data; / / array int front,rear; / / queue head and tail pointer public: CirQueue () {data=new T [QueueSize]; front=rear=0;} ~ CirQueue () {delete [] data; front=rear=0 } void EnQueue (T e) {if ((rear+1)% QueueSize==front) / / queue full conditional throw "overflow"; rear= (rear+1)% QueueSize; data [rear] = e;} T DeQueue () {if (rear==front) / / queue empty condition throw "underflow"; front= (front+1)% QueueSize; return data [front] } T GetQueue () {if (rear==front) / / team null condition throw "underflow"; return data [(front+1)% QueueSize];} bool empty () {if (front==rear) / / team null condition: front==rear return true; return false;}} # endif at this point, the study on "how to implement circular sequential queues in C++" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.