1、链表实现队列:
相关基础知识请看这里http://blog.youkuaiyun.com/ochangwen/article/details/50686855
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。
在队列这种数据结构中,最先插入的元素将是最先被删除的元素;反之最后插入的元素将是最后被删除的元素,因此队列又称为“先进先出”(FIFO—first in first out)的线性表。
现在自己用队列双端链表实现:
public class FirstLastListQueue {
private FirstLastList list = new FirstLastList();
//入列
public void push(Object obj) {
list.insertLast(obj);
}
//出列
public Object pop() throws Exception {
return list.deleteFirst();
}
public void display() {
list.display();
}
//双端链表数据结构
private class FirstLastList {
private class Node {
Object data;
Node next;
public Node(Object obj) {
this.data = obj;
}
}
private Node head;
private Node rear;
public FirstLastList() {
head = null;
rear = null;
}
//队列增加一个值是添加在队尾
public void insertLast(Object obj) {
Node node = new Node(obj);
if (head == null) {
head = node;
} else {
//这里要想清楚
rear.next = node;
}
rear = node;
}
//队列删除就是删除头结点
public Object deleteFirst() throws Exception{
if(head == null)
throw new Exception("empty");
if(head.next == null)
rear = null;
Node temp = head;
head = head.next;
return temp.data;
}
public void display(){
if (head == null) {
System.out.println("Queue is empty!");
}
System.out.print("head -> rear : | ");
Node cur = head;
while(cur != null){
System.out.print(cur.data.toString() + " | ");
cur = cur.next;
}
System.out.print("\n");
}
}
}
测试
@Test
public void test() throws Exception {
FirstLastListQueue list = new FirstLastListQueue();
list.push(1);
list.push(2);
list.push(3);
list.display();
System.out.println(list.pop());
list.display();
list.push(4);
list.display();
}
head -> rear : | 1 | 2 | 3 |
1
head -> rear : | 2 | 3 |
head -> rear : | 2 | 3 | 4 |
1.2.系统LinkedList实现Queue接口
在java5中新增加了java.util.Queue接口,用以支持队列的常见操作。该接口扩展了java.util.Collection接口。
Queue接口里的方法
boolean add(E e):将指定元素添加到此列表的末尾。
boolean offer(E e):增加此列表的指定元素的末尾(最后一个元素)
E remove():获取并移除此列表的头(第一个元素)
E poll():获取并移除此列表的头(第一个元素)。
E element():检索,但是不移除此列表的头(第一个元素)。
E peek():检索,但是不移除此列表的头(第一个元素)
建议使用offer()来加入元素,使用poll()来获取并移出元素。它们的优点是通过返回值可以判断成功与否;
而add()和remove()方法在失败的时候会抛出异常。
element()和peek()的区别:由源码可知:peek()查找的头结点如果为空返回null,而element()则抛出异常
值得注意的是LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。
Java内部已经通过LinkList实现了队列:Queue<Integer> queue = new LinkedList<Integer>();
下面是实现LinkedList里的源码
package com.changwen.javabase.DataStructure.LinkedList.doubly;
import java.util.NoSuchElementException;
public class Queue<E> {
transient int size = 0;
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (不变) (last.next == null && last.item != null)
*/
transient Node<E> last;
/**
* Appends the specified element to the end of this list.
* 将指定元素添加到此列表的末尾。
*/
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* Adds the specified element as the tail (last element) of this list.
* 增加此列表的指定元素的末尾(最后一个元素)
* 建议使用这个方法加入元素!
*/
public boolean offer(E e) {
return add(e);
}
/**
* Retrieves and removes the head (first element) of this list.
* 获取并移除此列表的头(第一个元素)
*/
public E remove() {
return removeFirst();
}
/**
* Retrieves and removes the head (first element) of this list.
* 获取并移除此列表的头(第一个元素)。
* 建议用这个方法获取并移出元素
*/
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
/**
* Retrieves, but does not remove, the head (first element) of this list.
* 检索,但是不移除此列表的头(第一个元素)。
*
* @return the head of this list
* @throws NoSuchElementException if this list is empty
*/
public E element() {
return getFirst();
}
/**
* Retrieves, but does not remove, the head (first element) of this list.
* 检索,但是不移除此列表的头(第一个元素)。
*
* @return the head of this list, or {@code null} if this list is empty
*/
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
/**
* Returns the number of elements in this list.
* 返回此列表中的元素的数量。
*/
public int size() {
return size;
}
/**
* Returns the first element in this list.
* 返回此列表的第一个元素。
*
* @throws NoSuchElementException if this list is empty
*/
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
/**
* Removes and returns the first element from this list.
* 移除并返回此列表的第一个元素。
*
* @return 返回该列表中的第一个元素
* @throws NoSuchElementException if this list is empty
*/
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
/**
* Unlinks non-null first node f.
*/
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null; // help GC
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
return element;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<E>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
}
private static class Node<E> {
E item;
Node<E> prev;
Node<E> next;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
}