先要說的是,java的API裡面已經提供了單向鏈表的類,大家可以直接拿來用。在這自己實現的目的是為了更好的理解鏈表資料結構。這裡主要是介紹一些常用結構裡面都會用到的方法,以及鏈表具體是如何操作的。
People.java 類是自訂的資料類型
public class People {private String name; //姓名private int age; //年齡private int id; //唯一標識idpublic People(String name, int age, int id) {this.name = name;this.age = age;this.id = id;}public String getName() {return this.name;}public int getAge() {return this.age;}public int getId() {return this.id;}public void printMessage() {System.out.println("name:" + this.name + " age:" + this.age + " id:" + this.id);}}
Node.java 類是封裝的節點類
public class Node {public People people; //資料域public Node next = null; //指標域 指向下一個節點的記憶體位址public Node(People people) {this.people = people;}public void printMessage() {this.people.printMessage();}}
LinkedList.java 類就是我們要操作的鏈表類了,裡面實現一些對鏈表的基本操作方法。
public class LinkedList {private Node head; //前端節點public LinkedList() { this.head = null;}//插入一個前端節點public void insertHeadNode(People people) {Node node = new Node(people);node.next = head;head = node;}//刪除前端節點 public People deleteHeadNode() {if (head == null) {System.out.println("空鏈表");return null;}People people = head.people;head = head.next;return people;}//在任意位置插入一個節點 public void insertNode(People people, int index) {if (head == null) {System.out.println("空鏈表");return;}if (index == 0) { //插入前端節點insertHeadNode(people);return;}Node node = new Node(people);Node current = head;int j;for (j = 0; j < (index - 1) && current != null; j++) {current = current.next;if (current == null) {System.out.println("插入的節點不存在");return;}}System.out.println("插入id為" +people.getId() + "的people:");node.next = current.next;current.next = node;}//刪除任意位置節點public People deleteNode(int index) {if (head == null) {System.out.println("空鏈表");return null;}if (index == 0) { //刪除前端節點return deleteHeadNode();}int j;Node current = head;Node remove;for (j = 0; j < (index - 1) && current != null; j++) {current = current.next;if (current == null) {System.out.println("刪除的節點不存在");return null;}}remove = current.next;People people = remove.people;System.out.println("刪除id為" +people.getId() + "的people:");current.next = remove.next;return people;}//根據id尋找 Peoplepublic People findNodeById(int id) {if (head == null) {System.out.println("空鏈表");return null;}System.out.println("尋找id為" +id + "的people:");Node current = head;while (current != null) {if (current.people.getId() == id) {current.people.printMessage();return current.people;}current = current.next;}System.out.println("不存在id為" + id + "的people");return null;}//根據id刪除 Peoplepublic People deleteNodeById(int id) {if (head == null) {System.out.println("空鏈表");return null;}System.out.println("刪除id為" +id + "的people:");if (head.people.getId() == id) {//如果刪除的是前端節點head.people.printMessage();People people;people = head.people;head = head.next;return people;}else {Node beforeCurrent = head; //當前節點的前一節點Node current = head.next; //當前節點while (current != null) {if (current.people.getId() == id) {current.people.printMessage();beforeCurrent.next = current.next;return current.people;}beforeCurrent = current;current = current.next;}}System.out.println("不存在id為" + id + "的people");return null;}//列印整個鏈表public void printLinkedList() {System.out.println("列印鏈表各節點數值:");if (head == null) {System.out.println("空鏈表");}Node current = head;while (current != null) {current.printMessage();current = current.next;}}}
驗證結果:
public class HelloWorld {public static void main(String[] args) {//建立鏈表LinkedList linkedList = new LinkedList();//插入一個前端節點linkedList.insertHeadNode(new People("王大", 18, 1));linkedList.insertHeadNode(new People("王二", 19, 2));linkedList.insertHeadNode(new People("王三", 28, 3));linkedList.insertHeadNode(new People("王四", 31, 4));linkedList.insertHeadNode(new People("王五", 42, 5)); linkedList.printLinkedList(); /*name:王五 age:42 id:5name:王四 age:31 id:4name:王三 age:28 id:3name:王二 age:19 id:2name:王大 age:18 id:1 */ //刪除id=3的節點linkedList.deleteNodeById(3);linkedList.printLinkedList();/*name:王五 age:42 id:5name:王四 age:31 id:4name:王二 age:19 id:2name:王大 age:18 id:1 *///尋找id=5的節點linkedList.findNodeById(5);//在位置2處插入節點linkedList.insertNode(new People("王六", 12, 6), 2);linkedList.printLinkedList();/*name:王五 age:42 id:5name:王四 age:31 id:4name:王六 age:12 id:6name:王二 age:19 id:2name:王大 age:18 id:1 *///刪除在位置1的節點linkedList.deleteNode(1);linkedList.printLinkedList();/*name:王五 age:42 id:5name:王六 age:12 id:6name:王二 age:19 id:2name:王大 age:18 id:1 */}}