万金流
以码会友。 吾Q:578751655。 水平有限,轻喷,谢!
随笔 - 189,  文章 - 0,  评论 - 7,  阅读 - 14万

如题:

顺序队列,数组实现:

复制代码
public class Array_Queue
{
    int[] a=new int[5];
    int front=0,rear=0,count=0;
    public boolean isEmpty()
    {
        if(front==rear&&count==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public boolean isFull()
    {
        if(front==rear&&count>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void enQueue(int x)
    {
        if(!isFull())
        {
            a[rear]=x;
            rear=(rear+1)%(a.length);
            count++;
        }
        else
        {
            System.out.println("Array_Queue is Full!");
        }
    }
    public int deQueue()
    {
        int t;
        if(!isEmpty())
        {
            t=a[front];
            front=(front+1)%(a.length);
            count--;
        }
        else
        {
            System.out.println("Array_Queue is Empty!");
            t=-1;
        }
        return t;
    }
}
复制代码

 主程序c1:

复制代码
//顺序队
        Array_Queue myaq=new Array_Queue();
        int t;
        for (int i = 1; i <= 6; i++)
        {
            myaq.enQueue(i);
        }
//        //debug1
//        for (int item : myaq.a)
//        {
//            System.out.print("debug1"+item+"\t");
//        }
//        System.out.println("front:"+myaq.front+"rear:"+myaq.rear+"count:"+myaq.count);
//        //debug1
        for (int i = 1; i <= 2; i++)
        {
            t = myaq.deQueue();
            if (t != -1)
            {
                System.out.print(t + "\t");
            }
        }
        for (int i = 7; i <= 8; i++)
        {
            myaq.enQueue(i);
        }
        for (int i = 1; i <= 10; i++)
        {
            t = myaq.deQueue();
            if (t != -1)
            {
                System.out.print(t + "\t");
            }
        }
复制代码

运行结果:

Array_Queue is Full!
1    2    3    4    5    7    8    Array_Queue is Empty!
Array_Queue is Empty!
Array_Queue is Empty!
Array_Queue is Empty!
Array_Queue is Empty!

 链式队列节点代码:

 

 链式队列代码:

复制代码
public class Linked_Queue {
    Linked_Node front,rear;
    public Linked_Queue()
    {
        front=new Linked_Node();
        rear=front;
    }

    public boolean isEmpty()
    {
        if (front.next==null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void enQueue(int x)
    {
            rear.next =new Linked_Node(x);
            rear.next.pre=rear;
            rear = rear.next;

    }

    public int deQueue()
    {
        int t;
        if (!isEmpty())
        {
            t = front.next.v;
            front.next = front.next.next;
            if(front.next!=null)
            {
                front.next.pre=front;
            }
            else
            {
                rear=front;
            }
        }
        else
        {
            System.out.println("Array_Queue is Empty!");
            t = -1;
        }
        return t;
    }
}
复制代码

主程序:

 

 运行结果:

 链表头结点全都有“头”(即木头乌龟)。根据自己的理解和喜好,程序是活的。

posted on   万金流  阅读(197)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?

点击右上角即可分享
微信分享提示