链表的结构非常多样,以下情况组合起来就有8种链表结构:
单向、双向
带头、不带头
循环、非循环
链表的实现(单链表,不带头,非循环)
class Node { public int val; public Node next; public Node(int val) { this.val = val; } } public class MyLinkedList { public Node head; public void createLinkedList() { Node node1 = new Node(12); Node node2 = new Node(82); Node node3 = new Node(52); Node node4 = new Node(32); node1.next = node2; node2.next = node3; node3.next = node4; this.head = node1; } public void show() { Node cur = this.head; while (cur != null) { System.out.print(cur.val + " "); cur = cur.next; } System.out.println(); } public int getSize() { int count = 0; Node cur = this.head; while (cur != null) { count++; cur = cur.next; } return count; } //查找是否包含关键字key是否在单链表当中 public boolean contains(int key) { Node cur = this.head; while (cur != null) { if (cur.val == key) { return true; } cur = cur.next; } return false; } //头插法 public void addFirst(int data) { Node node = new Node(data); node.next = this.head; this.head = node; } //尾插法 public void addLast(int data) { Node node = new Node(data); if (this.head == null) { this.head = node; return; } Node cur = this.head; while (cur.next != null) { cur = cur.next; } cur.next = node; } //任意位置插入,第一个数据节点为0号下标 public boolean addIndex(int index, int data) { if (index < 0 || index > getSize()) { System.out.println("该位置不合法!"); return false; } if (index == 0) { addFirst(data); return true; } if (index == getSize()) { addLast(data); return false; } Node node = new Node(data); Node cur = findIndexSubOne(index); node.next = cur.next; cur.next = node; return true; } private Node findIndexSubOne(int index) { if (this.head == null) { return null; } Node cur = this.head; while (index - 1 != 0) { cur = cur.next; index--; } return cur; } private Node searchPrev(int key) { if (this.head == null) { return null; } Node cur = this.head; while (cur.next != null) { if(cur.next.val == key) { return cur; } cur = cur.next; } return null; } private boolean isEmpty() { return this.head == null; } public void remove(int key ) { if (isEmpty()) { System.out.println("该链表为空!"); return; } if (this.head.val == key) { this.head = this.head.next; return; } Node prev = searchPrev(key); if (prev == null) { System.out.println("没有你要删除的节点!"); return; } Node del = prev.next; prev.next = del.next; } public void removeAllKey(int key) { if (isEmpty()) { return ; } Node cur = this.head.next; Node prev = this.head; while (cur != null) { if (cur.val == key) { prev.next = cur.next; } else { prev = cur; } cur = cur.next; } if (this.head.val == key) { this.head = this.head.next; } } public void clear (){ this.head = null; } }
我们可以在另一个类中验证结果。
public class Test { public static void main(String[] args) { MyLinkedList mylinkedList = new MyLinkedList(); mylinkedList.addFirst(2); mylinkedList.addFirst(23); mylinkedList.addFirst(7); mylinkedList.show(); mylinkedList.addLast(1); mylinkedList.addLast(1); mylinkedList.addLast(2); mylinkedList.addIndex(2,13); mylinkedList.addIndex(7,1); mylinkedList.addIndex(0,100); mylinkedList.show(); System.out.println(mylinkedList.contains(13)); System.out.println(mylinkedList.contains(111)); mylinkedList.remove(55); mylinkedList.remove(23); mylinkedList.remove(30); mylinkedList.createLinkedList(); mylinkedList.show(); System.out.println(mylinkedList.getSize()); } }