用while循环执行选择操作

首先定义一个接口。

代码如下:

public interface IQueue {
    public void clear();
    public boolean isEmpty();
    public int length();
    public Object peek();
    public void offer(Object x)throws Exception;
    public Object poll();
}

接下来用CircleSqQueue类去实现接口。

实现代码如下:

public class CircleSqQueue implements IQueue{
    private Object[] queueElem;
    private int front;
    private int rear;

    public CircleSqQueue(int maxSize){
        front = rear = 0;
        queueElem=new Object[maxSize];
    }

    public void clear(){
        front = rear = 0;
    }

    public boolean isEmpty(){
        if (front == rear){
            return true;
        }
        else if(front==(rear+1)%queueElem.length){
            return false;
        }
        return false;
    }

    public int length(){
        return (rear-front+queueElem.length)%queueElem.length;
    }

    public Object peek(){
        if (front==rear){
            return null;
        }else {
            return queueElem[front];
        }
    }

    public void offer(Object x)throws Exception{
        if ((rear+1)%queueElem.length==front)
            throw new Exception("队列已满!");
        else {
            queueElem[rear]=x;
            rear=(rear+1)%queueElem.length;
        }

    }

    public Object poll(){
        if (front==rear)
            return null;
        else {
            Object t=queueElem[front];
            front=(front+1)%queueElem.length;
            return t;
        }
    }

    public void display(){
        if (!isEmpty()){
            for (int i=front;i!=rear;i=(i+1)%queueElem.length)
                System.out.println(queueElem[i].toString()+" ");
        }else {
            System.out.println("此队列为空!");
        }
    }

    public void text()throws Exception{
        CircleSqQueue q=new CircleSqQueue(0);
        CircleSqQueue q1=new CircleSqQueue(0);
        CircleSqQueue q2=new CircleSqQueue(0);
        int j=0;
        int i=0;
        while(j<10){
            if (!q1.isEmpty() && i<4){
                q.offer(q1.poll());
                i++;
                j++;
            }else if(i==4 && !q2.isEmpty()){
                q.offer(q2.poll());
                j++;
                i=0;
            }else {
                while (j<10 && i<5 && !q2.isEmpty()){
                    q.offer(q2.poll());
                    i++;
                    j++;
                }
                i=0;
            }if (i==10){
                q.display();
                q.clear();
                j=0;
            }
            if (q1.isEmpty() && q2.isEmpty()){
                j=10;
                q.display();
            }
        }
    }
}

 最后编写测试类函数。

代码如下:

import java.util.*;
public class text {
    public static void main(String[] args) {
        CircleSqQueue c1=new CircleSqQueue(5);
        Scanner sc=new Scanner(System.in);
        Scanner sc1=new Scanner(System.in);
        while(true) {
            System.out.println("----------------------------------------------------");
            System.out.println("请选择下列数字选择相应的操作:");
            System.out.println("1、求队循环队列的长度");
            System.out.println("2、判断循环队列是否为空");
            System.out.println("3、清空循环队列");
            System.out.println("4、取循环你队列队首元素");
            System.out.println("5、入队操作");
            System.out.println("6、出队操作");
            System.out.println("7、显示所有循环队列元素");
            System.out.println("8、退出");
            System.out.println("----------------------------------------------------");
            System.out.println("                                  20级软工X班    张三");
            int s = sc.nextInt();
            label:
            switch (s){
                case 1:
                    System.out.println("队列的长度为:"+c1.length());
                    break;
                case 2:
                    boolean temp=c1.isEmpty();
                    if (temp){
                        System.out.println("该队列为空!");
                    }else {
                        System.out.println("该队列为非空!");
                    }
                    break;
                case 3:
                    c1.clear();
                    System.out.println("清空操作成功!");
                    break;
                case 4:
                    System.out.println("队列首队列的元素为:"+c1.peek());
                    break;
                case 5:
                    System.out.println("请输入要入队的元素:");
                    int s1=sc1.nextInt();
                    Object o=(Object)s1;
                    try {
                        c1.offer(o);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("入队操作成功!");
                    break;
                case 6:
                    c1.poll();
                    System.out.println("出队操作成功!");
                    break;
                case 7:
                    System.out.println("循环队列元素为:");
                    c1.display();
                    break;
                case 8:
                    System.out.println("退出成功");
                    try {
                        c1.text();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                {
                    return;
                }
            }
        }

    }
}

 运行结果

 

 在下面输入想要执行的操作即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值