文章目录
-
- 1、创建队列
- 2、队列节点
- 3、程序入口
1、创建队列
cs
namespace Testmain
{
public class StackQueue
{
QNode Front = new QNode(-1);
QNode Rear = new QNode(-1);
QNode curNode;
public StackQueue(int data)
{
curNode = new QNode(data);
if (Front.next == null)
{
Front.next = curNode;
Rear = curNode;
}
}
public StackQueue()
{
}
public void enQueue(int data)
{
curNode = new QNode(data);
if (Front.next == null)
{
Front.next = curNode;
Rear = curNode;
}
else
{
Rear.next = curNode;
Rear = curNode;
}
}
public int deQueue()
{
if (Front.next == Rear)
{
Front.next = Rear.next;
Rear.Data = -1;
Rear = Front;
return -1;
}
else
{
int data = Front.next.Data;
Front.next = Front.next.next;
return data;
}
}
}
}
2、队列节点
cs
namespace Testmain
{
public class QNode
{
public int Data;
public QNode? next;
public QNode(int data)
{
this.Data = data;
next = null;
}
}
}
3、程序入口
cs
using System;
namespace Testmain
{
class Program
{
static void Main(string[] args)
{
try
{
StackQueue stackQueue = new StackQueue(1);
stackQueue.enQueue(2);
stackQueue.enQueue(3);
stackQueue.enQueue(4);
stackQueue.enQueue(5);
Console.WriteLine(stackQueue.deQueue());
Console.WriteLine(stackQueue.deQueue());
Console.WriteLine(stackQueue.deQueue());
Console.WriteLine(stackQueue.deQueue());
Console.WriteLine(stackQueue.deQueue());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}