环形链表、约瑟夫问题+Java栈

发布时间:2024年01月22日

约瑟夫问题:

public class Josepfu {
    public static void main(String[] args) {
        CircleSingList circleSingList = new CircleSingList();
        circleSingList.add(5);
        circleSingList.josepfu(1,2,5);
    }
}

class  CircleSingList{
    private  Boy first = null;

    public  void  add(int num){
        if (num<1){
            System.out.println("num值不正确~~");
        }
        Boy curBoy = null;
        for (int i = 1; i <=num ; i++) {
            Boy boy = new Boy(i);
            if (i==1){
                first=boy;
                first.next=first;
                curBoy=first;
            }else {
                curBoy.next=boy;
                boy.next=first;
                curBoy=boy;
            }
        }
    }

    public  void  showBoy(){
        if (first==null){
            System.out.println("链表为空~~~");
            return;
        }
        Boy boy=first;
        while (true){
            System.out.println(boy.getNo());
            if (boy.next==first){
                break;
            }
            boy=boy.next;
        }
    }
    //startNo 表示从第个小孩开始数
    //countNum 表示数几下
    //nums 表示一共有几个小孩

    public  void  josepfu(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;
        }
        for (int i = 0; i < startNo-1; i++) {
            first=first.next;
            helper=helper.next;

        }
        while (true){
            if (helper==first){
                break;
            }
            for (int i = 0; i < countNum-1; i++) {
                first=first.next;
                helper=helper.next;
            }
            System.out.println(first.getNo());
            first=first.next;
            helper.next=first;

        }
        System.out.println("最后圈中的小孩编号是"+helper.getNo());


    }
}


class  Boy{
    private  int no ;
    public   Boy next;

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

    public Boy() {
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

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

Java栈:

public class Stack {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);


        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.push(5);

        arrayStack.pop();

        arrayStack.list();


    }
}
class  ArrayStack{
    private final int maxSize;
    private  int[] stack;
    private int top=-1;

    public ArrayStack(int maxSize ){
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    //栈空
    public boolean isEmpty(){
      return top==-1;
    }

    //栈满
    public  boolean isFull(){
        return top==maxSize-1;
    }

    //入栈
    public  void  push(int value){
        if (isFull()){
            System.out.println("栈已满不能插入数据~~");
            return;
        }
        top++;
        stack[top]=value;
    }

    //出栈
    public int  pop(){
        if (isEmpty()){
            throw new RuntimeException("栈空,没有数据~~");
        }
        int value = stack[top];
        top--;
        return value;
    }

    //遍历栈
    public  void  list(){
        if (isEmpty()){
            System.out.println("栈空,没有数据~~");
            return;
        }
        for (int i =top; i >=0 ; i--) {
            System.out.println(stack[i]+" "+i);
        }
    }

}

文章来源:https://blog.csdn.net/m0_74945604/article/details/135720488
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。