本文已参与「新人创作礼」活动,一起开启掘金创作之路。
本文旨在对链表的各种操作自己进行模拟实现,以加深自己对链表操作的理解。
public class LinkList<E> {
public static class Node<E>{
E value;
Node<E> next;
public Node(E value){
this.value = value;
}
}
Node<E> head; //指向链表中的第一个有效节点
//头插法
public void addFirst(E data){
Node<E> newNode = new Node<>(data);
newNode.next = head;
head = newNode;
}
//尾插法
public void addLast(E data){
Node<E> newNode = new Node<>(data);
if (null == head){
head = newNode;
}else{
Node<E> cur = head;
while (null != cur.next)
cur = cur.next;
cur.next = newNode;
}
}
//插入任意位置
public boolean add(int index,E data){
if(index < 0){
throw new IllegalArgumentException("addIndex:position非法");
}
if(0 == index){
addFirst(data);
return true;
}
Node<E> cur = head;
Node<E> pre = head.next;
Node<E> node = new Node<>(data);
while(1 < index--){
cur = cur.next;
pre = pre.next;
}
cur.next = node;
node.next = pre;
return true;
}
// //删除任意位置的元素,没能写出
// public boolean remove(int index){
// if (null == head || index < 0 ){
// return false;
// }
// Node<E> cur = head;
// Node<E> pre = head.next;
// while(0 < index--){
// cur = cur.next;
// pre = pre.next;
// }
// }
//删除第一次出现的key元素
public void remove(E e){
Node<E> cur = head;
Node<E> pre = null;
while(null != cur){
if (e.equals(cur.value)){
cur.value = null;
if (pre == null){
head = cur.next;
}else{
pre.next = cur.next;
}
return;
}
pre = cur;
cur = cur.next;
}
}
//删除所有值为key的元素
public void removeAll(E e){
Node<E> cur = head;
Node<E> pre = null;
while(cur != null){
if(e.equals(cur.value)){
// 删除节点
cur.value = null;
if(pre == null){
head = cur.next;
cur = head;
}else{
pre.next = cur.next;
cur = pre.next;
}
}else{
pre = cur;
cur = cur.next;
}
}
}
//输出最后一个结点
public E getLastNode(){
if(null == head)
return head.value;
Node<E> cur = head;
while(null != cur.next){
cur = cur.next;
}
return cur.value;
}
//输出倒数第二个结点
public E getPenultimateNode() {
if (null == head || null == head.next)
return null;
Node<E> cur = head;
Node<E> pre = cur.next;
while(null != pre.next){
cur = cur.next;
pre = pre.next;
}
return cur.value;
}
//找到链表第n个结点
public E getNNode(int n) {
Node<E> cur = head;
if (n < 0){
return null;
}
while(0 < n--){
cur = cur.next;
}
return cur.value;
}
//计算元素个数
public int size(){
if (null == head.next)
return 1;
Node<E> cur = head;
int count = 0;
while(null != cur){
count++;
cur = cur.next;
}
return count;
}
//是否包含某个元素
public boolean contain(E e){
Node<E> cur = head;
while(null != cur){
if (e.equals(cur.value))
return true;
cur = cur.next;
}
return false;
}
//打印链表中元素
public void printLinkList(){
Node<E> cur = head;
while (null != cur){
System.out.print(cur.value);
if (null != (cur.next)){
System.out.print("-->");
}
cur = cur.next;
}
System.out.println();
}
@Override
public String toString() {
Node<E> cur = head;
String str = "";
while (null != cur){
str += cur.value;
if (null != (cur.next)){
str += "-->";
}
cur = cur.next;
}
return str;
}
public static void testLinkList() {
LinkList<Integer> linkList = new LinkList<>();
linkList.addLast(1);
linkList.addLast(2);
linkList.addLast(3);
linkList.addLast(4);
linkList.addFirst(5);
linkList.addLast(1);
linkList.addLast(2);
linkList.addLast(3);
linkList.addLast(4);
linkList.addFirst(5);
linkList.printLinkList();
System.out.println(linkList);
System.out.println("最后一个结点:"+linkList.getLastNode());
System.out.println("倒数第二个结点:"+linkList.getPenultimateNode());
System.out.println("位置3的元素为:" + linkList.getNNode(3));
System.out.println("链表中元素的个数为:" + linkList.size());
System.out.println("链表中是否包含4:" + linkList.contain(4));
linkList.add(1,6); //像位置2插入元素6
System.out.println(linkList);
linkList.remove(3);
System.out.println("删除第一个元素3后:" + linkList);
linkList.removeAll(1);
System.out.println("删除所有元素1后: " + linkList);
}
public static void main(String[] args) {
testLinkList();
}
}