淘先锋技术网

首页 1 2 3 4 5 6 7

环形链表

  1. 环形链表又叫循环链表,其与单链表的唯一区别是尾部节点的next不再为空,则是指向了头部节点,这样便形成了一个环。
    在这里插入图片描述
  2. 代码实现如下
public class AnnulLink {
    private Person pointer;
    private int size;

    public AnnulLink() {
    }

    //  增
    public void add(Person p) {
        if (isEmpty()) {
            setPointer(p);
            p.setNext(p);
            ++size;
            return;
        }
        Person temp = pointer;
        for (; ; ) {
            Person next = temp.getNext();
            if (next == pointer || next == null) {
                temp.setNext(p);
                p.setNext(pointer);
                ++size;
                return;
            }
            temp = temp.getNext();
        }
    }

    //  删
    public void del(int no) {
        if (isEmpty())
            throw new RuntimeException("kong");
        if (size == 1) {
            pointer = null;
            --size;
            return;
        }
        Person temp = pointer.getNext(), prev = pointer;

        for (; ; ) {
            Integer no1 = temp.getNo();
            if (no1.equals(no)) {
                --size;
                if (temp == pointer) {
                    for (; ; ) {
                        Person next = temp.getNext();
                        if (next == pointer) {
                            setPointer(next.getNext());
                            temp.setNext(next.getNext());
                            return;
                        } else {
                            temp = next;
                        }
                    }
                } else {
                    prev.setNext(temp.getNext());
                    return;
                }
            }

            prev = temp;
            if (temp == pointer)
                return;
            temp = temp.getNext();
        }
    }

    public boolean isEmpty() {
        return size == 0;
    }

    //  改
    public boolean update(Person p) {
        Person temp = pointer;
        if (isEmpty())
            return false;
        int no = p.getNo();
        for (; ; ) {
            Integer no1 = temp.getNo();
            if (no1.equals(no)) {
                temp.setName(p.getName());
                return true;
            }
            temp = temp.getNext();
            if (temp == pointer || temp == null)
                return false;
        }
    }

    //  查
    public Person find(int no) {
        Person temp = pointer;
        if (isEmpty())
            return null;
        for (; ; ) {
            Integer no1 = temp.getNo();
            if (no1.equals(no)) {
                return temp;
            }
            temp = temp.getNext();
            if (temp == pointer || temp == null)
                return null;

        }
    }

    //  长度
    public int length() {
        return size;
    }

    //  show
    public void show() {
        if (isEmpty()) {
            return;
        }
        Person temp = pointer;

        for (; ; ) {
            System.out.println(temp);

            temp = temp.getNext();
            if (temp == pointer || temp == null)
                return;

        }
    }

    // get pointer
    public Person getPointer() {
        return pointer;
    }

    public void setPointer(Person p) {
        pointer = p;
    }
}

约瑟夫问题

  1. 约瑟夫问题(有时也称为约瑟夫斯置换,是一个计算机科学和数学中的问题。在计算机编程的算法中,类似问题又称为约瑟夫环。又称“丢手绢问题”.)
  2. 使用以上无头单链表解决约瑟夫问题
    public static void main(String[] args) {
        AnnulLink link = new AnnulLink();
        //  约瑟夫环 假设有10个节点,循环到3跳出
        for (int i = 1; i <= 10; i++) {
            link.add(new Person(i, i + ""));
        }
        int counter = 3;
        //  存放数据
        ArrayList<Person> list = new ArrayList<>();
        Person temp = link.getPointer();
        if (link.isEmpty())
            return;
        for (; ; ) {
            if (link.length() == 1) {
                return;
            }
            for (int i = 1; i < counter; i++) {
                temp = temp.getNext();
            }
            list.add(temp);
            link.del(temp.getNo());
            temp = temp.getNext();
        }
    }

总结

加油噢!