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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:26:25+00:00 2026-06-14T12:26:25+00:00

I have been having some extreme difficulties with an assignment that I have been

  • 0

I have been having some extreme difficulties with an assignment that I have been given.

The assignment is to create 4 different data structures, LinkedList, DoublyLinked list, deque, and sorted doublylinked list in java. I have almost everything except for the add method in the sorted one.

I have been trying different things for about 4 hours, and every time I do it, the test cases that I have been given fail.

I’m posting my current code, it doesn’t work how I want it to. If anyone can tell by looking at it, what I need to fix, that would be awesome. I can also post code to other methods and other things.
Thank you!

public void add(T item) {
    if (head.getData() == null) {
        head = new Node(item);
        tail = new Node(null);
        tail.setPrev(head);
        head.setNext(tail);
        count++;
    } else if (((Comparable<T>) head.getData()).compareTo(item) >=0) {
        Node tempNode = head;
        head = new Node(item);
        head.setNext(tempNode);
        tempNode.setPrev(head);
        count++;

    } else {
        Node current = head.getNext();
        for (int i = 1; i < size()  && current.getData() != null; i++) {
            if (((Comparable<T>) current.getData()).compareTo(item) > 0) {
                current = current.getNext();
            } else if  (((Comparable<T>) current.getData()).compareTo(item) <= 0) {
                Node newNode = new Node(item);
                current.getPrev().setNext(newNode);
                newNode.setNext(current);
                newNode.setNext(current.getPrev());
                current.setPrev(newNode);
                count++;
                break;
            } 
        }
        tail = new Node(item);
        current.setNext(tail);
        tail.setPrev(current);
        count++;
        return;
    }
}

EDIT ONE:

Okay, I can fix the casts thing, but that won’t exactly help what I’m trying to accomplish. Sorry about the vagueness or unclear parts about my question. It’s slightly difficult to explain.
I was also given a lot of test cases, for example this one:

 public void testAddAll() {
    SortedDLL<Integer> list = new SortedDLL<Integer>();
    assertEquals(0, list.size());

    Integer[] objects = {32,0,5125,-12};
    Integer[] sorted = {-12,0,32,5125};
    list.addAll(objects);
    assertEquals(4, list.size());

    for (int i = 0; i < objects.length; i++) {
        assertEquals(sorted[i], list.get(i));   
    }
}

When I do this one(using a system.out.println instead of assertEquals) for the last section i got :
(-12, -12)
(0, 0)
(32, 5125)
(5125, 0)
which is not quite right.
I’m just not sure what’s going on. I can’t post all of my tried solutions, because I’ve deleted most of them.

  • 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-14T12:26:26+00:00Added an answer on June 14, 2026 at 12:26 pm

    I find your code hard to read and comprehend. It would be nice if you posted your entire class so we know your exact variable definitions etc.

    The error I could find in your code’s logic is the following:

        tail = new Node(item);
        current.setNext(tail);
        tail.setPrev(current);
        count++;
        return;
    

    This should be changed to something like:

    if ( current == tail ) {
        tail = new Node(item);
        current.setNext(tail);
        tail.setPrev(current);
        count++;
        return;
    }
    

    EDIT: The full code has been removed.

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

Sidebar

Related Questions

I have a PHP application that I have been having some problems with, some
I have been having some trouble loading the CheckBoxList with data. I have been
I am having some trouble with this. We basically have been asked to create
I have been having some issues retrieving JSON data from a WCF service application
Hi, i have been having some problems using JSON data in flash builder lately
I am having some memory leak problems in IE. I have been reading, that
I have been having some real trouble with this for a while. I am
We have been having some debate this week at my company as to how
I'm working on http://lobounce.com.s138810.gridserver.com/ and have been having some problems with the slider which
I have been struggling with this for quite some time having been accustomed to

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.