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 3491912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:41:40+00:00 2026-05-18T11:41:40+00:00

We’ve been asked to implement a Linked Set in java. Below is my attempt,

  • 0

We’ve been asked to implement a Linked Set in java. Below is my attempt, it has all the methods we are asked to write, but the method remove calls a null pointer exception without fail. Try as I might I can’t seem to figure it out, any help much appreciated.

import java.util.*;

class LinkedSet<T> {

 private static class Node<T> {

  private T item;
  private Node<T> next;

  Node(T item, Node<T> next) {

   this.item = item;
   this.next = next;
  }

 }


 private Node<T> head = null;
 private int numItems = 0;

 int size() {

  return (numItems);

 }

 public boolean add(T t) {

  if(contains(t)) return false;

  Node<T> newNode = new Node(t, null); //new node to be added

  if(numItems==0) {

   head = newNode;
   numItems++;
   return true;
  }

  Node<T> temp = head;

  while(temp.next != null) temp = temp.next;
  temp.next = newNode;
  numItems++;
  return true;

 }


 public boolean remove(T t) {

  if(numItems == 0) return false; //check for empty set
  //was tempted to use contains here but would have made it N^2 I think

  Node<T> p = head; //t if present
  Node<T> pPrev = null; //previous node to p

  while(p!=null && !equals(t, p.item)) {

   pPrev = p;
   p = p.next;

  }

  //t is present if node p!= null , node p != null ==> t in node p

  if(p==null) return false;
  else {

   pPrev.next = p.next; //null pointer
   numItems--;
   return true;
  }


 }

 public boolean contains(T t) {


  Node<T> temp = head;

  for(int i = 0; i < numItems; i++) {

   if(equals(temp.item, t)) return true;
   temp = temp.next;
  }

  return false;

 }

 private boolean equals(T t1, T t2) { //t1, t2 may be null

  if(t1!=null) return t1.equals(t2); //learn this
  else return t2 == null; //learn this

 }

 public static void main(String[] args) {

  LinkedSet<Integer> test = new LinkedSet<Integer>();

  test.add(1);
  test.add(2);
  test.add(3);

  for(int i = 0; i < 10; i ++) {

   System.out.println("Testing i = " + i + " - " + test.contains(i));
  }

  System.out.println(); System.out.println(); System.out.println();

  System.out.println(test.remove(1));


 }



}
  • 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-05-18T11:41:40+00:00Added an answer on May 18, 2026 at 11:41 am

    The obvious point is that the first element in the list does not have a previous element. (Some linked list implementations will add a dummy link to handle this more cleanly.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.