单向链表,本次写的是含有头指针的单向链表,有add()、delete()、和遍历方法实现,基本思路都是采用指针法进行元素一一对应 来操作。
package com.chunying;
import java.util.Objects;
/**
* @author chunying
* @Date: 2020/7/1 0001
*/
// 单向链表带头指针 的实现 整体采用指针法进行增加、删除以及遍历
public class LinkedList {
private final HeroNode head = new HeroNode(null, "" , 0, "");
//单向链表的添加 直接判断哪个元素是尾元素即可
public void add(HeroNode param) {
HeroNode temp = head;
while (true) {
if (temp.getNext() == null) {
break;
}else {
temp = temp.getNext();
}
}
temp.setNext(param);
}
//单向链表的有序添加 正序
public void addSort(HeroNode param) {
HeroNode temp = head;
HeroNode next = temp.getNext();
Integer rank = param.getRank();
//只有一个头元素 直接加在头指针后即可
if (next == null) {
temp.setNext(param);
return;
}
//除了头元素、只有一个真实元素 将两个元素比较即可
if (next.getNext() == null) {
if (rank >= next.getRank()) {
next.setNext(param);
}else {
head.setNext(param);
param.setNext(next);
}
return;
}
//有两个及以上的真实元素 需要两个临时指针 拿到左边元素和右边元素后插入即可
while (true) {
//如果到最后一个元素了 直接插入到最后即可
if (next == null) {
temp.setNext(param);
}
if (rank >= temp.getRank() && rank <= next.getRank()) {
temp.setNext(param);
param.setNext(next);
break;
}
temp = next;
next = temp.getNext();
}
}
//单向链表的删除 删除尾元素
public void delete() {
HeroNode temp = head;
while (true) {
if (temp.getNext() != null && temp.getNext().getNext() == null) {
temp.setNext(null);
break;
}else {
temp = temp.getNext();
}
}
}
//指定元素删除
public void delete(HeroNode heroNode) {
if (heroNode == null) {
return;
}
HeroNode temp = head;
HeroNode next = temp.getNext();
while (true) {
if (heroNode.equals(next)) {
temp.setNext(next.getNext());
break;
}
temp = next;
next = next.getNext();
//没有元素与之对应
if (next == null) {
break;
}
}
}
//遍历
public void show() {
HeroNode temp = head;
while (true) {
if (temp.getNext() == null ) {
System.out.println(temp);
break;
}else {
System.out.println(temp);
temp = temp.getNext();
}
}
}
public static void main(String[] args) {
HeroNode heroNode = new HeroNode(null, "宋江", 10, "及时雨");
HeroNode heroNode1 = new HeroNode(null, "零充", 5, "豹子头");
HeroNode heroNode2 = new HeroNode(null, "无用", 3, "赛诸葛");
LinkedList linkedList = new LinkedList();
linkedList.addSort(heroNode);
linkedList.addSort(heroNode1);
linkedList.addSort(heroNode2);
// linkedList.delete();
linkedList.show();
}
}
//要添加的元素
class HeroNode {
private HeroNode next;
private String name;
private Integer rank;
private String nickName;
public HeroNode(HeroNode next, String name, Integer rank, String nickName) {
this.next = next;
this.name = name;
this.rank = rank;
this.nickName = nickName;
}
public HeroNode getNext() {
return next;
}
public void setNext(HeroNode next) {
this.next = next;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getRank() {
return rank;
}
public void setRank(Integer rank) {
this.rank = rank;
}
@Override
public String toString() {
return "HeroNode{" +
", name='" + name + '\'' +
", rank=" + rank +
", nickName='" + nickName + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HeroNode heroNode = (HeroNode) o;
return Objects.equals(name, heroNode.name) &&
Objects.equals(rank, heroNode.rank) &&
Objects.equals(nickName, heroNode.nickName);
}
@Override
public int hashCode() {
return Objects.hash(name, rank, nickName);
}
}