Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9209225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:47:32+00:00 2026-06-18T00:47:32+00:00

Im trying to write the methods to union which can be described: if A,

  • 0

Im trying to write the methods to “union” which can be described: if A, B, C are sets, has the form C = A.union(B). Union returns a Set that contains all the elements in set A and B, but only list duplicates once.

My idea for this method is to traverse set A and add all of its elements to the union set, then traverse set B, if an element of set B is already present in the union set, then do not insert it to result, otherwise insert all to the union set.

This is complicated for a beginner like me since I want to include all 3 lists into the method (which I get a bunch of errors). I already wrote some methods in my SLinkedList class to check and add element but the parameters take an object from a Node

/** 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 class

/** 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; }

}

*update*
The question is if there is a second list involved public SLinkedList union (SLinkedList secondSet) , what syntax should i use to traverse set B and check if an element of set B is already present in result then do not insert it to result, otherwise insert. Do I need to creat a node for set B and traverse it?, there may be a compare method to compare the 2 sets outside of the union method?

Please help. Thanks all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:47:33+00:00Added an answer on June 18, 2026 at 12:47 am
    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a unit test that will loop through all action methods
I'm trying to write IL that calls methods in mscorlib, but I can't figure
I'm trying to write a plugin that aliases some methods in ActiveRecord in the
I'm trying to write a method that will compute all permutations of a power
I'm trying to write a method that removes all non alphabetic characters from a
I'm trying to write a web app that needs to expose some methods that
I'm trying to write a generic class in Java. A few methods in that
I am trying to write documents for methods that use design patterns in PHP.
I am trying to write a method that loops and repeatedly calls other methods.
I am trying to write a small ruby script that will have two methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.