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

        }

    }
}
相关推荐
程序猿编码几秒前
用 C++ 玩转字符级 Transformer 语言模型:从原理到实现
开发语言·c++·深度学习·语言模型·transformer
古城小栈几秒前
Go语言 赋能 区块链 开发
开发语言·golang·区块链
唐青枫几秒前
一次弄懂 C# 内联数组(Inline Array):高性能数组的新选择
c#·.net
coderxiaohan2 分钟前
【C++】AVL树实现
开发语言·c++
架构师专栏3 分钟前
Spring Boot 4 整合46篇教程,Spring Boot 4 企业级项目开发完整实践指南
java·spring boot·后端
专注VB编程开发20年3 分钟前
Activex dll创建调用-Python,Node.js, JAVA主流编程语言操作COM对象
java·开发语言·python·node.js·activex dll
权泽谦6 分钟前
用大语言模型实现一个离线翻译小程序(无网络也能用)
开发语言·人工智能·语言模型·小程序·php
小生凡一9 分钟前
图解|Go的GMP在计算密集型和IO密集型的区别
开发语言·golang
代码or搬砖1 小时前
公共字段抽取自动填充
android·java·数据库
共享家95271 小时前
Qt 实战-Music 播放器
开发语言·qt