(Java implementation) the one-way circular list

What is a one-way circular list

Substantially circular list unidirectional way linked list the same, the only difference is the way the circular list does not point to the end node null, but the head node (note: not head pointer).
Thus, the circular list any node of the one-way part does not exist NULL value.

Due to the nature of the one-way circular list, it is very effective in dealing with some of the data when the ring. Joseph Central famous problem can be solved by a one-way circulation list, here we will have further introduction.

Due to differences in one-way and one-way circular linked list linked list is really small, adds to change search principles are the same. So here we do not explain in detail, only available source. (If you still do not understand the words, there is a one-way linked list of portal)


Source implementation (Java)

public class Node<Anytype> {
    public Anytype data;
    public Node<Anytype> next;
    public Node(Anytype data,Node<Anytype> next){
        this.data=data;
        this.next=next;
    }
}

------------------------------------

public class SingleLink<AnyType> {


    //首元节点
    public Node<AnyType> first;

    //头指针
    public Node<AnyType> head;

    //链表长度
    int thesize;

    //初始化链表
    public boolean initlist(){
        thesize=0;
        first=new Node<>(null,null);
        head=new Node<>(null,first);
        first.next=head;
        return true;
    }

    //判断链表是否为空
    public boolean isEmpty(){
        return thesize==0;
    }

    //获取节点
    public Node<AnyType> getNode(int i){
        Node<AnyType> renode=head;
        for(int j=-2;j<i;j++){
            renode=renode.next;
        }
        return renode;
    }

    //在末尾添加元素
    public void add(AnyType a){
        Node<AnyType> renode=new Node<>(a,null);
        getNode(thesize-1).next=renode;
        renode.next=first.next;
        thesize++;
    }

    //删除i位置节点,并返回删掉的数据
    public AnyType remove(int i){
        if(i==thesize-1){
            AnyType a=getNode(thesize-1).data;
            getNode(thesize-2).next=first.next;
            return a;
        }
        Node<AnyType> prev=getNode(i-1);
        AnyType a=prev.next.data;
        prev.next=prev.next.next;
        thesize--;
        return  a;
    }

    public void remove2(Node<AnyType> n){

    }

    //在i位置插入新节点
    public void insert(int i,AnyType a){
        Node<AnyType> prev=getNode(i-1);
        Node<AnyType> renode=new Node<>(a,prev.next);
        prev.next=renode;
        thesize++;
    }

    //获取i位置节点的数据
    public AnyType get(int i){
        return getNode(i).data;
    }

    //为i位置元素重新赋值
    public void set(int i,AnyType a){
        getNode(i).data=a;
    }

    //返回链表节点个数
    public int length(){
        return thesize;
    }

    //清空链表
    public void clear(){
        initlist();
    }

    //打印链表
    public void print(){
        for(int i=0;i<thesize;i++){
            System.out.println(getNode(i).data);
        }
    }

}

One-way circular list of applications ---- Josephus problem

The origins of the problem

It is said that the famous Jewish historian Josephus had the following story: After the Roman occupation Qiaotapate, 39 Jews and Josephus and his friends hid in a cave, 39 Jews decide would rather die and do not get caught enemies and decided a suicide, 41 individuals arranged in a circle, the first personal Countin, every third number reported to the people who have to commit suicide, then a re-count by the newspaper, until all suicide killed so far. However, Josephus and his friends did not want to comply. Start with a person, beyond the individual k-2 (for the first man has been crossed), and kill the k individuals. Next, k-1 and then over the individual, and the k-th individual kill. The process continues around the circle, until finally only the next person to leave, this person can continue to live. The problem is that, given and, beginning to stand somewhere in order to avoid execution? Josephus wanted his friend to pretend to comply, he will arrange with his friends in the 16th and 31st position, then escaped this death game.

Ideas analysis

First of all people is a circle of the siege, and need to be able to cycle a lot of laps in order to exclude everyone, and it is ideal for a one-way circular list just completed was resolved, the next node end node again to get the head node just in case of problems and fit.
First, as long as we get the first node of the list, and then turn back to get the next node pointer by next head node, find the three to remove the list, followed by cycle until the list is empty, the removal order is what we need death order.

import java.util.Scanner;

public class JosephRing {
    public static void main(String[] args){
        int sum=0;
        int space=0;
        String s="";
        System.out.println("输入环数和间隔");
        Scanner sc=new Scanner(System.in);
        sum=sc.nextInt();
        space=sc.nextInt();
        SingleLink<Integer> sl=new SingleLink<>();
        sl.initlist();
        //编号add进链表
        for(int i=0;i<sum;i++){
            sl.add(i+1);
        }
        Node<Integer> n=sl.first;
        while(n.next!=n){
            for(int i=1;i<space;i++){
                n=n.next;
            }
            int a=n.next.data;
            n.next=n.next.next;
            s=s+a+",";
        }
        System.out.println(s);
    }
}
/*
    输入:41
          3
    输出:3,6,9,12,15,18,21,24,27,30,33,36,39,1,5,10,14,19,23,28,32,37,41,7,13,20,26,34,40,8,17,29,38,11,25,2,22,4,35,16,
 */

Guess you like

Origin www.cnblogs.com/sang-bit/p/11610181.html