目录
根据排名(英雄编号)将英雄插入到指定位置实现排序(如果重复,显示失败,给出说明)
自定义节点
class HeroNode {
public int no;
public String name;
public String nickName;
public HeroNode next;
public HeroNode(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
//创建节点元素
HeroNode h1 = new HeroNode(1, "宋江", "及时雨");
HeroNode h2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode h3 = new HeroNode(3, "吴用", "智多星");
HeroNode h4 = new HeroNode(4, "林冲", "豹子头");
//创建单链表
SingleLinkedList s = new SingleLinkedList();
自定义单链表中的功能与方法:
// 先定义一个头节点,这个不动
private HeroNode head = new HeroNode(0, "", "");
//获取头节点
public HeroNode getHead() {
return head;
}
添加一个新的节点
public void add(HeroNode heroNode) {
// 遍历链表,找到最后一个,然后指向新节点
// 因为头节点不能动,所以要有一个辅助节点作为遍历
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
根据排名(英雄编号)将英雄插入到指定位置实现排序(如果重复,显示失败,给出说明)
public void addByOrder(HeroNode heroNode) {
//需要辅助指针temp 帮忙,又因为是单链表,只能next所以找的temp是位于添加位置的前一个位置
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == heroNode.no) {
flag = true;
break;
} else if (temp.next.no > heroNode.no) {//找到了
break;
}
temp = temp.next;
}
if (flag) {
System.out.println("编号" + heroNode.no + "重复了,不能加");
} else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
修改元素(根据编号来定位元素,编号不能修改)
public void upDate(HeroNode heroNode) {
// 首先判断链表是否为空
if (head.next == null) {
System.out.println("链表为空,没有可修改的元素");
return;
}
boolean flag = false;
HeroNode temp = head.next;
while (true) {
if (temp == null) {//找完了
break;
}
if (temp.no == heroNode.no) {//找到了
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = heroNode.name;
temp.nickName = heroNode.nickName;
} else {
System.out.println("你要修改的编号为:" + heroNode.no + "的英雄没找到诶,糟糕!");
}
}
删除一个节点
public void delete(int index) {
boolean flag = false;
HeroNode temp = head;
if (temp.next == null) {
System.out.println("链表为空,没得删");
}
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == index) {
//找到了
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else {
System.out.println("没找到编号为:" + index + "的元素");
}
}
获取到单链表的节点个数
public int getLength() {
if (head.next == null) {
return -1;
}
HeroNode temp = head;
int num = 0;
while (temp.next != null) {
num++;
temp = temp.next;
}
return num;
}
获取单链表的倒数第K个节点
public HeroNode getLastK(int k) {
if (head.next == null) {
return null;
}
if (k > getLength() || k <= 0) {
return null;
}
int num = getLength() + 1 - k;
HeroNode temp = head;
for (int i = 0; i < num; i++) {
temp = temp.next;
}
return temp;
}
显示链表元素(遍历)
public void show() {
if (head.next == null) {
System.out.println("链表是空的,没有元素");
} else {
// 因为头节点不能动,所以要有一个辅助节点作为遍历
HeroNode temp = head.next;
while (true) {
if (temp == null) {
break;
} else {
System.out.println(temp);
temp = temp.next;
}
}
}
}
将单链表反转
public void reversetList(HeroNode head) {
//如果链表为空,或者只有一个节点就直接返回
if (head.next == null || head.next.next == null) {
return;
}
//创建辅助遍历
HeroNode temp = head.next;
HeroNode next = null;
SingleLinkedList nsll = new SingleLinkedList();
while (temp != null) {
next = temp.next;
//插入操作
temp.next = nsll.head.next;
nsll.head.next = temp;
temp = next;
}
head.next = nsll.head.next;
}
利用栈来实现逆序打印
public void LastPrint(HeroNode head) {
Stack<HeroNode> stack = new Stack();
if (head.next == null) {//只有空链表不能打印
return;
}
HeroNode temp = head.next;
while (temp != null) {
stack.add(temp);
temp = temp.next;
}
//把栈中的节点出栈打印
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
单向链表的缺点分析:
1)单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找。
2)单向链表不能自我删除,需要靠辅助节点,而双向链表,则可以自我删除,所以前面我们单链表删除时节点,总是找到temp,temp是待删除节点的前一个节点
定义节点(多了一个向前的指针)
class HeroNode {
public int no;
public String name;
public String nickName;
public HeroNode next;//默认为null
public HeroNode pre;//默认为null
添加元素时
if (temp.next == null) {//直接加在最后的情况
heroNode.pre = temp;
temp.next = heroNode;
} else {//这四句需要注意,微妙的顺序,避免next断了以后又要用next而进入死循环
heroNode.next = temp.next;
temp.next.pre = heroNode;
temp.next = heroNode;
heroNode.pre = temp;