Is it possible to create a nested LinkedObject [Java]

时间:2017-08-30 20:56:19

标签: java object linked-list singly-linked-list doubly-linked-list

fatal: A branch named 'new-branch' already exists. s aren't a thing, but I called it that because I'm wanting it to mimic the behavior of a LinkedList.

What I'm specifically trying to figure out is whether it's possible to create an git branch -D new-branch git checkout -b new-branch that is a git checkout -b new-branch --force and a reference to the proceeding object, from an array of strings. This would be used to form a chain of custody where each person that handles a piece of evidence is only aware of who they passed the evidence on to.

So, let's say I had 5 individuals who passed evidence to each other:

"Fred", "Jake", "Jane", "Beth", "Zog"

From that array of strings, I would want to create something that looked like this:

Name: Fred Object: Jake Name: Jake Object: Jane Name: Jane Object Beth Name: Beth Object: Zog Name: Zog Object: null


I'm asking this because I've made many attempts to figure this out to no avail. I don't have code to provide because my attempts have failed. I've done my best to find some kind of answer to this question.

1 个答案:

答案 0 :(得分:0)

简单。

    class LinkedObject {
        final String name;
        LinkedObject next;

        LinkedObject(String name) {
            this.name = name;
        }
    }

    LinkedObject createLinkedList(List<String> names) {
        LinkedObject head = null;

        ListIterator<String> it = names.listIterator(names.size() - 1);
        while(it.hasPrevious()) {
            if(head == null) head = new LinkedObject(it.previous());
            else {
                LinkedObject o = new LinkedObject(it.previous());
                o.next = head;
                head = o;
            }
        }

        return head;
    }

我们正在创建一个类LinkedObject,其中包含一个String字段,存储数据以及对下一个LinkedObject的引用。第一个LinkedObject称为链表的“头部”,描述整个列表。这是singly linked list

从您的示例中,如果列表中有'beth'对象,则可以从beth.next的值中找到'zog'对象,依此类推。