2017-10-11 93 views
0

我已经实现了一个用于添加和删除字符的DLL。代码的目的是为注册的按键提供输出。一个例子是'bai-d'会给'坏'(' - '代表退格)。 到目前为止,这些添加和删除操作都很顺利。但注册的按键还包含'<'和'>',它们代表光标向左和向右移动。一个例子是'球'< < d'>''必须给'坏'。 我觉得很难想出一个方法来在我的代码中包含'<'和'>''。在双向链表(Java)中跟踪“游标”的位置

  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++; 
} 

我希望您的帮助和建议!

+0

抢笔和纸,并绘制出你的例子。在删除/添加这些位置时,请查看执行不同操作时'游标'应该在哪里以及'链接'中的'next'和'previous'引用会发生什么。您需要引入一个类字段,该字段始终引用正确的“链接”,并允许执行诸如向右移动,向左移动以浏览链接列表等操作,以及删除和添加当前的“链接”光标位置。你总是可以从Java的LinkedList实现中的'ListIterator'中获得灵感。 –

回答

0

我想你转移根据你前后指针上<或>