【问题标题】:Finding uppercase in linkedlist and returning new linkedlist with found elements?在链表中查找大写并返回带有找到元素的新链表?
【发布时间】:2014-04-06 12:43:56
【问题描述】:

我必须编写一个方法来搜索链接列表(ListNode,每个列表节点包含一个字符),找到所有大写字符,将它们复制到新的 ListNode 并返回新的 ListNode。到目前为止,这是我的代码,但它没有通过 JUnit 测试(由教授提供)

这是列表节点:

public class ListNode {
    public char element;
    public ListNode next;

}

这是我写的方法,它似乎不起作用:

 public static ListNode copyUpperCase(ListNode head) {

    ListNode newListNode = mkEmpty();
    if(head == null){
        throw new ListsException("Lists: null passed to copyUpperCase");
    }else{
        char[] sss = toString(head).toCharArray();
        for(int i = 0; i < sss.length ; i++ )
                if(Character.isUpperCase(sss[i])){
                    newListNode.element = sss[i];       
                }
                newListNode = newListNode.next;
        }           
    return newListNode;
}

代码有什么用?为什么会失败?

【问题讨论】:

    标签: java eclipse algorithm list linked-list


    【解决方案1】:

    扩展@enterbios 的答案(为他+1),尝试以下操作:

    public static ListNode toUpperCase(ListNode head) {
        if (head == null)
            throw new ListsException("Lists: null passed to copyUpperCase");
    
        ListNode newHead = null;
        ListNode current = null;
    
        char[] sss = toString(head).toCharArray();
    
        for (int i=0; i<sss.length; i++) {
            if (Character.isUpperCase(sss[i])) {
                if (current == null) {
                    current = mkEmpty();
                    newHead = current;
                } else {
                    current.next = mkEmpty();
                    current = current.next;
                }
                current.element = sss[i];
            }
        }
        return newHead;
    
    }
    

    【讨论】:

      【解决方案2】:

      您需要在某处创建 newListNode.next。我在提供的代码 sn-p 中没有看到它。 尝试更改您的方法,例如:

       public static ListNode copyUpperCase(ListNode head) {
      
          ListNode newListNode = mkEmpty(); 
          ListNode newHead = newListNode;   //KEEP HEAD OF NEW LINKED LIST
          if(head == null){
              throw new ListsException("Lists: null passed to copyUpperCase");
          }else{
              char[] sss = toString(head).toCharArray();
              for(int i = 0; i < sss.length ; i++ )
                  if(Character.isUpperCase(sss[i])){
                      newListNode.element = sss[i];
                      newListNode.next = mkEmpty();   //CREATE NEW INSTANCES INSIDE LOOP
                      newListNode = newListNode.next; //MOVING FORWARD TO NEXT NODE, newListNode is the last node of new linked list
                  }
          }
          return newHead;
      }
      

      【讨论】:

      • 作为预防措施,这将导致列表末尾出现一个空节点。 (不知道有没有问题,只是想指出)
      • "XabIdRXA7pX",这是字符串,会在ListNode中变成char元素。运行 copyUpperCase-method 后,它只返回“RA”。它不应该返回“XIRXAX”吗?这是为什么呢?
      • 我明白了,我认为这将是一个问题@dcharms
      • +1 因为我扩展了你的答案,我讨厌人们从亲近的人那里狙击点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 2014-04-09
      相关资源
      最近更新 更多