跟踪光标的位置'在双向链表(Java)中

时间:2017-10-11 14:38:28

标签: java cursor doubly-linked-list

我已经实现了一个用于添加和删除字符的DLL。代码的目的是为已注册的按键提供输出。一个例子是' bai-d'会给“坏”#39; (' - '代表退格)。 到目前为止,这些添加和删除操作都很顺利。但是注册的按键还包含'<'和'>',表示光标向左右移动。一个例子是' ball< < d> > - '必须给予“坏”#。 我发现很难想出一种方法来包含'<'和'>'在我的代码中。

            MyLinkedList newList = new MyLinkedList();
            String password = input.nextLine();

            for (char ch : password.toCharArray()){
                if(Character.isLetter(ch) || Character.isDigit(ch)){
                    newList.addRear(ch);
                    }

                if(ch == '-'){
                    newList.removeRear();                       
                }
                /*if(ch == '<'){
                    //cursor shifting

                }
                if(ch == '>'){
                    //cursor shifting
                }*/
        }   

            newList.print();

}

这里我只是为角色调用方法。在下一节中,我将介绍DLL的一部分实现。我有大小的方法,正面和背面插入,从正面和背面删除。

public void removeFront(){ 
    if(head==null) return;

    head = head.next;
    head.previous = null;
    size--;
}

public void removeRear(){ 
    if(head==null) return;
    if(head.next == null){
        head = null;
        size--;
        return; 
    }
    Link current = head;
    while(current.next.next != null){
        current = current.next;
    }
    current.next = null;
    size--;
}
public void addFront(char data){ 
    if(head==null){
        head = new Link(null, data, null);
        }

    else{
        if(Character.isLetter(data) || Character.isDigit(data)){
            Link newLink = new Link(null, data, head); 
            head.previous = newLink; 
            head = newLink; 
        }
    }
    size++;
}

public void addRear(char data){ 
    if (head==null){
        head= new Link(null, data, null);   
    }
    else{
        Link current = head;
        while(current.next != null){
            current = current.next;
        }
        Link newLink = new Link(current, data, null);
        current.next = newLink;
    }
    size++;
}

感谢您的帮助和建议!

1 个答案:

答案 0 :(得分:0)

我认为你基于&lt;转换你的前后指针了。或&gt;