返回顶部

收藏

Java模拟队列

更多
package com.stackANDqueue;

import java.io.DataInputStream;
import java.io.IOException;

/*
 * 循环队列的入队和出队
 */
public class Queue {
    static int MAX = 20;
    static String[] item = new String[MAX];
    static int front, rear;

    public Queue() {
        // TODO Auto-generated constructor stub
        front = 0;
        rear = -1;
    }

    /**
     * 按任何一键立刻执行Fuction
     */
    public static void anyKey_f(){
        char tChar;
        System.out.println("   Press any key to contunue...");
        try {
            tChar = (char) System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void enqueue_f(){//入队函数
        DataInputStream in = new DataInputStream(System.in);
        if(rear >= MAX-1) System.out.println("\n Queue is full ! \n");
        else{
            rear ++;
            System.out.println(" \n Please enter item to insert ! \n");
            System.out.flush();
            try {
                item[rear] = in.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("");
    }

    public static void dequeue_f(){//出队函数
        if(front > rear) System.out.println("\n No item ,Queue is empty ! \n");
        else{
            System.out.print("\n Item " + item[front] + " is deleted ! \n");
            front ++;
        }
        System.out.println("");
    }

    public static void list_f(){
        DataInputStream in = new DataInputStream(System.in);
        int count=0, i=0;
        if(front > rear) System.out.print("\n  No item , Queue is empty ! \n\n");
        else{
            System.out.print("\n ITEM \n");
            System.out.flush();
            for(i = front; i<= rear; i++){
                System.out.print("");
                System.out.print(item[i] + "\n");
                count ++;
                if(count % 20 ==0) anyKey_f();
            }
            System.out.print(" --------------------\n");
            System.out.print(" Total item : "+count + "\n\n");
            anyKey_f();
            System.out.println("");
        }
    }

    @SuppressWarnings("deprecation")
    public static void main(String args[]){
        DataInputStream in = new DataInputStream(System.in);
        String op = "";
        int option = 0;
        Queue obj = new Queue();
        do{
            System.out.println("****************** Stack Program *******************");
            System.out.println("                                                    ");
            System.out.println("                 <1> Insert Node                    ");
            System.out.println("                 <2> Delete Node                    ");
            System.out.println("                 <3> List  Node                     ");
            System.out.println("                 <4> Exit !                         ");
            System.out.println("                                                    ");
            System.out.println("****************************************************");
            System.out.print("\n         Choice :  ");
            System.out.flush();http://www.huiyi8.com/jiaoben/
            op = "";
            try {
                op = in.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            option = 0;
            try{
                option = Integer.valueOf(op).intValue();
            }catch(NumberFormatException e){
                System.out.println("\n Please input (1,2,3,4).....");
                System.out.println("\n\n\n");
            }
            switch(option){
                case 1:
                    enqueue_f();
                    break;
                case 2:
                    dequeue_f();
                    break;
                case 3:
                    list_f();
                    break;
                case 4:
                    System.exit(0);
            }
        }while(true);
    }

}

标签:java,队列

收藏

0人收藏

支持

1

反对

0

相关聚客文章
  1. 博主 发表 2009-11-08 04:00:00 java数据结构学习笔记之优先队列
  2. lorem 发表 2018-10-21 11:28:28 猫狗队列的再解
  3. tangw 发表 2013-03-20 08:19:53 分享:java算法学习(五)–队列
  4. lorem 发表 2018-10-21 11:28:28 猫狗队列的再解
  5. 博主 发表 2017-10-22 06:23:40 Kafka实战一
  6. 博主 发表 2013-03-05 04:00:00 Java并发包中的同步队列SynchronousQueue实现原理
  7. 博主 发表 2015-02-25 15:48:06 Kafka实战一
  8. Terry Zhang 发表 2014-05-04 12:29:21 [集合框架] Queue 接口
  9. 博主 发表 2017-05-30 15:54:23 Kafka实战一
  10. www.tonitech.com的站长 发表 2014-03-05 06:32:26 Java+MySQL报错Value ‘0000-00-00’ can not be represen
  11. xiaoli.he 发表 2018-07-16 15:12:35 Jenkins 2.132 发布,可扩展的持续集成引擎
  12. 博主 发表 2014-05-24 14:21:33 《Java 7 Concurrency Cookbook 读书笔记》基本线程同步

发表评论