Java数据结构–双向链表
双向链表类
import java.util.Iterator;
public class TowWayLinkList<T> implements Iterable<T> {
private class Node<T> {
private T item;
private Node<T> next;
private Node<T> pre;
public Node(T item, Node pre, Node next) {
this.item = item;
this.next = next;
this.pre = pre;
}
}
private Node head;
private Node last;
private int length;
TowWayLinkList() {
this.head = new Node(null, null, null);
this.last = null;
length = 0;
}
private boolean checkIndex(int i) {
if (i > length ) {
throw new IndexOutOfBoundsException("越界!!");
} else {
return true;
}
}
public void clear() {
this.head.next = null;
this.head.pre = null;
this.head.item = null;
this.last = null;
length = 0;
}
public boolean isEmpty() {
return length == 0;
}
public int length() {
return length;
}
public T get(int i) {
T value = null;
if (checkIndex(i)) {
Node temp = head;
for (int index = 0; index <= i; index++) {
temp = temp.next;
}
value = (T) temp.item;
}
return value;
}
public void insert(T t) {
Node temp = head;
if (isEmpty()) {
last = new Node(t, head, null);
head.next = last;
} else {
Node oldLast = last;
Node newNode = new Node(t, oldLast, null);
oldLast.next = newNode;
last = newNode;
}
length++;
}
public void insert(int i, T t) {
if (checkIndex(i)) {
if (isEmpty()) {
insert(t);
} else {
Node temp = head.next;
for (int index = 0; index < i; index++) {
temp = temp.next;
}
Node nextNode = temp.next;
Node newNode = new Node(t, temp, nextNode);
temp.next = newNode;
newNode.pre = newNode;
length++;
}
}
}
public T remove(int i) {
T oldValue = null;
if (checkIndex(i)) {
Node temp = head;
for (int index = 0; index < i; index++) {
temp = temp.next;
}
oldValue = (T) temp.next.item;
if(i==length-1)
{
last=temp;
temp.next=null;
}else
{
Node nextNode = temp.next.next;
temp.next = nextNode;
nextNode.pre = temp;
}
length--;
}
return oldValue;
}
public T PreNode(Node<T> n){
return n.pre.item;
}
public int indexOf(T t) {
Node temp = head.next;
for (int i = 0; i < length; i++) {
if (temp.item.equals(t)) {
return i;
}
temp = temp.next;
}
return -1;
}
public T getFrist() {
if (isEmpty()) {
return null;
}
return (T) head.next.item;
}
public T getLast() {
if (isEmpty()) {
return null;
}
return (T) last.item;
}
@Override
public Iterator<T> iterator() {
return new Itr();
}
private class Itr implements Iterator
{
Node temp=null;
public Itr()
{
temp=head;
}
@Override
public boolean hasNext() {
return temp.next!=null;
}
@Override
public Object next() {
temp=temp.next;
return (T)temp.item;
}
}
}
测试类
import 链表.LinkList;
public class TowWayLinkListTest
{
public static void main(String[] args) {
TowWayLinkList<String> linkList=new TowWayLinkList<>();
linkList.insert("111");
linkList.insert("222");
linkList.insert("333");
linkList.insert("444");
linkList.insert(1,"ooo");
System.out.println(linkList.isEmpty());
System.out.println(linkList.get(2));
System.out.println(linkList.length());
System.out.println(linkList.remove(4));
for(String s:linkList)
{
System.out.println(s);
}
System.out.println(linkList.getFrist());
System.out.println(linkList.getLast());
}
}