In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use the C# queue". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The characteristics of queues are very simple, that is, first-in, first-out, generally implemented using arrays.
To realize the queue, it is necessary to realize several functions: join the team, leave the team, judge the fullness of the team, judge the emptiness of the team, get the head of the team and the end of the line.
The key to implementing the queue lies in the setting of the head and tail pointers of the queue:
Assuming that in the initial state, the head pointer is 0 and the end pointer is-1, then the positions of the two pointers are the head and tail of the line, respectively. The team is empty when the next one at the end of the line is the head of the line, and the team is full when the next one at the end of the line is the head of the team.
Assuming that in the initial state, the head and tail pointers are all 0, then the tail pointer refers to the end of the line, and the next position of the head pointer is the head of the team. The team is empty when the two pointers are equal and full when the next pointer at the end of the line is the head of the line.
If we draw a picture, we will find that no matter how you set the pointer, there must be a space in the array when the queue is full. So the array length should be 1 more than the queue length.
At the same time, for convenience, we will set the queue pointer to circular, that is, the length of the remaining array after each move.
Public class MyCircularQueue {private int front; private int rear; private int [] queue; public MyCircularQueue (int k) {queue=new int [kud1]; front=0; rear=-1;} public bool EnQueue (int value) {if ((rear+2)% queue.Length==front) return false; rear++;rear=rear%queue.Length; queue [rear] = value; return true } public bool DeQueue () {if ((rear+1)% queue.Length==front) return false; front++;front=front%queue.Length; return true;} public int Front () {if (! IsEmpty ()) return queue [front]; else return-1 } public int Rear () {if (! IsEmpty () return queue [rear]; else return-1;} public bool IsEmpty () {if ((rear+1)% queue.Length==front) return true; else return false;} public bool IsFull () {if ((rear+2)% queue.Length==front) return true Else return false;}}
In fact, C# also has a queue library, as shown in the following figure
It should be noted that both Dequeque and ToArray have a return type of object, which may need to be cast
Class Program {static void Main (string [] args) {Queue Q = new Queue (); q.Enqueue ('A'); char ch = (char) q.Dequeue (); Console.WriteLine ("The removed value: {0}", ch);}} "how the C# queue is used" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.