Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions src/main/java/com/ziyu/linkedlist/DoubleLinkedListDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package com.ziyu.linkedlist;

public class DoubleLinkedListDemo {


public static void main(String[] args) {
//进行测试
//先创建节点
HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
HeroNode2 hero5 = new HeroNode2(4, "林冲2", "豹子头2");
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
// DoubleLinkedList doubleLinkedList2 = new DoubleLinkedList();
doubleLinkedList.add(hero1);
doubleLinkedList.add(hero2);
doubleLinkedList.add(hero3);
doubleLinkedList.add(hero4);
doubleLinkedList.list();

System.out.println("-----");
doubleLinkedList.update(hero5);
doubleLinkedList.list();

doubleLinkedList.delete(4);
System.out.println("-----");
doubleLinkedList.list();

}
}

class DoubleLinkedList {
//初始化头结点,不存数据
HeroNode2 head = new HeroNode2(0, "", "");

//返回头结点
public HeroNode2 getHead() {
return head;
}


//遍历链表的节点的信息
public void list() {
if (head.next == null) {
System.out.println("链表为空~");
return;
}

//节点本身
HeroNode2 temp = head.next;

while (true) {
if (temp == null) {
break;
}
//输出节点的信息
System.out.println(temp);
temp = temp.next;
}
}


//添加节点 双向链表的尾部
public void add(HeroNode2 node) {

//引入一个辅助节点
HeroNode2 temp = head;

while (true) {

//当temp的下一个节点为空的时候,则退出
if (temp.next == null) {
break;
}
temp = temp.next;//链表后移动
}
//循环结束,将新节点放到尾部即可
temp.next = node;
node.pre = temp;
}


//修改节点,除编号之外的内容
public void update(HeroNode2 node) {
if (head.next == null) {
System.out.println("链表为空!");
return;
}

if (node == null) {
System.out.println("新节点内容为空!");
return;
}

HeroNode2 temp = head;
boolean flag = false;
while (true) {

//到达链表的尾部,退出即可。
if (temp.next == null) {
break;
}

if (temp.next.no == node.no) {
//找到了该节点的内容
flag = true;
break;
}
temp = temp.next;
}

if (flag) {
//找到该节点修改内容即可
temp.next.name = node.name;
temp.next.nikename = node.nikename;
} else {
//没找到要修改的节点
System.out.println("没有找到要修改的节点的内容。");
return;
}
}

/**
* 双链表可以找到自己的这个节点,然后删除即可。
*
* @param no
*/
public void delete(int no) {
if (head.next == null) {
System.out.println("该链表为空!,无法删除");
return;
}

HeroNode2 curr = head.next; //哨兵
boolean flag = false; //默认没找到

while (true) {
if (curr == null) {
break;
}
if (curr.no == no) {
flag = true;
break;
}
curr = curr.next;
}

if (flag) {
//找到了要删除的节点,删除即可
curr.pre.next = curr.next;

//代码有风险,如果删除的是最后一个节点就是 curr.next = null
if (curr.next != null) {
curr.next.pre = curr.pre;
}
} else {
System.out.println("没有找到该节点!");
return;
}

}


}

class HeroNode2 {

public int no;
public String name;
public String nikename;
public HeroNode2 next; //默认为null
public HeroNode2 pre; //默认为null

public HeroNode2(int no, String name, String nikename) {
this.no = no;
this.name = name;
this.nikename = nikename;
}

@Override
public String toString() {
return "DoubleLinkedList{" +
"no=" + no +
", name='" + name + '\'' +
", nikename='" + nikename + '\'' +
'}';
}

}
189 changes: 189 additions & 0 deletions src/main/java/com/ziyu/linkedlist/JosepfuDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com.ziyu.linkedlist;

public class JosepfuDemo {

public static void main(String[] args) {


//测试一把,看看环形链表是否正确
CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
circleSingleLinkedList.addBoy(5);
circleSingleLinkedList.showBoy();
System.out.println();
//小孩子出圈是否正确
circleSingleLinkedList.countBoy2(1, 2, 5);


}
}

class CircleSingleLinkedList {

//创建一个First节点
private Boy first = null;


//添加小孩,构建环形链表
public void addBoy(int nums) {
//nums 校验
if (nums < 1) {
System.out.println("nums的值不正确");
return;
}

Boy currBoy = null; //辅助变量,帮助构建环形链表
//for循环创建一个链表
for (int i = 1; i <= nums; i++) {
//根据编号创建小孩节点
Boy boy = new Boy(i);
//如果是第一个小孩,要特殊处理
if (i == 1) {
first = boy;
first.next = first; //自己指向自己
currBoy = first; //辅助变量指向第一个小孩子
} else {
currBoy.next = boy;
boy.next = first;
currBoy = boy;//curr 指向下一个新节点
}
}
}


public void showBoy() {
//判断是不是为空
if (first == null || first.next == null) {
System.out.println("链表为空!");
return;
}

//first指针不能动记录第一个,需要一个辅助指针遍历
Boy currBoy = first;

while (true) {
System.out.printf("小孩子的编号%d \n", currBoy.no);
if (currBoy.next == first) {
break;
} else {
currBoy = currBoy.next;
}
}
}

//根据用户的输入,计算出小孩子出圈的顺序

/**
* @param startNo 表示从第几个小孩子开始
* @param countNum 表示数几下
* @param nums 几个小孩子
*/
public void countBoy(int startNo, int countNum, int nums) {
//数据校验
if (first == null || startNo < 1 || startNo > nums) {
System.out.println("输入有误");
return;
}

//创建需要的辅助节点
Boy helper = first;
//确定第一个节点和最后一个节点
while (true) {
if (helper.next == first) {
break;
}
helper = helper.next;
}

//小哈子报数前,先让first 和 helper 移动k-1次
for (int j = 0; j < startNo - 1; j++) {
first = first.next;
helper = helper.next;
}

//出圈
while (true) {
//说明圈中只有一个节点
if (helper == first) {
break;
}
//移动 m-1 次 first and helper 移动 countNum-1
for (int j = 0; j < countNum - 1; j++) {
first = first.next;
helper = helper.next;
}
//此时的first节点就是要出圈的节点
System.out.printf("小孩子的%d出圈\n", first.no);
first = first.next;
//指针移动跳过去出圈的小孩子
helper.next = first;
}

System.out.printf("最终留在圈中的小孩子是%d\n", first.no);
}

/**
* @param startNo 起始数字
* @param countNum 步长 喊道几,几出圈
* @param num 总共几个
*/
public void countBoy2(int startNo, int countNum, int num) {

if (first == null || startNo < 1 || startNo > num) {
System.out.println("输入有误!");
return;
}

//定义辅助节点helper
Boy helper = first;

//第一次遍历,找到helper的位置 helper 放到first之前。
while (true) {
if (helper.next == first) {
break;
}
helper = helper.next;
}

//第一次helper和 first 都要移动 startNo-1次
for (int j = 0; j < startNo - 1; j++) {
first = first.next;
helper = helper.next;
}

//出圈,first 走在前面,helper走后面记录下次报数的位置
while (true) {

if (helper == first) {
break; //最后一个
}
for (int j = 0; j < countNum - 1; j++) {
first = first.next;
helper = helper.next;
}
System.out.println("这次出圈的数字为:" + first.no);
first = first.next;
helper.next = first;
}
System.out.println("最后的幸运儿为:" + first.no);
}

}

//创建boy类,表示节点
class Boy {
public int no;
public Boy next;

public Boy(int no) {
this.no = no;
}

@Override
public String toString() {
return "Boy{" +
"no=" + no +
'}';
}
}


Loading