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

How to realize the data structure and queue of C #

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to realize the data structure and queue of C#". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn "how to realize the data structure and queue of C#".

The queue of C # data structure and algorithm is a special linear table, which only allows deletion at the front end of the table (front) and insert operation at the back end of the table (back). The end of the insert operation is called the end of the queue, and the end of the delete operation is called the head of the line. This is the first-in, first-out (FIFO) rule that we often talk about. Queue rule was applied in China a long time ago. For example, granary management officials did not master this rule. Before they mastered this rule, the grain at the bottom of the warehouse went bad for too long. Later, a smart person opened a door on the second side of the granary and entered the warehouse while leaving the warehouse, so the management was much more convenient. When there are no elements in the queue, it is called an empty queue.

The interfaces of the queue implementation of C# data structure and algorithm are as follows:

Public interface IQueen < T > {int Length (); bool IsEmpty (); bool IsFull (); void Clear (); void IN (T items); T Out (); T GetFrontItem ();}

The principle and code of the queue implementation of C# data structure and algorithm are as follows:

Public class JQueen < T >: IQueen < T > {private int size; private T [] item; private int front; private int back; public JQueen (): this (100) {size = 100; item = new T [100]; front = back =-1;} public JQueen (int length) {size = length Item = new T [length]; front = back =-1;} public T this [int index] {get {return item [index];} set {item [index] = value;}} public int Front {get {return front;} set {front = value }} public int Back {get {return back;} set {back = value;}} public int MaxLength {get {return size;} set {size = value;}} public int Length () {return (back-front + size)% size } public bool IsEmpty () {return (front = = back);} public bool IsFull () {return ((back + 1)% size = = front);} public void Clear () {front = back =-1 } public void IN (T items) {if (IsFull ()) {throw new ArgumentOutOfRangeException ("RangeException", "Queen RangeException: queen is full");} item[ + + back] = items;} public T Out () {T tmp = default (T) If (IsEmpty ()) {throw new ArgumentOutOfRangeException ("RangeException", "Queen RangeException: queen is empty");} tmp = front [+ + front]; return tmp } public T GetFrontItem () {if (IsEmpty ()) {throw new ArgumentOutOfRangeException ("RangeException", "Queen RangeException: queen is empty");} return item [front + 1];}}

Test queue code for the queue of C# data structures and algorithms:

Public class Program {static void Main (string [] args) {try {JQueen < string > JQ = new JQueen < string > (); Console.WriteLine (JQ.IsEmpty ()); / / empty Console.WriteLine (JQ.IsFull ()); / / full Console.WriteLine (JQ.MaxLength) / / queue length Console.WriteLine (JQ.Length ()) at initialization; / / queue element length Console.WriteLine (JQ.Front); / / queue head position Console.WriteLine (JQ.Back); / / queue tail position JQ.IN ("A"); / / insert element JQ.IN ("B") JQ.IN ("C"); JQ.IN ("D"); Console.WriteLine (JQ.GetFrontItem ()); / / queue leader element Console.WriteLine ("- element after leaving the queue -"); JQ.Out (); / / A JQ.Out () Console.WriteLine (JQ.GetFrontItem ()); / / queue header element Console.ReadLine ();} catch (Exception ex) {Console.WriteLine (ex.Message); / / exception Console.ReadLine ();}

The running result of the queue program of C# data structure and algorithm is as follows:

Thank you for your reading. the above is the content of "how to realize the data structure and queue of C#". After the study of this article, I believe you have a deeper understanding of how to realize the data structure and queue of C#. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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