当前位置:网站首页>C#实现队列结构定义、入队、出队操作
C#实现队列结构定义、入队、出队操作
2022-06-29 12:05:00 【黄昏和星空】
本文介绍C#实现队列结构定义、入队、出队操作
1、队列结构定义
///
/// 队列结构体定义
///
public struct SqQueue {
public int[] data;//队列存放的元素
public int front, real;//队头和队尾指针
}
2、队列初始化
///
/// 队列初始化
///
///
static void InitQueue(ref SqQueue Q) {
Q.real = Q.front=0;
}
3、判断队列是否为空
///
/// 判断队列是否为空
///
///
///
static bool isEmpty( SqQueue Q) {
if (Q.front == Q.real) { return true; }
else return false;
}
4、入队
///
/// 入队
///
///
///
///
static bool EnQueue(ref SqQueue Q,int x) {
if ((Q.real+1)%MAXSizeQ.front) { return false; }
Q.data[Q.real] = x;
Q.real = (Q.real + 1) % MAXSize;
return true;
}
5、出队
static bool DeQueue(ref SqQueue Q, ref int x) {
if (Q.realQ.front) { return false; }
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MAXSize;
return true;
}
6、实际main函数测试
static void Main(string[] args)
{
SqQueue Q = new SqQueue();
Q.data = new int[MAXSize];
int x = 0;
//初始化队列
InitQueue(ref Q);
while (x!=999) {
Console.WriteLine(“请输入入队元素:”);
x = int.Parse(Console.ReadLine());
if ((Q.real+1)%MAXSize==Q.front) {
Console.WriteLine(“队列已满。”);
break;
}
if (x!=999) EnQueue(ref Q, x);
}
//出队
Console.WriteLine(“出元素如下:”);
while (!isEmpty(Q)) {
DeQueue(ref Q,ref x);
Console.WriteLine(x);
}
Console.ReadLine();
}
边栏推荐
猜你喜欢

How to create new user for ORACLE 19c (CDB & PDB)
![[JUC series] ThreadLocal of synchronization tool class](/img/15/2f8ce68b9e5ee8dab03fb688712935.png)
[JUC series] ThreadLocal of synchronization tool class

How to install oracle19c in Centos8

倍福TwinCAT3 的OPC_UA通信测试案例

Interpolated scatter data

内插散点数据

JVM之方法区

ERP preparation of bill of materials Huaxia

MATLAB求极限

C # indexe l'arbre binaire en traversant l'ordre moyen
随机推荐
C#实现二叉树非递归中序遍历程序
C#通過中序遍曆對二叉樹進行線索化
Gbase8s database into standard and into raw clauses
如何计算win/tai/loss in paired t-test
地球观测卫星数据
Method area of JVM
QQ group was stolen, a large-scale social death scene caught off guard
QQ集体被盗号,猝不及防的大型社死名场面
Recommended model reproduction (II): fine arrangement model deepfm, DIN
C#通过线索二叉树进行中序遍历输出
Gbase8s database select has a having clause
Comment calculer Win / Tai / Loss in paired t - test
[leetcode] 14. Longest public prefix
Cocos star meetings at Hangzhou station in 2022
Gbase8s database into temp clause creates a temporary table to save query results.
oracle 19c : change the user sys/system username pasword under Linux
解决问题:ModuleNotFoundError: No module named ‘pip‘
MySQL 主从复制原理以及流程
MIT linear algebra Chinese Notes
File contained log poisoning (user agent)