链表(LinkedList) Java 语言实现

来源:互联网 发布:telnet端口命令 编辑:程序博客网 时间:2024/04/28 22:02

先要说的是,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 */}}


0 0
原创粉丝点击