Java:方法的方法

时间:2017-05-19 23:40:04

标签: java list methods

我在下面的代码中有一个双向链表。暂时忽略评论。 (我已经从github上的某个地方复制了重要部分,所以如果你发现类似的东西,不要感到惊讶。)

我的问题非常基础,我需要以下表达式:

n.prext的n.next。

如果我没弄错的话,n.next和n.previous都是方法。

我该怎么做?

提前致谢

public class DoublyLinkedList {

int size =0;
Knoten head = null;
Knoten tail = null;


public Knoten addAtEnd(int data){
    Knoten n = new Knoten(data);
    if(size==0){
        head = n;
        tail = n;
    }else{
        tail.next = n;
        n.previous = tail;
        tail =n;
    }
    size++;
    return n;
}

public Knoten Search(int data)
{
    Knoten n = new Knoten(data);
    /*for i in DoublyLinkedList
    /*if hat n.data = data(of parameter)
     * {n.next of previous n n is now : n.next
     * n.previous of next n = n previous
     * }
     */
    size--;
    return n;
}

public Knoten delete(int data)
{
    Knoten n = new Knoten(data);
    if(n.previous != null)
    {

    /*  n.next of n.previous = n.next
     * n.previous of n.previous = n previous
     */
    }
    else {
        // n.next is now head
    }

    size--;
    return n;
}



public static void main(String[] args) {
    DoublyLinkedList d = new DoublyLinkedList();
    d.addAtEnd(3);
}

}

class Knoten
{
    int data;
    Knoten next;
    Knoten previous;

    public Knoten(int data)
    {
        this.data = data;
        next = null;
        previous = null;  
    }
}

1 个答案:

答案 0 :(得分:0)

该类Knoten实现了doubly linked list pattern,因此nextprevious是遍历列表所需的指向列表中开始和结束节点的指针。链接列表可以在这里描述:

enter image description here

This class DoublyLinkedList是Java中的一个简单实现。 您可以在此处查看next()previous()如何作为方法实现:

        public Item next() {
            if (!hasNext()) throw new NoSuchElementException();
            lastAccessed = current;
            Item item = current.item;
            current = current.next; 
            index++;
            return item;
        }

        public Item previous() {
            if (!hasPrevious()) throw new NoSuchElementException();
            current = current.prev;
            index--;
            lastAccessed = current;
            return current.item;
        }

但这不是他们在Knoten班级中的行为方式。

相关问题