java one-way circular list

Graphic list

Take the lead in the list of nodes:
First Node list

Do not take the lead in the list of nodes:

Without head node list

the difference

First Node list readily implement the code
is not easily achieved First Node and doubly linked circular list

Implementation code (increase or decrease delete)

Node implements:

public class node
{


    private int no;
    public  node next;
    public node(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    @Override
    public String toString() {
        return "Node [no=" + no + "]";
    }
}

List implementation:


public class linkedList {
    private node head;//头结点


    public linkedList() {
    }

    public boolean add( node newhero) {//添加节点
        boolean isSuccessful=false;
            if(head==null){//处理空表
                head=newhero;
                newhero.next=head;
                isSuccessful=true;
            }else{//不为空时
                node node= head;
                while(true){
                    if (node.next==head){
                        break;
                    }
                    node=node.next;
                }
                node.next=newhero;
                newhero.next=head;
                isSuccessful=true;

            }
        return isSuccessful;

    }

    public node delete(int index) {//删除制定节点
        node outputNode = null;
        node node= head; //指向第一个节点
        if ((index)==0) {//查到该节点的前一个节点
            outputNode = node;
            head=null;
            head = node.next;//删除语句
            return outputNode;
        }
        int location=0;//计时器
        while (true){  //
            if (isEmpty()) {//如果链表为空中断
                break;
            }else  if (location==index-1){//查到该节点的前一个节点
                outputNode=node.next;
                node.next=node.next.next;//删除语句

                break;
            }
            if (node.next==null){
                break;
            }
            location++;//计时器加加
            node=node.next;//节点后移
        }
        return outputNode;

    }

    public boolean isEmpty() {
        return head==null;

    }


    public void display() {//遍历整个链表

        node node=head;
        while(node!=null){
            System.out.println(node.toString());

            if (node.next.getNo()==head.getNo()){
                break;
            }
            node=node.next;

        }
        //1.对象不为空才能执行---->可避免空指针异常
        //2.循环里的收尾不是断开的

    }

The list of applications: Joseph solving problems

Problem Description: After the Roman occupation Qiaotapate, 39 Jews and Josephus and his friends hid in a cave, 39 Jews decided'd rather die than not to be caught by the enemy, 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 off by the next, until everyone committed suicide 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

Sample input:

人数:39 

Sample output:

安全位置是:16 31

problem analysis:

1. To achieve nodes (each node represents a person, comprising a location number)
2. The use of a circular linked list representative of a circle
3. Each number of three numbers, the third node is deleted, re-counting until the remaining list two elements
4. The outputs of the two nodes

java reference code:

 @Test
    public void yuecefuTest() {

        int input =31;
        LinkedList <Persion> people = new LinkedList <>();
        LinkedList <Persion> people2 = new LinkedList <>();
        for (int i = 0; i < input ;i++) {
            people.add( new Persion(i+1));
        }
        int i = 0;
        for (int j = input*2-2; j >0 ;j--) {
            if (i==2){
                people2.add(people.remove(i));
                i=0;
            }
            i++;
        }
        for (Persion p:people
             ) {
            System.out.println("  "+p.toString());
        }

    }

LinkedList source code analysis

Guess you like

Origin www.cnblogs.com/rainbowbridge/p/11410307.html