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

        }

    }
}
相关推荐
bLEd RING8 分钟前
SpringBoot3.3.0集成Knife4j4.5.0实战
java
小松加哲26 分钟前
Spring MVC 核心原理全解析
java·spring·mvc
Ulyanov1 小时前
《PySide6 GUI开发指南:QML核心与实践》 第二篇:QML语法精要——构建声明式UI的基础
java·开发语言·javascript·python·ui·gui·雷达电子对抗系统仿真
码界筑梦坊1 小时前
357-基于Java的大型商场应急预案管理系统
java·开发语言·毕业设计·知识分享
anzhxu1 小时前
Go基础之环境搭建
开发语言·后端·golang
云烟成雨TD1 小时前
Spring AI Alibaba 1.x 系列【31】集成 Studio 模块实现可视化 Agent 调试
java·人工智能·spring
014-code1 小时前
Spring Data JPA 实战指南
java·spring
安小牛1 小时前
Android 开发汉字转带声调的拼音
android·java·学习·android studio
聚美智数1 小时前
企业实际控制人查询-公司实控人查询
android·java·javascript
zb200641201 小时前
SpringBoot详解
java·spring boot·后端