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

        }

    }
}
相关推荐
starzy19907 小时前
深入理解Scala隐式类:扩展方法机制
开发语言·人工智能·scala
Pniubi7 小时前
怎么排查慢SQL,怎么解决?
java·数据库·sql
M--Y8 小时前
LangGraph 工作流模式
开发语言·javascript·python·langchain·大模型·llm·langgraph
薛之谦_8 小时前
Java 全栈知识体系:从 IDEA 高效开发到 JVM、并发与 Stream 实战
java·jvm·intellij-idea
吴可可1239 小时前
WScript Shell操作注册表详解
开发语言
纪念 22910 小时前
C++ 算法基础 (差分)
开发语言·c++·算法
文丑颜不良啊12 小时前
LeetCode2761~2780题目解析
java·leetcode
IT枫斗者枫哥13 小时前
子线程断言失败,JUnit 为什么还显示通过?
java·后端
码农小河6614 小时前
基于PHP的电商后台蜜罐系统
开发语言·php
starzy199014 小时前
深入理解Scala柯里化函数:函数链式变换
开发语言·scala