- 链表是以节点的方式来存储,是链式存储
- 每个节点包含 data 域, next 域:指向下一个节点.
- 如图:发现链表的各个节点不一定是连续存储.
- 链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定
public class SingleLinkedList {
public static void main(String[] args) {
/**
* 先创建几个节点
*/
HeroNode heroNode = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode1 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode heroNode2 =new HeroNode(3,"吴用","智多星");
HeroNode heroNode3 =new HeroNode(4,"林冲","豹子头");
SingleLinkedListD singleLinkedList = new SingleLinkedListD();
singleLinkedList.add(heroNode1);
singleLinkedList.add(heroNode);
singleLinkedList.add(heroNode2);
singleLinkedList.add(heroNode3);
singleLinkedList.List();
}
}
/**
*定义SingleLinkedList单链表
*/
class SingleLinkedListD{
/**
* 初始化一个头结点,不存放任何数据
*/
private HeroNode head = new HeroNode(0,"","");
/**
* 添加节点
* 不考虑编号的顺序时,找到当前链表的最后节点,将指向最后节点的next指向新的节点
* @param heroNode
*/
public void add(HeroNode heroNode){
HeroNode temp = head;
/**遍历temp
* 为空,结束,不为空,指向next
*/
while (true){
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
/**
* 显示链表
* 先判断为空,遍历后temp后移,否则就是死循环
*/
public void List(){
if (head.next == null) {
System.out.println("链表为空!!!");
return;
}
HeroNode temp = head.next;
while (true){
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}
}
/**
*no:编号 name:名字 nickName:昵称
*/
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 + '\'' +
'}';
}
}
/**
*根据排名到指定位置,如果有这个排名,则增加失败
*/
public void addList(HeroNode heroNode){
HeroNode temp = head;
boolean result = false;
while (true) {
if (temp == null) {
break;
}
//位置找到,插入到temp的后面
if (temp.next.no > heroNode.no){
break;
}else if (temp.next.no == heroNode.no){
result = true;
break;
}
temp = temp.next;
}
//判断result的值
if(result){
System.out.printf("准备插入的英雄编号%d已经存在了,不能加入\n",heroNode.no);
}else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
/**
* 根据newHeroNode的no来修改
* @param newHeroNode
*/
public void update(HeroNode newHeroNode){
if (head.next == null){
System.out.println("链表为空");
return;
}
HeroNode temp = head.next;
boolean result = false;
while (true) {
if (temp == null){
break;
}
if (temp.no == newHeroNode.no){
result = true;
break;
}
temp = temp.next;
}
if (result) {
temp.name = newHeroNode.name;
temp.nickName = newHeroNode.nickName;
}else {
System.out.printf("没有找到编号 %d 的节点",newHeroNode.no);
}
}
/**
*删除
* result 标志是否找到
* @param no
*/
public void delete(int no) {
HeroNode temp = head;
boolean result = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == no){
result = true;
}
temp = temp.next;
}
if (result) {
temp.next = temp.next.next;
}else {
System.out.printf("要删除的节点 %d 不存在",no);
}
}
public static int length(HeroNode head) {
if (head.next == null){
return 0;
}
int length = 0;
HeroNode temp = head.next;
while (temp != null){
temp = temp.next;
length++;
}
return length;
}
/**
*查找链表倒数第 k 个节点
* 思路:遍历到size - index
* @param head 从头部开始遍历
* @param index 倒数第index个节点
*/
public static HeroNode findLastNode(HeroNode head,int index){
if (head.next == null) {
return null;
}
int size = length(head);
if (index <= 0 || index > size) {
return null;
}
HeroNode temp = head.next;
for (int i = 0; i <size-index ; i++) {
temp = temp.next;
}
return temp;
}
/**
* 定义一个新的节点,然后从头开始遍历,每遍历一个,取出放入新的链表最前端
* head.next = newHero.next; 实现单链表的反转
* next 指向当前节点【temp】的下一个节点
* next = temp.next; 暂时保管当前指向的下一个节点
* temp.next = newHero.next; 将下一个节点指向当前链表的最前端
* newHero.next = temp; 将temp连接到新的链表上
* temp = next; temp后移
* @param head
*/
public static void reversetList(HeroNode head){
if (head.next == null || head.next.next == null) {
return;
}
HeroNode newHero = new HeroNode(0,"","");
HeroNode temp = head.next;
HeroNode next = null;
while (temp != null) {
next = temp.next;
temp.next = newHero.next;
newHero.next = temp;
temp = next;
}
head.next = newHero.next;
}
方式二:可以将各个节点压入栈中,先进后出
/**
* 利用栈的先进后出
* @param head
*/
public static void reversePrint(HeroNode head){
if (head.next == null) {
return;
}
Stack<HeroNode> stack = new Stack<>();
HeroNode temp = head.next;
while (temp != null) {
stack.push(temp);
temp = temp.next;
}
while (stack.size() > 0){
System.out.println(stack.pop());
}
}
/**
* 查找指定元素
* @param head
* @param no
* @return
*/
public static HeroNode find(HeroNode head,int no){
HeroNode temp = head;
int size =length(head);
while (size > 0){
if (no == temp.no){
return temp;
}else {
temp = temp.next;
}
size--;
}
return temp;
}