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

        }

    }
}
相关推荐
WineMonk2 分钟前
ArcGIS Pro SDK (七)编辑 13 注解
arcgis·c#·gis·arcgis pro sdk
yukai080087 分钟前
Python一些可能用的到的函数系列130 UCS-Time Brick
开发语言·python
伏颜.7 分钟前
Spring懒加载Bean机制
java·spring
细心的莽夫7 分钟前
集合复习(java)
java·开发语言·笔记·学习·java-ee
卫卫周大胖;10 分钟前
【C++】多态(详解)
开发语言·c++
yours_Gabriel14 分钟前
java基础:面向对象(二)
java·开发语言·笔记·学习
Enaium17 分钟前
Rust入门实战 编写Minecraft启动器#3解析资源配置
java·开发语言·rust
我写代码菜如坤18 分钟前
Unity中遇到“Input Button unload_long_back_btn is not setup”问题
开发语言
虫小宝28 分钟前
在Spring Boot中实现多线程任务调度
java·spring boot·spring
许思王31 分钟前
【Python】组合数据类型:序列,列表,元组,字典,集合
开发语言·人工智能·python