Table of contents
3. Determine whether the element exists
4. Get the length of the queue
I. Overview
Represents a first-in, first-out collection of objects.
Queue, like other data structures, is a storage container. It follows the principle of first-in-first-out and can store any type, but it cannot get to the specified location. It can only be stored and taken out. After taking out the elements, the queue inside Elements are automatically deleted. In fact, the usage of queues and stacks is roughly the same, but the order of fetching data is different.
Official documentation reference: Queue class (System.Collections) | Microsoft Learn
2. Basic usage
1. Add elements
Add elements using the Enqueue method
using System;
using System.Collections;
using System.Collections.Generic;
namespace Queue_Test
{
internal class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue("你好");
queue.Enqueue(true);
queue.Enqueue(6.5);
Console.ReadKey();
}
}
}
2. Take out elements
1) Dequeue method
After the element is taken out, the element will be automatically deleted from the Queue
using System;
using System.Collections;
using System.Collections.Generic;
namespace Queue_Test
{
internal class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue("你好");
queue.Enqueue(true);
queue.Enqueue(6.5);
var value = queue.Dequeue();
Console.WriteLine("value:" + value);
Console.WriteLine("长度:" + queue.Count);
Console.ReadKey();
}
}
}
run
From the execution results, we can see that the one stored first is taken out first.
2) Peek method
Read the first element added, after reading, the element will not be deleted from the Queue, but only one element can be read
using System;
using System.Collections;
using System.Collections.Generic;
namespace Queue_Test
{
internal class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue("你好");
queue.Enqueue(true);
queue.Enqueue(6.5);
var value = queue.Peek();
Console.WriteLine("value:" + value);
Console.WriteLine("长度:" + queue.Count);
Console.ReadKey();
}
}
}
run
3. Determine whether the element exists
Use Contains to determine whether an element exists
using System;
using System.Collections;
using System.Collections.Generic;
namespace Queue_Test
{
internal class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue("你好");
queue.Enqueue(true);
queue.Enqueue(6.5);
if(queue.Contains(1))
Console.WriteLine("存在");
else
Console.WriteLine("不存在");
Console.ReadKey();
}
}
}
run
4. Get the length of the queue
The acquisition of the length is the same as List, using the Count property
using System;
using System.Collections;
using System.Collections.Generic;
namespace Queue_Test
{
internal class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue("你好");
queue.Enqueue(true);
queue.Enqueue(6.5);
Console.WriteLine("长度:" + queue.Count);
Console.ReadKey();
}
}
}
run
5. Traverse the queue
The queue can be traversed using foreach, which does not remove elements
using System;
using System.Collections;
using System.Collections.Generic;
namespace Queue_Test
{
internal class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue("你好");
queue.Enqueue(true);
queue.Enqueue(6.5);
foreach (var item in queue)
{
Console.WriteLine(item);
}
Console.WriteLine("长度:" + queue.Count);
Console.ReadKey();
}
}
}
run
6. Empty the container
Use the Clear method to clear the queue
using System;
using System.Collections;
using System.Collections.Generic;
namespace Queue_Test
{
internal class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue("你好");
queue.Enqueue(true);
queue.Enqueue(6.5);
Console.WriteLine("长度:" + queue.Count);
queue.Clear();
Console.WriteLine("长度:" + queue.Count);
Console.ReadKey();
}
}
}
run
7. Queue generic class
Queue, like stack, has a generic class with the same function. It is used to store data of fixed type. When using it, it needs to add the specified type. When using these data, it is not necessary to specify each data Without type detection, it is more standardized to use.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Queue_Test
{
internal class Program
{
static void Main(string[] args)
{
Queue<string> queue = new Queue<string>();
//向队列中添加元素
queue.Enqueue("老一");
queue.Enqueue("老二");
queue.Enqueue("老三");
//获取队列的数量
int count = queue.Count;
//队列中是否包含指定的 value
bool b = queue.Contains("老王");
//Peek方法是返回顶部的对象而不将其从堆栈中移除
string names = queue.Peek();
//获取队列中的元素
//每次调用 Dequeue 方法,获取并移除队列中队首的元素
string s1 = queue.Dequeue();
Console.WriteLine(s1);
string s2 = queue.Dequeue();
Console.WriteLine(s2);
string s3 = queue.Dequeue();
Console.WriteLine(s3);
//清空队列
queue.Clear();
Console.ReadKey();
}
}
}
3. End
The characteristics and usage methods of queue and stack are roughly the same. The main features are: first in, first out, boxing and unboxing, storage of any type, unable to get the element at the specified location, only the first stored element can be taken out.
end