创建队列链表(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);
            }

        }

    }
}
相关推荐
♡喜欢做梦11 分钟前
MyBatis XML 配置文件:从配置规范到 CRUD 开发实践
xml·java·java-ee·mybatis
爱吃烤鸡翅的酸菜鱼13 分钟前
Spring Boot 实现 WebSocket 实时通信:从原理到生产级实战
java·开发语言·spring boot·后端·websocket·spring
J不A秃V头A15 分钟前
Maven的分发管理与依赖拉取
java·maven
雪域迷影18 分钟前
C++中编写UT单元测试用例时如何mock非虚函数?
开发语言·c++·测试用例·gmock·cpp-stub开源项目
AI街潜水的八角1 小时前
Python电脑屏幕&摄像头录制软件(提供源代码)
开发语言·python
hadage2331 小时前
--- git 的一些使用 ---
开发语言·git·python
lly2024063 小时前
HTML与CSS:构建网页的基石
开发语言
一只会写代码的猫3 小时前
面向高性能计算与网络服务的C++微内核架构设计与多线程优化实践探索与经验分享
java·开发语言·jvm
萤丰信息4 小时前
智慧园区能源革命:从“耗电黑洞”到零碳样本的蜕变
java·大数据·人工智能·科技·安全·能源·智慧园区
曹牧5 小时前
Eclipse为方法添加注释
java·ide·eclipse