队列-数组实现:
using System;
namespace DS_ALGCode
{
/// <summary>
/// 队列-数组实现
/// </summary>
public class S_Queue
{
private int Rear = -1;
public int MaxSize;
public int[] Queue;
public S_Queue(int size)
{
MaxSize = size;
Queue = new int[MaxSize];
}
public bool IsFull { get => Rear == MaxSize - 1; }
public bool IsEmpty { get => Rear == -1; }
//入队
public void Push(int data)
{
if (IsFull)
{
Console.WriteLine("队列满");
return;
}
Rear++;
Queue[Rear] = data;
}
//出队
public int Pop()
{
if (IsEmpty)
{
Console.WriteLine("队列空");
return 0;
}
int data = Queue[0];
for (int i = 1; i <= Rear; i++)//重置队列
{
Queue[i - 1] = Queue[i];
}
Rear--;
return data;
}
//列队
public void Show()
{
if (IsEmpty)
{
Console.WriteLine("队列空");
return;
}
for (int i = 0; i <= Rear; i++)
{
Console.WriteLine($"Queue[{i}] = {Queue[i]}");
}
}
}
}
队列-链表实现:
namespace DS_ALGCode
{
/// <summary>
/// 链表节点
/// </summary>
public class Node
{
public int Data { get; set; }
public Node Next { get; set; }
public Node(int data)
{
Data = data;
Next = null;
}
}
}
using System;
namespace DS_ALGCode
{
/// <summary>
/// 队列-链表实现
/// </summary>
public class S_QueueByLink
{
private Node Front = null;
private Node Rear = null;
private int Size = 0;
public bool IsEmpty { get => Size == 0; }
//入队
public void Push(int data)
{
Node node = new Node(data);
if (IsEmpty)
{
Front = node;
}
else
{
Rear.Next = node;
}
Rear = node;
Size++;
}
//出队
public int Pop()
{
if (IsEmpty)
{
Console.WriteLine("队列空");
return 0;
}
int data = Front.Data;
Front = Front.Next;
Size--;
return data;
}
//列队
public void Show()
{
if (IsEmpty)
{
Console.WriteLine("队列空");
return;
}
Node curNode = Front;
while (curNode != null)
{
Console.WriteLine(curNode.Data);
curNode = curNode.Next;
}
}
}
}