基於java的佇列實現
阿新 • • 發佈:2019-01-03
基於單鏈表的佇列實現
package org.util.ds;
import java.util.Random;
/**
* 佇列
* @author Weibing Long
* @since 2018.04.16
* @param <Item> 泛型
*/
public class Queue<Item> {
private Node root;
private int n;
public static void main(String[] args) {
Queue<Integer> queue = new Queue<Integer>();
Random random = new Random();
System.out.println("進佇列");
for (int i = 0; i < 100; i++) {
int x = random.nextInt();
queue.enqueue(x);
System.out.println(x);
}
System.out.println("出佇列");
while (queue.size() > 0 )
System.out.println(queue.dequeue());
}
private class Node {
private Item item;
private Node next;
}
public void enqueue(Item item) {
if (item == null)
throw new NullPointerException("元素不能為空!");
if (root == null) {
root = new Node();
root.item = item;
root.next = root;
} else {
Node temp = root.next;
root.next = new Node();
root.next.item = item;
root = root.next;
root.next = temp;
}
n++;
}
public Item dequeue() {
if (n == 0)
throw new NullPointerException("佇列中元素為空!");
Item item = root.next.item;
root = root.next;
n--;
return item;
}
public int size() {
return n;
}
}
基於陣列的實現
上面程式碼是基於迴圈連結串列實現的,下面程式碼基於陣列實現
package org.util.ds;
/**
* 佇列,基於動態陣列
* @author 龍衛兵
*/
public class Queue<Item> {
private Object[] myArray;
private int capacity; // 容量
private int n; // 元素個數
public Queue() {
capacity = 2;
myArray = new Object[capacity];
}
public Queue(int capacity) {
this.capacity = capacity;
myArray = new Object[capacity];
}
public void push(Item item) {
if (n > capacity/2) {
capacity *= 2;
Object[] temp = new Object[capacity];
for (int i = 0; i < capacity/2; i++)
temp[i] = myArray[i];
myArray = temp;
}
myArray[n++] = item;
}
public void pop() {
if (isEmpty())
throw new NullPointerException("陣列為空");
else {
for (int i = 0; i < n-1; i++) {
myArray[i] = myArray[i + 1];
}
myArray[--n] = null;
}
}
public int size() {
return n;
}
public int getCapacity() {
return capacity;
}
public boolean isEmpty() {
return n == 0;
}
public void printContent() {
for (int i = 0; i < n; i++) {
System.out.println(myArray[i]);
}
}
@SuppressWarnings("unchecked")
public Item[] content() {
String itemName = myArray[0].getClass().getSimpleName();
if (itemName.equals("Integer")) {
Integer[] temp = new Integer[n];
for (int i = 0; i < n; i++) {
temp[i] = new Integer(myArray[i].toString());
// System.out.println(temp[i]);
}
return (Item[])temp;
} else if (itemName.equals("String")) {
String[] temp = new String[n];
for (int i = 0; i < n; i++) {
temp[i] = myArray[i].toString();
// System.out.println(temp[i]);
}
return (Item[])temp;
} else if (itemName.equals("Double")) {
Double[] temp = new Double[n];
for (int i = 0; i < n; i++) {
temp[i] = Double.valueOf(myArray[i].toString());
// System.out.println(temp[i]);
}
return (Item[])temp;
}
return null;
}
public static void main(String[] args) {
System.out.println("**********************元素為int********************************");
Queue<Integer> queue1 = new Queue<Integer>();
System.out.println("The first capacity of queue1 is " + queue1.getCapacity());
queue1.push(1);
queue1.push(2);
queue1.push(3);
System.out.println("The second capacity of queue1 is " + queue1.getCapacity());
System.out.println("The size of queue1 is " + queue1.size());
queue1.printContent();
System.out.println("**********************刪除一個元素後********************************");
queue1.pop();
queue1.printContent();
System.out.println("**********************刪除一個元素後********************************");
queue1.pop();
queue1.printContent();
System.out.println("**********************新增一個元素後********************************");
queue1.push(16);
queue1.printContent();
//Integer[] in = (Integer[])queue1.content();
Integer[] m = queue1.content();
for (int i = 0; i < queue1.size(); i++) {
System.out.println(m[i]);
}
System.out.println("**********************************************************");
Queue<Integer> queue2 = new Queue<Integer>(5);
System.out.println("The first capacity of queue2 is " + queue2.getCapacity());
queue2.push(4);
queue2.push(5);
queue2.push(6);
System.out.println("The second capacity of queue2 is " + queue2.getCapacity());
System.out.println("The size of queue2 is " + queue2.size());
queue2.printContent();
System.out.println("********************元素為字串******************************");
Queue<String> queue3 = new Queue<String>();
queue3.push("nihao");
queue3.push("haha");
queue3.push("zdjd");
String[] mm = queue3.content();
for (int i = 0; i < queue3.size(); i++) {
System.out.println(mm[i]);
}
System.out.println("********************元素為double******************************");
Queue<Double> queue4 = new Queue<Double>();
Double x1 = new Double("5.666");
queue4.push(x1);
Double x2 = new Double("-5.666");
queue4.push(x2);
Double[] mmm = queue4.content();
for (int i = 0; i < queue4.size(); i++) {
System.out.println(mmm[i]);
}
}
}
迴圈連結串列的結構如下: