Java中的LinkedListNode

时间:2014-12-24 04:03:39

标签: java c#

我正在尝试将C#代码转换为Java。除了if条件中的三行之外,我几乎已经转换了所有东西。

C#代码

LinkedList<T> buk = new LinkedList();
LinkedListNode<T> current = buk.First;
LinkedListNode<T> previous = null;
if (fooCondition) {
    previous = current.Previous;
} else {
    previous = current;
    current = current.Next;
}

等效的Java代码

LinkedList<T> buk = new LinkedList<>();
T current = buckets.getFirst();
T previous = null;
if (fooCondition) {
    ?                   //previous = current.Previous;  
} else {
    ?                   //previous = current;
    ?                   //current = current.Next;
}

由于Java中没有LinkedListNode类,任何人都可以建议Java中的等效代码吗?

修改

似乎完整的代码对于获得帮助非常重要。这是link

中的C#函数
  protected void MergeBuckets()
    {
        LinkedListNode<Bucket> current = buckets.First;
        LinkedListNode<Bucket> previous = null;

        int k = (int)Math.Ceiling(1 / epsilon);             // k=1/eps as integer
        int kDiv2Add2 = (int)(Math.Ceiling(0.5 * k) + 2);   // k/2 as integer
        // at this point 1/k <= eps, k >= 2, hence requires eps >= 0.5

        // number of concecutive buckets with same count causing a 
        // merge of the oldest two of those buckets
        int numberOfSameCount = 0;

        // traverse buckets from first to last, hence in order of 
        // descending timestamp and ascending count
        while (current != null)
        {
            // previous and current bucket have same count, increment counter
            if (previous != null && previous.Value.Count == current.Value.Count)
                numberOfSameCount++;
            // current is first with that count, reset counter to 1
            else
                numberOfSameCount = 1;

            // detect need for a merge
            if (numberOfSameCount == kDiv2Add2)
            {
                // merge buckets into current and remove previous
                current.Value.Timestamp = previous.Value.Timestamp;                 // take most recent timestamp
                current.Value.Count = previous.Value.Count + current.Value.Count;   // sum the counts of the buckets, 
                                                                                    // i.e. next power of two

                buckets.Remove(previous);

                // note that a merged bucket might cause a cascade of merges due to its new count,
                // hence the new current node should point to the merged bucket otherwise the 
                // cascade might go unnoticed, temporarily violating the invariant!

                previous = current.Previous;    // merged bucket's previous, since old previous is removed
                //current = current;            // trivial, merged bucket is new current

                // at this iteration, the traversal stays in place
            }
            // no merge required, continue normally
            else
            {
                previous = current;         // old current bucket or merged bucket
                current = current.Next;     // current's or merged's next

                // at this iteration, the traversal moves to the next (older) bucket
            }
        }
    }

5 个答案:

答案 0 :(得分:4)

您无法使用LinkedList提供的listIterator并使用其提供的方法浏览链接列表

ListIterator<T> listIterator = linkedListNode.listIterator(0);
if(yourCondition && listIterator.hasNext()){
    T next = listIterator.next();
}
else if (listIterator.hasPrevious()){
    T previous = listIterator.previous();
}

希望有所帮助

答案 1 :(得分:1)

Java类java.util.LinkedList的内部类LinkedList.NodeprivateNode中的LinkedList无法直接获取。相反,请参考List.indexOf(E)List.add(E, int)ListIterator等方法,以便在特定位置插入元素。

final LinkedList<T> list = new LinkedList<>();
list.add(object1);
if (cond) {
    list.add(object2, list.indexOf(object1));
} else {
    list.addFirst(object2);
}

在Java中处理LinkedList的常用习惯用法是创建LinkedList,但主要使用ListIterator对其进行操作。

final LinkedList<T> list = new LinkedList<>();
final ListIterator<T> iterator = list.listIterator();
if (!cond && list.hasNext()) {
    list.next();
}
list.add(object2);

答案 2 :(得分:0)

您无法为其创建没有节点的LinkedList。不幸的是,你的代码没有意义。

LinkedList节点(在这种情况下是双向链接)由下一个节点,前一个节点和节点中的数据以及访问器方法组成。它实现起来非常简单,因为它只是一个数据存储结构。

class LinkedListNode<T> {
    LinkedListNode prevNode, nextNode;
    T data;

    public LinkedListNode getNext() {
        return nextNode;
    }
    public LinkedListNode getPrev() {
        return prevNode;
    }
    public T getValue() {
        return data;
    }
    public void setNext( LinkedListNode n ) {
        nextNode = n;
    }
    public void setPrev( LinkedListNode n ) {
        prevNode = n;
    }
    public void setValue( T data ) {
        data = n;
    }
}

答案 3 :(得分:0)

您可以编写自己的LinkedListNode类版本,其中包含previousnext作为属性/字段。例如 -

class LinkedListNode{

 LinkedListNode previous;
 LinkedListNode next; 

}

然后添加一些getter和setter方法来访问属性/字段。您可以添加另一个属性/字段来存储节点的值。这是基本结构。这可能对您有所帮助。您也可以查看this链接 感谢

答案 4 :(得分:-2)

    LinkedList<T> buk=new LinkedList<T>();
    //make list
    T current=buk.getFirst();
    T previous=null;

    if (fooCondition) {
        previous = current.previous;
    } else {
        previous = current;
        current = current.next;
    }

和T的结构:

Class T{
  public T previous;
  public T next;
  //rest
}