3-单向链表
单向链表
基本介绍

应用


package com.company.linkedlist;
/**
* @Function :
* date 2021/5/8 - 16:35
* How :
*/
public class LinkedList {
public static void main(String[] args) {
HeroNode hero1 = new HeroNode(1, "李逵", "黑旋风");
HeroNode hero2 = new HeroNode(2, "宋江", "及时雨");
HeroNode hero3 = new HeroNode(2, "宋江", "及时雨");
HeroNode hero4 = new HeroNode(3, "林冲", "豹子头");
HeroNode hero5 = new HeroNode(4, "吴用", "智多星");
HeroNode hero6 = new HeroNode(4, "吴用(update)", "智多星(update)");
SimplelinkedList simplelinkedList = new SimplelinkedList();
// simplelinkedList.addNode(hero1);
// simplelinkedList.addNode(hero2);
// simplelinkedList.addNode(hero3);
simplelinkedList.addNodeByOrder(hero1);
simplelinkedList.addNodeByOrder(hero3);
simplelinkedList.addNodeByOrder(hero4);
simplelinkedList.addNodeByOrder(hero2);
simplelinkedList.addNodeByOrder(hero5);
simplelinkedList.showList();
System.out.println("---------------------");
simplelinkedList.update(hero6);
simplelinkedList.showList();
System.out.println("---------------------");
simplelinkedList.delNode(3);
simplelinkedList.showList();
simplelinkedList.delNode(3);
simplelinkedList.showList();
}
}
//链表
class SimplelinkedList{
//头节点 不定义具体数据
private HeroNode head = new HeroNode(0,"","");
//添加节点到单项链表尾
public void addNode(HeroNode node){
//引入辅助变量
HeroNode temp = this.head;
while (true){
//查找链表尾
if (temp.nextNode == null){
break;
}
temp = temp.nextNode;
}
temp.nextNode = node;
}
//修改节点
//根据no修改
public void update(HeroNode node){
HeroNode temp = this.head;
boolean flag = false;
while (true){
//链表尾
if (this.head.nextNode == null){
break;
}
if (temp.id == node.id){
flag = true;
break;
}
temp = temp.nextNode;
}
if (flag){
temp.name = node.name;
temp.nickName = node.nickName;
}else {
System.out.println("没有找到该编号");
}
}
//删除节点
public void delNode(int n){
HeroNode temp = this.head;
boolean flag = false;
while (true){
if (temp.nextNode == null){
break;
}
if (temp.nextNode.id == n){
flag = true;
break;
}
temp = temp.nextNode;
}
if (flag){
temp.nextNode = temp.nextNode.nextNode;
}else {
System.out.println("要删除的节点不存在");
return ;
}
}
//按顺序添加节点
public void addNodeByOrder(HeroNode node){
//查找的temp位于添加位置的前一个
HeroNode temp = this.head;
//添加的编号是否存在
boolean flag = false;
while (true){
//链表尾
if (temp.nextNode == null){
break;
}
if(temp.nextNode.id>node.id){
break;
}else if (temp.nextNode.id == node.id){
flag = true;
break;
}
temp = temp.nextNode;
}
if (flag){
System.out.println("待插入的序号已经存在 无法插入");
return;
}else {
node.nextNode = temp.nextNode;
temp.nextNode = node;
}
}
//显示列表
public void showList(){
if (head.nextNode == null){
System.out.println("链表为空");
return;
}
HeroNode temp = this.head.nextNode;
while (true){
System.out.println(temp);
//查找链表尾
if (temp.nextNode == null){
break;
}
temp = temp.nextNode;
}
}
}
//定义heroNode 对象是一个节点
class HeroNode{
public int id;
public String name;
public String nickName;
public HeroNode nextNode;
public HeroNode(int id, String name, String nickName) {
this.id = id;
this.name = name;
this.nickName = nickName;
}
//显示
@Override
public String toString() {
return "HeroNode{" +
"id=" + id +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
双向链表
基本介绍


添加一个指向前一个节点的指针
约瑟夫环
介绍



实现

package com.company.linkedlist;
import java.util.function.BiConsumer;
/**
* @Function :
* date 2021/5/9 - 13:10
* How :
*/
public class Josepfu {
public static void main(String[] args) {
CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
circleSingleLinkedList.addChildNode(10);
circleSingleLinkedList.showCircleList();
}
}
//创建一个环形单向链表
class CircleSingleLinkedList{
//创建第一个节点
private Boy firstNode = new Boy(-1);
public void addChildNode(int num){
if (num<1){
System.out.println("输入的值不正确");
return;
}
Boy currBoy = null;
for (int i = 1; i <= num; i++) {
Boy boy = new Boy(i);
if (i==1){
firstNode = boy;
firstNode.setNextNode(firstNode);
currBoy = firstNode;
}else {
currBoy.setNextNode(boy);
boy.setNextNode(firstNode);
currBoy = boy;
}
}
}
public void showCircleList(){
Boy currBoy = null;
currBoy = this.firstNode;
while (true){
System.out.printf("id = : %d \n", currBoy.getId());
if (currBoy.getNextNode()==this.firstNode){
break;
}
currBoy = currBoy.getNextNode();
}
}
}
//创建一个类 表示节点
class Boy{
private int id;
private Boy nextNode;
public Boy (int id){
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Boy getNextNode() {
return nextNode;
}
public void setNextNode(Boy nextNode) {
this.nextNode = nextNode;
}
}
出圈




浙公网安备 33010602011771号