雙向鏈表和環形鏈表(約瑟夫問題)

雙向鏈表
package linkedlist;

public class DooubleLinkedListDemo {
    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,"林沖","豹子頭");
        DoubleLinkeList dll = new DoubleLinkeList();
//      dll.add(hero1);
//      dll.add(hero2);
//      dll.add(hero3);
//      dll.add(hero4);
//      dll.list();
//      HeroNode2 hero5 = new HeroNode2(4,"零充","豹子頭");
//      dll.update(hero5);
//      dll.list();
//      dll.del(3);
        dll.addByOrder(hero1);
        dll.addByOrder(hero2);
        dll.list();

    }
}

//建立一個雙向鏈表的類
class DoubleLinkeList{
    //初始化一個頭結點
    private HeroNode2 head = new HeroNode2(0,"","");
    public HeroNode2 getHead() {
        return head;
    }
    
    //遍歷雙向鏈表的方法
    public void list() {
        if(head.next == null) {
            System.out.println("鏈表爲null");
            return;
        }
        //定義輔助變量來遍歷
        HeroNode2 temp = head.next;
        while(true) {
            if(temp == null) {
                break;
            }
            System.out.println(temp);
            temp=temp.next;
        }
        
    }
    
    //添加節點
    public void add(HeroNode2 heroNode) {
        //建立輔助及節點
        HeroNode2 temp = head;
        //找到鏈表最後的節點
        while(true) {
            if(temp.next == null) {
                break;
            }
            //沒有找到,日後移
            temp=temp.next;
        }
        temp.next = heroNode;
        heroNode.pre=temp;
    }
    
    //有序添加
    public void addByOrder(HeroNode2 heroNode) {
        HeroNode2 temp = head;//臨時變量
        boolean flag = false;//英雄沒有存在就添加
        while(true) {
            if(temp.next == null) {
                break;
            }
            if(temp.next.no>heroNode.no) {
                break;
            }else if(temp.next.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
            
        }
        if(flag) {
            System.out.println("編號已經存在");
        }else {
            temp.next=heroNode;
            heroNode.pre=temp;
        }
        
    }
    
    //修改節點的內容
    public void update(HeroNode2 newHeroNode) {
        if(head.next == null) {
            System.out.println("鏈表爲空");
            return;
        }
        //定義輔助變量
        HeroNode2 temp = head.next;
        boolean flag = false;
        while(true) {
            if(temp == null) {
                break;
            }
            if(temp.no == newHeroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        }else {
            System.out.println("沒有找到要修改的編號");
        }
    }
    
    //刪除節點
    public void del(int no) {
        if(head.next == null) {
            System.out.println("鏈表爲空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while(true) {
            if(temp == null) {
                break;
            }
            if(temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            temp.pre.next = temp.next;
            if(temp.next != null ) {
                temp.next.pre = temp.pre;
            }
        }else {
            System.out.println("沒有找到要刪除的節點");
        }
    }
    
}

//定義HeroNode,每一個HeroNode 對象就是一個節點
class HeroNode2{
    
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;//指向下一個節點
    public HeroNode2 pre;//指向上一個節點
    
    //構造器
    public HeroNode2(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 + "]";
    }
}

約瑟夫問題java

img

package linkedlist;

public class Josephu {
    public static void main(String[] args) {
        CircleSingleLinkedList csll = new CircleSingleLinkedList();
        csll.addBoy(5);
        csll.showBoy();
        
        csll.countBoy(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 curBoy = null;//輔助指針,幫助構建環形鏈表
        //使用for循環建立咱們環形鏈表
        for(int i=1; i<=nums; i++) {
            //根據編號,建立小孩節點
            Boy boy = new Boy(i);
            //若是是第一個小孩
            if(i == 1) {
                first =boy;
                first.setNext(first);//構成環裝
                curBoy = first;//讓curBoy指向第一個小孩
            }else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }
    
    //遍歷
    public void showBoy() {
        if(first.getNext() == null) {
            System.out.println("鏈表爲空");
            return;
        }
        Boy curBoy = first;
        while(true){
            System.out.println(curBoy.getNo());
            if(curBoy.getNext() == first) {
                break;
            }
            curBoy = curBoy.getNext();
        }
    }
    
    //根據用戶輸入,計算出小孩的出圈順序
    //startNo 表示從第幾個小孩開始數數
    //countNum 表示數幾下
    //nums 表示最初由多少個小孩在圈中
    public void countBoy(int startNo,int countNum,int nums) {
        if(first == null || startNo <1 || startNo>nums) {
            System.out.println("參數輸入有誤");
            return;
        }
        //建立輔助指針,幫助完成小孩出圈
        Boy helper = first;
        //需求建立一個輔助指針helper,事先應該指向環形鏈表的最後這個節點
        while(true) {
            if(helper.getNext() == first) {
                break;
            }
            helper = helper.getNext();
        }
        for(int j=0;j<startNo-1;j++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        while(true) {
            if(helper == first) {
                break;
            }
            //讓first和helper指針同時移動
            for(int j=0;j<countNum-1;j++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            System.out.println("小孩出圈"+first.getNo());
            first=first.getNext();
            helper.setNext(first);
        }
        System.out.println("最後留在圈中額小孩編號"+first.getNo());
    }
}

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;
    }
}
相關文章
相關標籤/搜索