链表(LinkedList) Java 语言实现

这篇博客探讨了如何在Java中自定义实现单向链表数据结构,以加深对其内部操作的理解。尽管Java API提供了LinkedList类,作者仍然详细介绍了创建链表所需的关键方法。
摘要由CSDN通过智能技术生成

先要说的是,java的API里面已经提供了单向链表的类,大家可以直接拿来用。在这自己实现的目的是为了更好的理解链表数据结构。这里主要是介绍一些常用结构里面都会用到的方法,以及链表具体是如何操作的。

People.java 类是自定义的数据类型

public class People {
	private String name; //姓名
	private int age; //年龄
	private int id; //唯一标识id
	
	public 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查找 People
	public 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删除 People
	public 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:5
		name:王四 age:31 id:4
		name:王三 age:28 id:3
		name:王二 age:19 id:2
		name:王大 age:18 id:1
 		 */
		
 		//删除id=3的节点
		linkedList.deleteNodeById(3);
		linkedList.printLinkedList();
		/*
		name:王五 age:42 id:5
		name:王四 age:31 id:4
		name:王二 age:19 id:2
		name:王大 age:18 id:1	 
		 */
		
		//查找id=5的节点
		linkedList.findNodeById(5);
		
		//在位置2处插入节点
		linkedList.insertNode(new People("王六", 12, 6), 2);
		linkedList.printLinkedList();
		/*
		name:王五 age:42 id:5
		name:王四 age:31 id:4
		name:王六 age:12 id:6
		name:王二 age:19 id:2
		name:王大 age:18 id:1
		 */
		
		//删除在位置1的节点
		linkedList.deleteNode(1);
		linkedList.printLinkedList();
		/*
		name:王五 age:42 id:5
		name:王六 age:12 id:6
		name:王二 age:19 id:2
		name:王大 age:18 id:1
		 */
	}
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值