堆栈,另一个经典的数据结构。先进先出。
直接看代码吧,实现以下功能:
创建队列
插入值
取出值
值查看当前值
顺序插入
public class TheQueue { private String[] queueArray; private int queueSize; private int front, rear, numberOfItems =0; TheQueue(int size){ queueSize = size; queueArray = new String[size]; Arrays.fill(queueArray, "-1"); } public void insert(String input){ if(rear+1 < queueSize){ queueArray[rear]= input; rear++; numberOfItems++; }else System.out.println("The Queue is Full"); } public String remove(){ if(queueArray[front] != "-1"){ System.out.println(queueArray[front]); queueArray[front] = "-1"; front++; numberOfItems--; return queueArray[front]; } System.out.println("Queue is Empty"); return "-1"; } public String peek(){ if(queueArray[front] != "-1"){ System.out.println(queueArray[front]); return queueArray[front]; } System.out.println("Queue is Empty"); return "-1"; } public void priorityInsert(String input){ if(numberOfItems == 0){ insert (input); } else { if(rear+1 <queueSize){ for(int i= rear; i>=front; i--){ if(Integer.parseInt(input) < Integer.parseInt(queueArray[i-1])){ queueArray[i]= queueArray[i-1]; } else{ queueArray[i]= input; rear++; numberOfItems++; break; } } } else System.out.println("Queus is Full"); } } public void printQueue(){ StringBuffer sb = new StringBuffer("-"); for (int i = 0; i<queueSize; i++){ sb.append("-----"); } String septalLine= sb.toString(); System.out.println(septalLine); for (int i = 0; i<queueSize; i++){ System.out.print("| " + i + " "); } System.out.println("|"); System.out.println(septalLine); for (int i = 0; i<queueSize; i++){ if(queueArray[i].equals("-1")) System.out.print("| "); else System.out.print("| " + queueArray[i] + " "); } System.out.println("|"); System.out.println(septalLine); } public static void main(String[] args) { System.out.println("Create a Stack"); TheQueue queue = new TheQueue(10); queue.printQueue(); System.out.println(); System.out.println("Insert 2 values 10, 15"); queue.insert("10"); queue.insert("15"); queue.printQueue(); System.out.println(); System.out.print("Remove the first value: "); queue.remove(); queue.printQueue(); System.out.println(); System.out.print("Peek the current value: "); queue.peek(); System.out.println(); System.out.print("Remove the second value: "); queue.remove(); queue.printQueue(); System.out.println(); System.out.print("Remove the third value: "); queue.remove(); System.out.println(); System.out.println(); System.out.println("Insert Priority Value 11"); queue.priorityInsert("11"); queue.printQueue(); System.out.println(); System.out.println("Insert Priority Value 19"); queue.priorityInsert("19"); queue.printQueue(); System.out.println(); System.out.println("Insert Priority Value 16"); queue.priorityInsert("16"); queue.printQueue(); System.out.println(); System.out.println("Insert Priority Value 17"); queue.priorityInsert("17"); queue.printQueue(); System.out.println(); System.out.print("Remove a value"); queue.remove(); queue.printQueue(); System.out.println(); } }
输出结果
Create a Stack --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | | | | | | | | | | | --------------------------------------------------- Insert 2 values 10, 15 --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | 10 | 15 | | | | | | | | | --------------------------------------------------- Remove the first value: 10 --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | | 15 | | | | | | | | | --------------------------------------------------- Peek the current value: 15 --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | | 15 | | | | | | | | | --------------------------------------------------- Remove the second value: 15 --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | | | | | | | | | | | --------------------------------------------------- Remove the third value: Queue is Empty Insert Priority Value 11 --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | | | 11 | | | | | | | | --------------------------------------------------- Insert Priority Value 19 --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | | | 11 | 19 | | | | | | | --------------------------------------------------- Insert Priority Value 16 --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | | | 11 | 16 | 19 | | | | | | --------------------------------------------------- Insert Priority Value 17 --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | | | 11 | 16 | 17 | 19 | | | | | --------------------------------------------------- Remove a value11 --------------------------------------------------- | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------- | | | | 16 | 17 | 19 | | | | | ---------------------------------------------------
本文出自 “10314466” 博客,请务必保留此出处http://10324466.blog.51cto.com/10314466/1660615
原文地址:http://10324466.blog.51cto.com/10314466/1660615