java - 2个链表集之间的并集和交集,java

标签 java linked-list union

我正在尝试将方法写入“联合”,可以这样描述:如果 A、B、C 是集合,则具有 C = A.union(B) 的形式。 Union 返回一个 Set,它包含集合 A 和 B 中的所有元素,但只列出一次重复项。

我的思路是先遍历集合A,将其所有元素加入并集,再遍历集合B,如果并集已经存在集合B的一个元素,则不插入结果,否则全部插入联合集。

这对于像我这样的初学者来说很复杂,因为我想将所有 3 个列表都包含到该方法中(我得到了一堆错误)。我已经在 SLinkedList 类中编写了一些方法来检查和添加元素,但参数从节点获取对象

/** Singly linked list .*/
public class SLinkedList {

  protected Node head;   // head node of the list
  protected int size;    // number of nodes in the list

  /** Default constructor that creates an empty list */
  public SLinkedList() {
    head = new Node(null, null); // create a dummy head
    size = 0; 

 // add last
  public void addLast(Object data) {
      Node cur = head;
      // find last node
      while (cur.getNext() != null) {
          cur = cur.getNext();
      }
      // cur refers to the last node
      cur.setNext(new Node(data, null));
      size++;
  } 

 // contain method to check existing elements
  public boolean contain (Object target) {
      boolean status = false;
      Node cursor;
      for (cursor = head; cursor != null; cursor = cursor.getNext()) {
          if (target.equals(cursor.getElement())) {
              status = true;
          }       
      }   
      return status;
  }

  public SLinkedList union (SLinkedList secondSet) {
      SLinkedList unionSet = new SLinkedList();
      secondSet = new SLinkedList();
      Node cursor;
      for(cursor = head.getNext(); cursor != null; cursor = cursor.getNext()) {
          unionSet.addLast(cursor.getElement());
          // traverse secondSet, if an element is existed in either set A or union
              // set, skip, else add to union set
          }
      }
    return unionSet;
  }


  }

节点类

/** Node of a singly linked list of strings. */
public class Node {

  private Object element;   // we assume elements are character strings
  private Node next;

  /** Creates a node with the given element and next node. */
  public Node(Object o, Node n) {
    element = o;
    next = n;
  }

  /** Returns the element of this node. */
  public Object getElement() { return element; }

  /** Returns the next node of this node. */
  public Node getNext() { return next; }

  // Modifier methods:
  /** Sets the element of this node. */
  public void setElement(Object newElem) { element = newElem; }

  /** Sets the next node of this node. */
  public void setNext(Node newNext) { next = newNext; }

*更新* 问题是如果有第二个列表涉及 public SLinkedList union (SLinkedList secondSet) ,我应该使用什么语法来遍历集合 B 并检查结果中是否已经存在集合 B 的元素然后不要将其插入结果,否则插入。我需要为集合 B 创建一个节点并遍历它吗?可能有一个比较方法来比较 union 方法之外的 2 个集合?

请帮忙。谢谢大家。

最佳答案

SLinkedList unionSet = null; // need a new SLinkedList() here
Node cursor;
for(cursor = head.getNext(); cursor != null; cursor = cursor.getNext()) {
    unionSet.addLast(cursor.getElement()); // NPE because unionSet is null
}

关于java - 2个链表集之间的并集和交集,java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14577174/

相关文章:

java - Android 照片应用程序错误

c++ - 链表更改节点而不是添加另一个节点

sql - 无重复的多列联合查询

java - 创建链表时的removeZero()方法?

java - 谷歌应用引擎 java 上的 HttpOnly cookie

C 当链表为空时删除链表的节点

sql - 如何在Hadoop中使用sql将所有联合与多个表一起使用?

scala - 如何获取余积的所有可能成员

java - 当我在 GSON(使用枚举)上运行 Proguard 时应用程序崩溃

c - 数组和链表哪个性能最好(就访问速度而言)