节点类:
public class Node {
// 存上一个节点的引用
private Node prev;
// 存当前节点的数据
Object data;
// 存下一个节点的引用
private Node next;
public Node() {
}
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
操作类:
/**
* 数组:顺序存储 链表:随机存储(见缝插针) 链表是由节点构成,first指针指向第一个成为表头节点,而终止于最后一个指向NULL的last指针。
* 属性:长度、单向链表、双向链表
* 功能:添加(尾部添加、指定位置添加) 删除(默认删除头元素、删除指定元素) 修改 查询
*
*/
public class MyLinked {
// 链表的长度
private int size;
// 维护一个头节点
private Node first;
// 维护一个尾节点
private Node last;
public MyLinked() {
}
/**
* 默认添加元素到链表的尾部
*
* @param adata
* @return
*/
public boolean add(Object data) {
// 把传进来的数据构建成一个节点
Node node = new Node(data, null);
// 判断传进来的数据是不是第一个
if (first == null) {
// 如果头节点为空,说明我们传进来的数据就是第一个节点
first = node;
} else {
// 否则,我们传进来的数据和之前的尾节点连接 如:[之前的尾节点]--[添加这个数据的节点]
last.setNext(node);
}
// 并重新赋值给尾结点
last = node;
// 维护size
size++;
return true;
}
/**
* 在链表指定位置添加一个元素
*
* @param index
* @param data
* @return
*/
public boolean add(int index, Object data) {
// 根据指定的位置去获取目标节点
Node node = getNode(index);
// 把传入的数据构建新节点
Node newNode = new Node(data, null);
if (node != null) {
// 链表不为空
/*
* befor:[1] [2] [3] [4]
* after:[1] [2] [5] [3] [4]
* node:[2] ,[5]->[3],[2]->[5]
*/
// 传入数据的节点与index后面的一个节点牵手
newNode.setNext(node.getNext());
// index节点与传入数据的节点牵手
node.setNext(newNode);
} else {
// 链表为空
first = newNode;
last = newNode;
}
size++;
return true;
}
/**
* 根据指定的位置去获取目标节点
*
* @param index
* @return
*/
private Node getNode(int index) {
// 下标的合法性判断
if (index <= 0) {
index = 0;
}
if (index >= size - 1) {
index = size - 1;
}
/*
* 查找指定位置的节点(线性查找)
*/
// 代表从第一个开始走
Node target = first;
for (int i = 0; i < index; i++) {
// 每次往后走一步
target = target.getNext();
}
return target;
}
/**
* 删除头部元素
*
* @return
*/
public boolean remove() {
if (size < 0) {
return false;
}
if (first != null) {
first = first.getNext();
}
size--;
return true;
}
/**
* 删除指定位置元素
*
* @param index
* @return
*/
public boolean remove(int index) {
if (size < 0) {
return false;
}
if (size == 0) {
first = null;
last = null;
} else {
// 根据指定的位置去获取目标节点
Node node = getNode(index);
/*
* befor:[1] [2] [5] [3] [4]
* node: [2] ,[2]->[3]
* after:[1] [2] [3] [4]
*/
node.setNext(node.getNext().getNext());
}
size--;
return true;
}
/**
* 获取元素的个数
*
* @return
*/
public int getSize() {
return size;
}
/**
* 修改指定位置的元素
*
* @param index
* @param data
* @return
*/
public boolean set(int index, Object data) {
// 根据指定的位置去获取目标节点
Node node = getNode(index);
node.setData(data);
return true;
}
/**
* 获取指定位置的元素
* @param index
* @return
*/
public Object get(int index) {
// 根据指定的位置去获取目标节点
return getNode(index).getData();
}
/**
* 重写toString方法
*/
public String toString() {
StringBuilder bulider = new StringBuilder();
if(size == 0) {
bulider.append("[]");
}else {
bulider.append("[");
for(int i=0;i<size;i++) {
bulider.append(get(i)+",");
}
bulider.setCharAt(bulider.length()-1, ']');
}
return bulider.toString();
}
}