环形链表解决Josephu(约瑟夫)问题

约瑟夫问题:

已知 n 个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为 k 的人开始报数,数到 m 的那个人出圈;他的下一个人又从 1 开始报数,数到 m 的那个人又出圈;依此规律重复下去,直到剩余最后一个胜利者。


class CircularLinkedList{
    Person head = null;

    public void add(Person person){
        if (head == null){
            head = person;
            head.next = head;
            return;
        }
        Person temp = head;
        while(temp.next != head && temp.next != null){
            temp = temp.next;
        }
        temp.next = person;
        person.next = head;
    }

    public Person get(int num){
        if (num < 0 || num > length() - 1){
            throw new RuntimeException("Input data is wrong");
        }
        Person temp = head;
        while(num != 0){
            temp = temp.next;
            num--;
        }
        return temp;
    }

    public Person delete(int num){
        isEmpty();
        if (head.num == num){
            Person p = head;
            Person temp = head;
            while(temp.next != head){
                temp = temp.next;
            }
            if (head.next != null){
                head = head.next;
                temp.next = head;
            }else{
                head = null;
            }
            return p;
        }
        Person temp = head;
        while(temp.next != head){
            if (temp.next.num == num){
                Person p = temp.next;
                temp.next = temp.next.next;
                return p;
            }
            temp = temp.next;
        }
        throw new RuntimeException("LinkedList do not have this person");
    }

    public int length(){
        int count = 0;
        Person temp = head;
        if (head == null){
            return 0;
        }
        if (head.next == null){
            return 1;
        }
        while(temp.next != head){
            count++;
            temp = temp.next;
        }
        return count + 1;
    }

    public void show(){
        isEmpty();
        Person temp = head;
        System.out.println(temp);
        while(temp.next != head){
            temp = temp.next;
            System.out.println(temp);
        }
    }

    public void isEmpty(){
        if (head == null){
            throw new RuntimeException("LinkedList do not have data");
        }
    }

    public Person getHead(){
        return head;
    }

    public void josephus(int startNum, int count, int num){
        if (head == null || startNum < 1 || startNum > num){
            throw new RuntimeException("Input data is wrong");
        }
        Person temp;
        if (startNum == 1){
            temp = head;
            while(temp.next != head){
                temp = temp.next;
            }
        }else{
            temp = get(startNum - 2);
        }
        int counter = 1;
        while (num != 0){
            if (counter == count){
                Person p = temp.next;
                temp.next = temp.next.next;
                counter = 1;
            }
            temp = temp.next;
            num--;
            counter++;
        }
        System.out.println(temp);
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值