使用带head头的双向链表实现 –排行榜
管理单向链表的缺点分析:
表可以向前或者向后查找。
链表,则可以 自我删除 ,所以前面我们单链表删除
时节点,总是找到 temp,temp 是待删除节点的前一
个节点 ( 认真体会 ) .
分析 双向链表的遍历,添加,修改,删除的操作思路===》代码实现
1) 遍历 方和 单链表一样,只是可以向前,也可以向后查找
2) 添加 (默认添加到双向链表的最后)
(1) 先找到双向链表的最后这个节点
(2) temp.next = newHeroNode
(3) newHeroNode.pre = temp;
3) 修改 思路和 原来的单向链表一样.
4) 删除
(1) 因为是双向链表,因此,我们可以实现自我删除某个节点
(2) 直接找到要删除的这个节点,比如temp
(3) temp.pre.next = temp.next
(4) temp.next.pre = temp.pre;
一实现:
public class DoubleList {
public static void main(String[] args) {
DoubleNode node1 = new DoubleNode(1, "口红", "纪梵希");
DoubleNode node2 = new DoubleNode(2, "粉底液", "圣罗兰");
DoubleNode node3 = new DoubleNode(3, "香水", "香奈儿");
DoubleNode node4 = new DoubleNode(4, "眉笔", "植村秀");
DoubleNode newNode = new DoubleNode(2, "鼻影", "too cool for school");
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.add(node1);
doubleLinkedList.add(node2);
doubleLinkedList.add(node3);
doubleLinkedList.add(node4);
doubleLinkedList.update(newNode);
doubleLinkedList.delete(4);
doubleLinkedList.list();
}
}
class DoubleLinkedList{
private DoubleNode head = new DoubleNode(0," "," ");
//不考虑排序,默认添加到最后
public void add(DoubleNode node){
DoubleNode temp = head;
//找到链表的最后一个节点
while (true){
if(temp.next==null) break;
temp = temp.next;
}
temp.next=node;
node.pre=temp;
}
//根据node的no来修改node
public void update(DoubleNode newNode){
if(head.next==null){
System.out.println("链表为空");
return;
}
DoubleNode temp = head;
while(true){
if(temp.next==null){
System.out.println("链表中没有该节点,添加为新节点");
add(newNode);
return;
}
if(temp.next.no== newNode.no){
newNode.next = temp.next.next;
temp.next=newNode;
newNode.pre=temp;
return;
}
temp=temp.next;
}
}
//删除节点
public void delete(int no){
if(head.next==null){
System.out.println("链表为空");
return;
}
DoubleNode temp = head.next;
while(true){
if(temp==null){
System.out.println("没有找到该节点");
return;
}
if(temp.no==no){
if(temp.next!=null){
temp.next.pre=temp.pre;
}
temp.pre.next=temp.next;
return;
}
temp = temp.next;
}
}
public void list(){
if(head.next==null){
System.out.println("链表为空");
return;
}
DoubleNode temp = head.next;
while (true){
if(temp==null) break;
System.out.println(temp);
temp=temp.next;
}
}
public DoubleNode getHead(){
return head;
}
}
class DoubleNode{
public int no;
public String name;
public String nickname;
public DoubleNode next;
public DoubleNode pre;
public DoubleNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
@Override
public String toString() {
return "DoubleNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
二、添加节点按照no大小的方式
//考虑编号排序
public void addByOrder(DoubleNode node){
DoubleNode temp = head;
while (true){
if(temp.next==null){
temp.next=node;
node.pre=temp;
return;
}
if(temp.next.no>node.no){
node.pre=temp;
node.next=temp.next;
temp.next.pre=node;
temp.next=node;
return;
}else if(temp.next.no==node.no){
System.out.println("编号"+node.no+"已经存在,无法添加");
return;
}
temp=temp.next;
}
}
三、Josephu(约瑟夫、约瑟夫环) 问题
Josephu 问题为:设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。
提示:用一个不带头结点的循环链表来处理Josephu 问题:先构成一个有n个结点的单循环链表,然后由k结点起从1开始计数,计到m时,对应结点从链表中删除,然后再从被删除结点的下一个结点又从1开始计数,直到最后一个结点从链表中删除算法结束。
构建一个单向的环形链表思路
1. 先创建第一个节点, 让 first 指向该节点,并形成环形
2. 后面当我们每创建一个新的节点,就把该节点,加入到已有的环形链表中即可.
遍历环形链表
1. 先让一个辅助指针(变量) curBoy,指向first节点
2. 然后通过一个while循环遍历 该环形链表即可 curBoy.next == first 结束
public class Josephu {
public static void main(String[] args) {
CircleSingleLinkerList circleSingleLinkerList = new CircleSingleLinkerList();
circleSingleLinkerList.addBoy(5);
circleSingleLinkerList.showBoy();
}
}
//创建环形单向列表
class CircleSingleLinkerList{
//创建一个first节点
private Boy first = null;
public void addBoy(int nums){
if(nums<=0){
System.out.println("nums的值不正确");
return;
}
Boy curBoy = null;//辅助指针
for (int i = 1; i <= nums; i++) {
Boy boy = new Boy(i);
if(i==1){
first=boy;
first.setNext(first);
curBoy=first;
}else{
curBoy.setNext(boy);
boy.setNext(first);
curBoy=boy;
}
}
}
public void showBoy(){
if(first==null){
System.out.println("没有小孩~");
return;
}
Boy curBoy = first.getNext();//辅助指针
System.out.printf("小孩的编号:%d \n",first.getNo());
while(curBoy!=first){
System.out.printf("小孩的编号:%d \n",curBoy.getNo());
curBoy=curBoy.getNext();
}
}
}
class Boy{
private int no;//编号
private Boy next;//指向下一个节点
public Boy(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Boy getNext() {
return next;
}
public void setNext(Boy next) {
this.next = next;
}
}
根据用户的输入,生成一个小孩出圈的顺序
n = 5 , 即有5个人
k = 1, 从第一个人开始报数
m = 2, 数2下
1. 需求创建一个辅助指针(变量) helper , 事先应该指向环形链表的最后这个节点.
补充: 小孩报数前,先让 first 和 helper 移动 k - 1次
2. 当小孩报数时,让first 和 helper 指针同时 的移动 m - 1 次
3. 这时就可以将first 指向的小孩节点 出圈
first = first .next
helper.next = first
原来first 指向的节点就没有任何引用,就会被回收
出圈的顺序
2->4->1->5->3
//约瑟夫出圈
public void countBoy(int startNo,int countNum,int nums){
if(startNo<=0||countNum<=0||nums<=0||startNo>nums){
System.out.println("参数输入有误");
return;
}
addBoy(nums);
for (int i = 0; i < startNo-1; i++) {
first=first.getNext();
}
Boy helper = first;
while(helper.getNext()!=first){
helper=helper.getNext();
}
while(helper!=first){
for (int i = 0; i < countNum-1; i++) {
first=first.getNext();
helper=helper.getNext();
}
System.out.printf("小孩%d出圈\n",first.getNo());
first=first.getNext();
helper.setNext(first);
}
System.out.printf("最后小孩%d出圈\n",first.getNo());
}