单向链表
学了线性表数据结构,数组的和双向链表的,想要自己实现一个单向链表。
根据单向链表的原理,做了一个可以正序的也可以倒序的单向链表,我这个自定义的不可以使用迭代器,只能自己for循环遍历。
直接上代码
/**
* 自己写的单链表
* 可以正序也可以倒序
* 正序的好理解
* 倒序的可以实现Lru算法
* @param <T>
*/
public class MyLinkedList<T> {
/**
* 默认是false,类似栈结构,先存进去的,后取出来
*/
private boolean ISASC;
Node list;
int size;
/**
* 构造函数
* 默认是false,类似栈结构,先存进去的,后取出来
*/
public MyLinkedList() {
this(false);
}
/**
* 指定一个排序方式
*
* @param ISASC true是正序,false是倒序
*/
public MyLinkedList(boolean ISASC) {
this.ISASC = ISASC;
}
/**
* 增加
*/
public void add(T t) {
if (ISASC) {
// 按照添加顺序,顺序保存的链表
if (list == null) {
list = new Node(t, list);
} else {
Node current = new Node(t, list);
Node node = getNode(size - 1);
node.next = current;
}
} else {
//按照添加顺序,倒序保存
list = new Node(t, list);
}
size++;
}
/**
* 添加到指定位置
*
* @param index 指定位置
* @param t 添加的对象
*/
public void add(int index, T t) {
if (size == 0 && index == 0) {
add(t);
return;
}
checkIndex(index);
if (index == 0) {
list.next = new Node(t, list);
} else {
Node node = getNode(index - 1);
Node nodeNext = getNode(index);
node.next = new Node(t, nodeNext);
}
size++;
}
/**
* 删除第一个
*
* @return 返回删除的节点的数据
*/
public T remove() {
if (list != null) {
// Node removeNode = list;
// Node node = list.next;
// list.next = null;
// list = node;
// return removeNode.data;
Node node = list;
list = list.next;
node.next = null;
size--;
return node.data;
}
return null;
}
/**
* 删除指定位置的元素
*
* @param index 指定位置
* @return 返回删除的元素
*/
public T remove(int index) {
checkIndex(index);
size--;
if (index == 0) {
return remove();
} else {
Node node = getNode(index - 1);
Node current = node.next;
Node nextNode = current.next;
node.next = nextNode;
current.next = null;
return current.data;
}
}
/**
* 删除所有的节点
*/
public void removeAll() {
list = null;
size = 0;
}
/**
* 修改指定位置的数据
*
* @param index 指定位置
* @param t 需要修改的数据
*/
public void set(int index, T t) {
Node node = getNode(index);
node.data = t;
}
/**
* 查询指定位置的元素
*
* @param index 指定位置
* @return 返回数据
*/
public T get(int index) {
checkIndex(index);
Node current = list;
for (int i = 0; i < index; i++) {
current = current.next;
}
return (T) current.data;
}
/**
* 查询指定位置的节点
*
* @param index 指定位置
* @return 返回节点
*/
protected Node getNode(int index) {
checkIndex(index);
Node current = list;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
/**
* 检查索引是否越界
*
* @param index 角标
*/
protected void checkIndex(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("size=" + size + ",index=" + index);
}
/**
* 节点对象
*/
class Node {
//数据
T data;
//下一个节点
Node next;
/**
* 构造函数
*
* @param data 数据
* @param next 下一个节点
*/
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
@Override
public String toString() {
Node node = list;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < size; i++) {
stringBuilder.append(node.data + " ");
node = node.next;
}
return stringBuilder.toString();
}
}
想要测试我的这个类,直接复制我下面的代码。
public class TestMain {
public static void main(String[] args) {
testMyLinkedList();
}
private static void testMyLinkedList() {
MyLinkedList<String> linkedList = new MyLinkedList<>();
linkedList.add("aa");
linkedList.add("bb");
linkedList.add("cc");
linkedList.add(1,"dd");
// for (int i = 0; i < linkedList.size; i++) {
// System.out.println(linkedList.get(i)+"");
// }
System.out.println(linkedList.toString());
linkedList.remove();
System.out.println(linkedList.toString());
linkedList.remove(1);
System.out.println(linkedList.toString());
linkedList.set(1,"ee");
System.out.println(linkedList.toString());
linkedList.removeAll();
System.out.println(linkedList.toString());
linkedList.add(0,"ff");
System.out.println(linkedList.toString());
for (int i = 0; i < 10; i++) {
linkedList.add(String.valueOf(i));
}
System.out.println(linkedList.toString());
linkedList.add(2,"ad");
System.out.println(linkedList.toString());
linkedList.remove();
System.out.println(linkedList.toString());
linkedList.remove(4);
System.out.println(linkedList.toString());
}
}
我这个写的可能有漏洞,如果有看出来的大佬,希望能指出来,可以在下面留言,感谢指正。