欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

模拟队列

程序员文章站 2024-03-18 11:13:04
...

数组模拟队列1

/**
 * @author cjj_1
 * @date 2020-07-31 15:16
 */
public class Queue
{
    public static void main(String[] args) throws Exception {
        QueueArray qa = new QueueArray(3);
        Scanner s = new Scanner(System.in);
        boolean loop = true;
        char key ;
        while (loop)
        {
            System.out.println("s(show):代表展示队列");
            System.out.println("a(add):队列添加");
            System.out.println("g(get):取出数据");
            System.out.println("f(print):获取头");
            System.out.println("e(exit):程序退出!");
            key = s.next().charAt(0);
            switch (key){
                case 's':
                    qa.showQueue();
                    break;
                case 'a':
                    qa.addQueue(s.nextInt());
                    break;
                case 'g':
                    try {
                        qa.fetchQueue();//出队
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }

                    break;
                case 'f':
                    System.out.println(qa.fetchHead());
                    break;
                case 'e':
                    s.close();
                    loop=false;
                    break;
                default:break;
            }
        }

    }
    @Test
    public void queue4Array() {

    }
    /*
     * 内部类
     * @author cjj_1
     * @date 2020-07-31 15:19:40
     * @param null
     * @return
     **/
   static class QueueArray{
        int maxLength ;//对列的最大长度
        int front ;//队列头
        int rear;//队列尾部
        int[] arr;
        public QueueArray(int arrayMaxSize){
            maxLength = arrayMaxSize;
            arr = new int[maxLength];
            rear = -1;
            front = -1;
        }

        /**
         * 判断队列是否满了
         * @return
         */
        public boolean ifFull(){
            return maxLength == rear+1;
        }

        /**
         * 判断队列是否为空
         * @return
         */
        public boolean isEmpty(){
            return rear == front;
        }
        /**
         * 判断队列是否为空
         * @return
         */
        public void addQueue(int n) throws Exception {
            if(ifFull()){
                System.out.println("队列已经满。。。");
               return;
            }
            rear++;
            arr[rear] = n;
        }
    /*
     * 取数据
     * @author cjj_1
     * @date 2020-07-31 16:23:29
     * @param n
     * @return
     **/
        public int fetchQueue() throws Exception {
            if(isEmpty()){
                System.out.println("队列已空。。。");
                throw new Exception("队列已空。。。");
            }
            return front++;

        }

        /**
         * 展示队列
         * @throws Exception
         */
        public void showQueue() throws Exception {
            if(isEmpty())
                return;
           for(int i= front+1;i <=rear;i++)
                System.out.println(arr[i]);
//            front++;
        }
        /**
         * 取头部信息
         * @throws Exception
         */
        public int fetchHead() throws Exception {
            if(isEmpty())
                return -1;
            return arr[++front];
        }


    }
}