创建队列链表(C#、java)

文章目录

    • 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);
            }

        }

    }
}
相关推荐
IOT-Power24 分钟前
QT构建构建DataBus总线
开发语言·qt
Ophelia(秃头版35 分钟前
JS事件循环与NodeJS事件循环(libuv)
开发语言·javascript
米饭的白色1 小时前
matlab 中 `对数坐标` 画图下的 `hold on` 位置对坐标轴刻度的影响
开发语言·matlab
gjxDaniel1 小时前
Go编程语言入门与常见问题
开发语言·后端·go
计算机程序猿学长1 小时前
微信小程序毕设项目推荐-基于java+springboot+mysql+微信小程序的校园外卖点餐平台基于springboot+微信小程序的校园外卖直送平台【附源码+文档,调试定制服务】
java·微信小程序·课程设计
建群新人小猿1 小时前
陀螺匠企业助手——组织框架图
android·java·大数据·开发语言·容器
CV_J1 小时前
索引库操作
java·开发语言·elasticsearch·spring cloud
阿蒙Amon1 小时前
C#每日面试题-简述异常处理
开发语言·c#
申克Lab1 小时前
STM32 FreeRTOS 消息队列
java·stm32·嵌入式硬件
敲敲千反田2 小时前
多线程复习
java·开发语言