仅将不存在的元素添加到java中的列表中

时间:2018-04-19 16:56:08

标签: java linked-list

我有一份学校工作,我需要在java中使用列表并插入文本文件中的单词,并排除类似的单词。我试图让它成为新元素只有在它不等于现有元素的情况下才会添加到列表中,但它会不断添加元素。

我在类List中的方法,按字母顺序插入一个新元素

public void inserirOrdem( String elemento ) {
        No novoNo = new No( elemento );
        No actual = cabeca;  //actual=head of the list
        No anterior = null;
        // look for place where to add new element
        while( actual != null && actual.item.compareTo( elemento ) < 0 ) {
        anterior = actual;
        actual = actual.prox;
        }
        if(!elemento.equals(actual.item)) { //so that the new element won't be added if there's an equal one.... doesn't work
        novoNo.prox = actual;
        if( anterior == null ) //in case the list doesn't have any elements
        cabeca = novoNo;
        else
        anterior.prox = novoNo;
        nElementos++;
        }
}

我的节点类:

private class No{

    No prox;
    String item;

    No( String elemento ) {
        item = elemento; 
        prox = null; 

    }
}

2 个答案:

答案 0 :(得分:0)

这是一个有效的解决方案,但我建议您进一步测试。我没有你原来的LList课程,所以我使用的静态版本应该可以很容易地适应你的需要。

class Main {

    /* Inserts a node into a unique, alphabetical list */
    public static No inserirOrdem(No cabeca, String elemento) {
          if (elemento == null) { return cabeca; }
          if (cabeca == null) { return new No(elemento); }

          No novoNo = new No(elemento);
          No actual = cabeca;
          No anterior = null;

          // valid entry to add at beginning of list
          if (cabeca.item.compareTo(elemento) > 0) {
              novoNo.prox = cabeca;
              return novoNo;
          }

          while (actual != null && actual.item.compareTo(elemento) < 0) {
              anterior = actual;
              actual = actual.prox;
          }

          // valid entry to add at end of list
          if (actual == null) { 
              anterior.prox = novoNo;
              return cabeca;
          }

          // valid entry to add in middle of list
          if (actual.item.compareTo(elemento) > 0) { 
              if (anterior == null) {
                  cabeca = novoNo;
              }
              else {
                  anterior.prox = novoNo;
                  novoNo.prox = actual;
              }
          }

          return cabeca;
    }

    /* Prints a linked list */
    public static void p(No head) {
        No c = head;

        while (c != null) {
            System.out.print(c.item + " -> ");
            c = c.prox;
        }

        System.out.println("\n");
    }

    public static void main(String[] args) {
        No head = new No("a");
        No b = new No("b");
        No d = new No("d");
        No e = new No("e");
        head.prox = b;
        b.prox = d;
        d.prox = e;

        System.out.println("Original:");
        p(head);

        System.out.println("Try to add duplicates:");
        head = inserirOrdem(head, "a");
        head = inserirOrdem(head, "b");
        head = inserirOrdem(head, "d");
        p(head);

        System.out.println("Try to add c in the middle:");
        head = inserirOrdem(head, "c");
        p(head);

        System.out.println("Try to add f at the end:");
        head = inserirOrdem(head, "f");
        p(head);

        System.out.println("Try to add h at the end:");
        head = inserirOrdem(head, "h");
        p(head);

        System.out.println("Try to add g in the middle:");
        head = inserirOrdem(head, "g");
        p(head);

        head = null;

        System.out.println("Null head, add a:");
        head = inserirOrdem(head, "a");
        p(head);

        System.out.println("Try to add null str:");
        head = inserirOrdem(head, null);
        p(head);

        head = new No("c");

        System.out.println("Try to add duplicate:");
        head = inserirOrdem(head, "c");
        p(head);

        System.out.println("Try to add a at beginning of list:");
        head = inserirOrdem(head, "a");
        p(head);

        System.out.println("Try to add z at end of list:");
        head = inserirOrdem(head, "z");
        p(head);

        System.out.println("Try to add f at middle of list:");
        head = inserirOrdem(head, "f");
        p(head);
    }
}

class No {
    No prox;
    String item;

    No (String elemento) {
        item = elemento;
        prox = null;
    }
}

输出:


java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

Original:
a -> b -> d -> e -> 

Try to add duplicates:
a -> b -> d -> e -> 

Try to add c in the middle:
a -> b -> c -> d -> e -> 

Try to add f at the end:
a -> b -> c -> d -> e -> f -> 

Try to add h at the end:
a -> b -> c -> d -> e -> f -> h -> 

Try to add g in the middle:
a -> b -> c -> d -> e -> f -> g -> h -> 

Null head, add a:
a -> 

Try to add null str:
a -> 

Try to add duplicate:
c -> 

Try to add a at beginning of list:
a -> c -> 

Try to add z at end of list:
a -> c -> z -> 

Try to add f at middle of list:
a -> c -> f -> z -> 

答案 1 :(得分:0)

您可以使用Set集合。它不允许任何新元素已经等于集合中的元素。只需要在你的No类中实现equals方法。

请参阅Herehere,但Google中有很多示例。

干杯。