JAVA 数据结构与算法之————队列
直接上代码:
1,线性表作为队列
package stackandqueue;
import java.util.ArrayList;
public class LineQueue<E> {
private ArrayList<E> data = new ArrayList<>();
private int front = 0; // 队头指针
private int rear = 0; // 队尾指针
LineQueue() {}
// 获取队列长度
public int length() {
return rear - front;
}
// 判空
public boolean isEmpty() {
if (this.front == this.rear) {
return true;
} else {
return false;
}
}
// 进队
public boolean add(E e) {
data.add(e);
this.rear++;
return true;
}
// 出队
public E poll() {
if (isEmpty()) {
throw new RuntimeException("队列已空");
} else {
return (E) data.get(front++);
}
}
// 查看队头元素,但不删除
public E peek() {
if (isEmpty()) {
throw new RuntimeException("队列已空");
} else {
return (E) data.get(front);
}
}
// 测试
public static void main(String[] args) {
LineQueue<Integer> queue = new LineQueue<>();
queue.add(1);
queue.add(2);
System.out.println("length: " + queue.length());
System.out.println("poll: " + queue.poll());
System.out.println("peek: " + queue.peek());
System.out.println("isEmpty: " + queue.isEmpty());
}
}
2,链表作为队列
package stackandqueue;
public class LinkQueue<E> {
private int size;
private Node front;
private Node rear;
private class Node<E> {
E e;
Node next;
Node() {
}
;
Node(E e) {
this.e = e;
this.next = null;
}
}
LinkQueue() {
this.size = 0;
this.front = this.rear = null;
}
// 获取Node的元素值
private E getE(Node n) {
return (E) n.e;
}
// 获取队的大小
public int length() {
return size;
}
// 判空
public boolean isEmpty() {
if (this.size == 0) {
return true;
} else {
return false;
}
}
// 进队
public boolean add(E e) {
if (isEmpty()) {
Node<E> n = new Node(e);
this.front = this.rear = n;
size++;
return true;
} else {
Node<E> n = new Node(e);
this.rear.next = n;
this.rear = n;
size++;
return true;
}
}
// 出队
public E poll() {
if (isEmpty()) {
throw new RuntimeException("队列已空");
} else {
Node<E> value = this.front;
this.front = this.front.next;
value.next = null;
size--;
return getE(value);
}
}
// 查看队首元素
public E peek() {
if (isEmpty()) {
throw new RuntimeException("队列已空");
} else {
return getE(this.front);
}
}
// 测试
public static void main(String[] args) {
LinkQueue<String> queue = new LinkQueue<>();
queue.add("a");
queue.add("b");
System.out.println("length: " + queue.length());
System.out.println("poll: " + queue.poll());
System.out.println("peek: " + queue.peek());
System.out.println("isEmpty: " + queue.isEmpty());
}
}
3.用LInkedList实现队列更简单
package stackandqueue;
import java.util.LinkedList;
public class LinkedQueue<E> {
LinkedList<E> linkedList = new LinkedList<>();
// 获取队列元素个数
public int length() {
return linkedList.size();
}
// 判空
public boolean isEmpty() {
return linkedList.isEmpty();
}
// 进队
public boolean add(E e) {
return linkedList.add(e);
}
// 出队
public E poll() {
return linkedList.removeFirst();
}
// 查看队首元素
public E peek() {
return linkedList.getFirst();
}
// 测试
public static void main(String[] args) {
LinkedQueue<Integer> queue = new LinkedQueue<>();
queue.add(1);
queue.add(2);
System.out.println("length: " + queue.length());
System.out.println("poll: " + queue.poll());
System.out.println("peek: " + queue.peek());
System.out.println("isEmpty: " + queue.isEmpty());
}
}
4,还有更简单的实现方法,比如:LinkedBlockingQueue 和 ConcurrentLinkedQueue,这两个的区别可以看(https://blog.csdn.net/qq_33591903/article/details/82693558),这里实现一下ConcurrentLinkedQueue:
public static void main(String[] args) {
ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
// 判空
boolean empty = queue.isEmpty();
System.out.println(empty);
// 进队
queue.add(1);
// 查看队首元素,但不出队
int valuePeek = queue.peek();
System.out.println(valuePeek);
// 出队
int valuePoll = queue.poll();
System.out.println(valuePoll);
//
}
5, 循环队列
package stackandqueue;
import java.util.ArrayList;
public class LoopLineQueue<E> {
private ArrayList<E> data = null;
private int maxSize;
private int front;
private int rear;
private int length;
LoopLineQueue(){
this.length = 0;
this.front = this.rear = 0;
this.data = new ArrayList<>(10);
this.maxSize = 10;
}
LoopLineQueue(int initialSize){
if(initialSize < 0){
throw new RuntimeException("初始化队列的大小不能小于0" + initialSize);
}else{
this.length = 0;
this.maxSize = initialSize;
data = new ArrayList<>(initialSize);
this.front = this.rear = 0;
}
}
// 获取队列长度
public int length(){
return this.length;
}
// 判空
public boolean isEmpty(){
if(length == 0){
return true;
}else{
return false;
}
}
// 进队
public boolean add(E e){
if(this.length == this.maxSize){
throw new RuntimeException("队列已满");
}else{
data.add(e);
this.rear = (this.rear + 1) % this.maxSize; //这里与非循环队列不同
length++;
return true;
}
}
// 出队
public E poll(){
if(isEmpty()){
throw new RuntimeException("队列已空");
}else{
E value = (E)data.get(front);
this.front = (this.front + 1) % this.maxSize; //这里与非循环队列不同
length--;
return value;
}
}
// 查看队头元素,但不删除
public E peek(){
if(isEmpty()){
throw new RuntimeException("队列已空");
}else{
return (E)data.get(front);
}
}
}
代码拙劣,望大家多提意见,共同进步。