双端链表与双向链表
什么是双端链表:链表中保存着最后一个链结点引用的链表。
直接从代码开始分析,因为和链表相似,直接在链表的代码基础上进行修改。
/*
* 链结点,相当于是车厢
*/
public class Node {
//数据域
public long data;
//指针域
public Node next;
public Node previous;
public Node(long value) {
this.data = value;
}
/**
* 显示方法
*/
public void display() {
System.out.print(data + " ");
}
}
Node类不用变。
package dataStructs; public class FirstLasstLinkList { //头结点 private Node first; //尾结点 private Node last; public FirstLasstLinkList(){ first = null; last = null; } //插入一个结点,在头结点后进行插入 /* * 要对链表进行判断,如果为空,则设置尾结点为新添加的结点 * 完成后记得将新添加的结点作为新的last结点 * */ public void insertFirst(long value){ Node node = new Node(value); if (isEmpty()){ last = node; }else { node.next = first; first = node; } } /** * 插入一个结点,从尾结点进行插入 * 如果链表为 空,则直接设置头结点为新添加的结点 * 否则设置尾结点的后一个结点尾新添加的结点 */ public void insertLast(long value) { Node node = new Node(value); if(isEmpty()) { first = node; } else { last.next = node; } last = node; } /** * 删除一个结点,在头结点后进行删除 * 判断头结点是否有下一个结点,如果没有则设置尾结点尾null */ public Node deleteFirst() { Node tmp = first; if(first.next == null) { last = null; } first = tmp.next; return tmp; } /** * 显示方法 */ public void display() { Node current = first; while (current != null) { current.display(); current = current.next; } } //判断是否为空 public boolean isEmpty(){ return (first == null); } }
可以自己定义测试类进行测试。
接下来看双向链表:
每个结点除了保存了对下一个结点的引用,同时还保存着对上一个结点的引用
/*
* 链结点,相当于是车厢
*/
public class Node {
//数据域
public long data;
//指针域
public Node next;
public Node previous;
public Node(long value) {
this.data = value;
}
/**
* 显示方法
*/
public void display() {
System.out.print(data + " ");
}
}
需要定义一个前驱指针。
/*
* 双向链表
*/
public class DoubleLinkList {
//头结点
private Node first;
//尾结点
private Node last;
public DoubleLinkList() {
first = null;
last = null;
}
/**
* 插入一个结点,在头结点后进行插入*要对链表进行判断,如果为空,则设置尾结点尾新添加的结点,不为空,需要设置头结点的前一个结点为新添加的结点
*/
public void insertFirst(long value) {
Node node = new Node(value);
if(isEmpty()) {
last = node;
} else {
first.previous = node;
}
node.next = first;
first = node;
}
/**
* 插入一个结点,从尾结点进行插入*先判断是否为空,为空,则设置头结点为新添加的结点,否则设置尾结点的后一个结点为新添加的结点,同时设置
*新添加的结点的前一个结点为为尾结点
*/
public void insertLast(long value) {
Node node = new Node(value);
if(isEmpty()) {
first = node;
} else {
last.next = node;
node.previous = last;
}
last = node;
}
/**
* 删除一个结点,在头结点后进行删除*判断头结点是否有下一个结点,没有设置结点为null,否则设置头结点的下一个结点的previous为null
*/
public Node deleteFirst() {
Node tmp = first;
if(first.next == null) {
last = null;
} else {
first.next.previous = null;
}
first = tmp.next;
return tmp;
}
/**
* 删除结点,从尾部进行删除*如果头结点没有其他结点,则头结点设置为null,否则尾结点的前一个结点的next设置尾null,设置尾结点为前一个结点
*/
public Node deleteLast() {
Node tmp = last;
if(first.next == null) {
first = null;
} else {
last.previous.next = null;
}
last = last.previous;
return last;
}
/**
* 显示方法
*/
public void display() {
Node current = first;
while(current != null) {
current.display();
current = current.next;
}
System.out.println();
}
/**
* 查找方法
*/
public Node find(long value) {
Node current = first;
while(current.data != value) {
if(current.next == null) {
return null;
}
current = current.next;
}
return current;
}
/**
* 删除方法,根据数据域来进行删除
*/
public Node delete(long value) {
Node current = first;
while(current.data != value) {
if(current.next == null) {
return null;
}
current = current.next;
}
if(current == first) {
first = first.next;
} else {
current.previous.next = current.next;
}
return current;
}
/**
* 判断是否为空
*/
public boolean isEmpty() {
return (first == null);
}
}
可以自己定义测试进行测试。