1、双端链表
2、测试双端链表
3、双向链表
4、测试双向链表
1、双端链表
package com.cwq.ch05;
import com.cwq.ch04.Node;
/**
* 双端链表,将数据串起来
* @author Carpoor
* @date 2019年1月29日
*/
public class MyFirstLastLinkedList {
private Node first;
private Node last;
public MyFirstLastLinkedList() {
first = null;
}
/**
* 插入数据,向队头插入
*/
public void insertFirst(long value) {
Node node = new Node(value);
if (isEmpty()) {
last = node;
}
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;
}
/**
* 删除数据,删队头
*/
public Node deleteFirst() {
Node tmp = first;
if (tmp.next == null) {
last = null;
}
first = first.next;
return tmp;
}
/**
* 展示数据
*/
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;
Node prev = first;
while (current.data != value) {
if (current.next == null) {
return null;
}
prev = current;
current = current.next;
}
if (current == first) {
first = first.next;
} else {
prev.next = current.next;
}
return current;
}
public boolean isEmpty() {
return first == null;
}
}
2、测试双端链表
package com.cwq.ch05;
/**
* @author Carpoor
* @date 2019年1月30日
*/
public class TestFirstLastMyLinkedList {
public static void main(String[] args) {
MyFirstLastLinkedList list = new MyFirstLastLinkedList();
list.insertFirst(50);
list.insertFirst(40);
list.insertFirst(30);
list.insertLast(50);
list.insertLast(40);
list.insertLast(30);
list.display();
while(!list.isEmpty()) {
list.deleteFirst();
list.display();
}
}
}
3、双向链表
package com.cwq.ch05;
/**
* 双端链表,将数据串起来
* @author Carpoor
* @date 2019年1月29日
*/
public class MyDoubleLinkedList {
private Node first;
private Node last;
public MyDoubleLinkedList() {
first = null;
}
/**
* 插入数据,向队头插入
*/
public void insertFirst(long value) {
Node node = new Node(value);
if (isEmpty()) {
last = node;
}else {
first.prev = 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.prev = last;
}
last = node;
}
/**
* 删除数据,删队头
*/
public Node deleteFirst() {
Node tmp = first;
if (tmp.next == null) {
last = null;
}else {
first.next.prev = null;
}
first = first.next;
return tmp;
}
/**
* 删除数据,删队尾
*/
public Node deleteLast() {
Node tmp = last;
if (tmp.prev == null) {
first = null;
}else {
tmp.prev.next = null;
}
last = last.prev;
return tmp;
}
/**
* 展示数据
*/
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;
Node prev = first;
while (current.data != value) {
if (current.next == null) {
return null;
}
prev = current;
current = current.next;
}
if (current == first) {
first = first.next;
} else {
prev.next = current.next;
}
return current;
}
public boolean isEmpty() {
return first == null;
}
}
4、测试双向链表
package com.cwq.ch05;
/**
* @author Carpoor
* @date 2019年1月31日
*/
public class TestMyDoubleLinkedList {
public static void main(String[] args) {
MyDoubleLinkedList list = new MyDoubleLinkedList();
list.insertFirst(50);
list.insertFirst(40);
list.insertFirst(30);
list.insertLast(50);
list.insertLast(40);
list.insertLast(30);
list.display();
while(!list.isEmpty()) {
list.deleteFirst();
list.display();
}
System.out.println("------------------------------------");
list.insertFirst(50);
list.insertFirst(40);
list.insertFirst(30);
list.insertLast(50);
list.insertLast(40);
list.insertLast(30);
list.display();
while(!list.isEmpty()) {
list.deleteLast();
list.display();
}
}
}