1、创建一个Node类
package com.wyz.list.linklist;
public class Node {
private Node frontNode;// 节点的上一个节点
private Node backNode;// 节点的下一个节点
private Object dataNode;// 节点数据。
public Node getFrontNode() {
return frontNode;
}
public void setFrontNode(Node frontNode) {
this.frontNode = frontNode;
}
public Node getBackNode() {
return backNode;
}
public void setBackNode(Node backNode) {
this.backNode = backNode;
}
public Object getDataNode() {
return dataNode;
}
public void setDataNode(Object dataNode) {
this.dataNode = dataNode;
}
public Node() {
}
public Node(Node frontNode, Node backNode) {
super();
this.frontNode = frontNode;
this.backNode = backNode;
}
}
2、创建一个LinkListTest类
package com.wyz.list.linklist;
public class LinkListTest {
private Node headNode;// 第一个节点
private Node lastNode;// 最后一个节点
private int size;// 节点数量。
public void add(Object object) {
Node node = new Node();
// 新建一个节点,数据传进去
node.setDataNode(object);
if (headNode == null) {
// 将节点数据传递给链表。
headNode = node;
lastNode = node;
} else {
// 将新的节点设置为最后节点。
/**
* 这三行很重要啊,要互相指定下,要不容易出现空指针
*/
node.setFrontNode(lastNode);
lastNode.setBackNode(node);
node.setBackNode(null);
// 将该节点设置为最后节点
lastNode = node;
}
// 记录节点数量
size++;
}
public void add(int index, Object object) {
Node node = new Node();
// 新建一个节点,数据传进去
node.setDataNode(object);
if (headNode == null) {
// 将节点数据传递给链表。
headNode = node;
lastNode = node;
} else {
Node tmp = pNode(index);
Node f = tmp.getFrontNode();
// 插入点的和前面连接
f.setBackNode(node);
node.setFrontNode(f);
// 插入点和后面连接
tmp.setFrontNode(node);
node.setBackNode(tmp);
// ++size;
}
// 记录节点数量
size++;
}
public void remove(Object object) {
if (nullCheck()) {
System.out.println("链表为空");
} else {
// TODO 查询索引对应的节点,并返回节点
Node temp = new Node();
// 将头节点给赋给临时节点
temp = headNode;
nullCheck();// 链表为空判断
if (headNode != null) {
// 循环查询该索引对应的节点位置。
for (int i = 0; i < size; i++) {
if (temp.getDataNode().equals(object)) {
Node f = temp.getFrontNode();
Node b = temp.getBackNode();
f.setBackNode(b);
b.setFrontNode(f);
size--;
break;
} else {
temp = temp.getBackNode();
}
}
}
}
}
public void remove(int index) {
if (nullCheck()) {
System.out.println("链表为空");
} else {
// TODO 查询索引对应的节点,并返回节点
Node temp = pNode(index);
Node f = temp.getFrontNode();
Node b = temp.getBackNode();
f.setBackNode(b);
b.setFrontNode(f);
size--;
}
}
public void get(int index) {
nullCheck();
Node tmp = new Node();
if (headNode != null) {
// 使用节点查询方法,查询该节点的Node
tmp = pNode(index);
}
System.out.println(tmp.getDataNode());
}
public Node pNode(int index) {
if (nullCheck()) {
return null;
} else {
// TODO 查询索引对应的节点,并返回节点
Node temp = new Node();
// 将头节点给赋给临时节点
temp = headNode;
nullCheck();// 链表为空判断
if (headNode != null) {
if (headNode == lastNode) {
return headNode;
} else {
// 循环查询该索引对应的节点位置。
for (int i = 0; i < index; i++) {
temp = temp.getBackNode();
}
}
}
return temp;
}
}
public boolean nullCheck() {
if (headNode == null) {
System.out.println("链表为null!");
return true;
} else {
return false;
}
}
public static void main(String[] args) {
LinkListTest list = new LinkListTest();
System.out.println("****list的新增****");
list.add("000");
list.add("111");
list.add("222");
list.add("333");
list.add("444");
list.add("555");
System.out.println(list.size);
for (int i = 0; i < list.size; i++) {
list.get(i);
}
System.out.println("****list按对象删除****");
list.remove("333");
System.out.println(list.size);
for (int i = 0; i < list.size; i++) {
list.get(i);
}
System.out.println("****list按位置删除****");
list.remove(3);
System.out.println(list.size);
for (int i = 0; i < list.size; i++) {
list.get(i);
}
System.out.println(list.size);
System.out.println("****list按位置插入****");
System.out.println(list.size);
list.add(2, "小明");
System.out.println(list.size);
for (int i = 0; i < list.size; i++) {
list.get(i);
}
}
}