队列也是一种受限线性表,其特点是先进队的元素先出队
- 链队列
package com.day09;
/**
* 链队列.
*/
public class day09_LinkedQueue {
/**
* 队列的长度
*/
int length = 0;
/**
* 内部类
*/
class Node {
/**
* 数据域
*/
int data;
/**
* 指向下一个节点的指针
*/
Node Next;
/**
* ******************
* 构造方法给队列赋值.
*
* @param data The data.
* ******************
*/
public Node(int data) {
Next = null;
this.data = data;
} // of Node
} // of class Node
/**
* 头结点
*/
Node header;
/**
* 尾节点
*/
Node tail;
/**
* ********************
* 初始化一个新的空队列
* ********************
*/
public day09_LinkedQueue() {
header = new Node(0);
tail = header;
} // of day09_LinkedQueue()
/**
* ********************
* inqueue.
*
* @param paraValue 新节点的值.
* ********************
*/
public boolean inQueue(int paraValue) {
Node newNode = new Node(paraValue);
tail.Next = newNode;
tail = newNode;
length++;
return true;
} // of inQueue
/**
* ********************
* deQueue.
*
* @return 头结点的值
* ********************
*/
public int deQueue() {
int result = header.Next.data;
header.Next = header.Next.Next;
length--;
return result;
} // of deQueue
/***
* reset the Queue
*/
public void reset() {
header.Next = null;
length = 0;
} // of reset
/**
* ********************
* 重写toString方法.
* ********************
*/
@Override
public String toString() {
String result = "";
if (header.Next == null) {
result="空";
return result;
} // of if
Node temp = header.Next;
for (int i = 0; i < length; ++i) {
result += temp.data + ",";
temp = temp.Next;
} // of for i
return result;
} //of toString
/***
* 程序入口
* @param args
*/
public static void main(String[] args) {
day09_LinkedQueue testQueue = new day09_LinkedQueue();
System.out.println("初始化队列为:"+testQueue.toString());
for (int i = 0; i < 5; ++i) {
testQueue.inQueue(i);
} // of for i
System.out.println("循环入队后,队列为:"+testQueue);
System.out.println("出队元素:" + testQueue.deQueue());
System.out.println("出队元素:" + testQueue.deQueue());
System.out.println("出队两次后的队列为:"+testQueue);
testQueue.reset();
System.out.println("重置后的队列为:"+testQueue);
} // of main
} // of day09_LinkedQueue
运行效果:
- 循环队列
这个就要比链队列有思维难度一些,循环队列就要把内存压榨到极致,有点像追逐战。判断队列是空还是满就成为关键,当队头与队尾重合时记为空队列,当队尾追尾队头时记为满队列(这样做会牺牲一格内存),或者也可以增加一个长度属性,入队加出队减,通过该属性来判断队列的情况。
package com.day09;
/**
* 循环队列.
*/
public class day09_CircleIntQueue {
/**
* 队列最大长度,有一个空间不可用.
*/
public static final int MAX = 10;
/**
* 元素存放的数组
*/
int data[];
/**
* 队列头结点
*/
int head;
/**
* 队列尾节点
* 初始头尾相同
*/
int tail;
/**
* ******************
* 构造方法**********
* ******************
*/
public day09_CircleIntQueue() {
data = new int[MAX];
head = tail = 0;
} //of CircleIntQueue
/**
* ********************
* inQueue.
*
* @param paraValue 新节点的值
* ********************
*/
public boolean inQueue(int paraValue) {
if ((tail + 1) % MAX == head) {
System.out.println("队列满");
return false;
}// of if
data[tail++ % MAX] = paraValue;
//tail++;
return true;
}//of inQueue
/**
* ********************
* deQueue.
*
* @return 头结点的值.
* ********************
*/
public int deQueue() {
if (head == tail) {
System.out.println();
}//of if
int result = data[head++%MAX];
return result;
}//of deQueue
/**
* ********************
* reset the Queue.
* ********************
*/
public void reset() {
head = 0;
tail = 0;
}//of reset
/**
* ********************
* 重写toString方法
* ********************
*/
@Override
public String toString() {
String resultString = "";
if (head == tail) {
return "空队列";
}//of if
for (int i = head; i < tail; i++) {
resultString += data[i % MAX] + ", ";
} // Of for i
return resultString;
}//of toString
/**
* ********************
* 程序入口
*
* @param args 暂未使用
* ********************
*/
public static void main(String args[]) {
day09_CircleIntQueue tempQueue = new day09_CircleIntQueue();
System.out.println("初始化队列为: " + tempQueue.toString());
for (int i = 0; i < 5; i++) {
tempQueue.inQueue(i + 1);
} // Of for i
System.out.println("循环入队后,队列为: " + tempQueue.toString());
int tempValue = tempQueue.deQueue();
System.out.println("出队元素为: " + tempValue + ", 当前队列为: " + tempQueue.toString());
for (int i = 0; i < 6; i++) {
tempQueue.inQueue(i + 10);
System.out.println("入队, 当前队列为: " + tempQueue.toString());
} // Of for i
for (int i = 0; i < 3; i++) {
tempValue = tempQueue.deQueue();
System.out.println("出队元素为: " + tempValue + ", 当前队列为: " + tempQueue.toString());
} // Of for i
for (int i = 0; i < 6; i++) {
tempQueue.inQueue(i + 100);
System.out.println("入队, 当前队列为: " + tempQueue.toString());
} // Of for i
}// Of main
}// Of day09_CircleIntQueue
运行结果:
修改为char类型的队列:
package com.day09;
/**
* 循环队列.
*/
public class day09_CircleCharQueue {
/**
* 队列最大长度,有一个空间不可用.
*/
public static final int MAX = 10;
/**
* 元素存放的数组
*/
char []data;
/**
* 队列头结点
*/
int head;
/**
* 队列尾节点
* 初始头尾相同
*/
int tail;
/**
* ******************
* 构造方法**********
* ******************
*/
public day09_CircleCharQueue() {
data = new char[MAX];
head = tail = 0;
} //of CircleIntQueue
/**
* ********************
* inQueue.
*
* @param paraValue 新节点的值
* ********************
*/
public boolean inQueue(char paraValue) {
if ((tail + 1) % MAX == head) {
System.out.println("队列满");
return false;
}// of if
data[tail++ % MAX] = paraValue;
//tail++;
return true;
}//of inQueue
/**
* ********************
* deQueue.
*
* @return 头结点的值.
* ********************
*/
public char deQueue() {
if (head == tail) {
System.out.println();
return '\0';
}//of if
char result = data[head++%MAX];
return result;
}//of deQueue
/**
* ********************
* reset the Queue.
* ********************
*/
public void reset() {
head = 0;
tail = 0;
}//of reset
/**
* ********************
* 重写toString方法
* ********************
*/
@Override
public String toString() {
String resultString = "";
if (head == tail) {
return "空队列";
}//of if
for (int i = head; i < tail; i++) {
resultString += data[i % MAX] + ", ";
} // Of for i
return resultString;
}//of toString
/**
* ********************
* 程序入口
*
* @param args 暂未使用
* ********************
*/
public static void main(String args[]) {
day09_CircleCharQueue tempQueue = new day09_CircleCharQueue();
System.out.println("初始化队列为: " + tempQueue.toString());
for (char i = '0'; i < '5'; i++) {
tempQueue.inQueue(i);
} // Of for i
System.out.println("循环入队后,队列为: " + tempQueue.toString());
int tempValue = tempQueue.deQueue();
System.out.println("出队元素为: " + tempValue + ", 当前队列为: " + tempQueue.toString());
for (char i ='a'; i < 'f'; i++) {
tempQueue.inQueue(i);
System.out.println("入队, 当前队列为: " + tempQueue.toString());
} // Of for i
for (int i = 0; i < 3; i++) {
tempValue = tempQueue.deQueue();
System.out.println("出队元素为: " + tempValue + ", 当前队列为: " + tempQueue.toString());
} // Of for i
for (char i = 'A'; i < 'F'; i++) {
tempQueue.inQueue(i);
System.out.println("入队, 当前队列为: " + tempQueue.toString());
} // Of for i
}// Of main
}// Of day09_CircleCharQueue
总结:总体上来看队列还是不难的,只要写的时候不犯迷糊。为了实现char类型的队列我们把之前的Int队列原封不动的改了一遍,但是java1.5以上,引入泛型,泛型作为通配类型,只需在使用时声明是什么类型即可。