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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:01:46+00:00 2026-06-17T18:01:46+00:00

So I was given this code, which to my knowledge I am not allowed

  • 0

So I was given this code, which to my knowledge I am not allowed to change:

public void insertionSort () {
    if (head == tail) {
        // empty list is sorted
        return;
    }
    Node nextNode = head.next; // start inserting second node
    while (nextNode != tail) {
        // set insertNode to node to be inserted in this iteration
        Node insertNode = nextNode; 
        // set nextNode to node to be inserted in next iteration
        nextNode = nextNode.next;  

        // find position where insertNode has to be inserted
        Node current = insertNode.prev;
        while (current != null && insertNode.value.compareTo(current.value) < 0) {
            current = current.prev;
        }

        // insert insertNode after current
        insertNode.moveAfter(current);
    }
}

I am not very familiar with linked lists but from what I can tell if the second while loop operates on the first iteration then this code will pass null into moveAfter() So far for moveAfter() I have:

 /**
     * Inserts this node after the specified node. If this and the specified node 
     * refer to the same node in the list, the list remains unchanged. If the 
     * specified node current is null, then this node is inserted at the head of 
     * the list.
     *
     * Precondition: this Node and the specified node are two nodes of this list
     *               this node and the specified node are not equal to the tail
     * 
     * @param node - the node in this list after which this node is inserted
     */
    public void moveAfter (Node node) {
        if(this.prev == null && node.next == null){
            throw new NoSuchElementException();
        }
        if(node.prev == null && node.next==null){
            throw new NoSuchElementException();
        }
        if(this == tail){
            throw new NoSuchElementException();
        }
        if(node == tail){
            throw new NoSuchElementException();
        }

          this.prev.next = this.next;
          this.next = node.next;
          node.next = this;                            
          this.prev = node;
    }
}  

If I am correct that insertionSort() passes null to moveAfter() how can I rectify this and reset “current” to it original value if i can not change insertionSort();

*Side note: Apologies if my question is hard to read or not asked correctly. I seem to have a knack at screwing them up on this website.

  • 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-17T18:01:48+00:00Added an answer on June 17, 2026 at 6:01 pm

    The comments before your moveAfter() method state:

    If the specified node current is null, then this node is inserted at the head of the list.

    Since insertionSort() uses a head variable, I assume this is a member variable which you can also use in moveAfter() to do as the specification states.

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

Sidebar

Related Questions

Given this JavaScript code (which is just a comment referring to a url): //
Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
Given this code: List<string> things = new List<string>(); foreach (string thing in things) {
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I have a code like this below, which gives me a $link that equals
Given this code, tableButton = new JButton(new ImageIcon(80F_FG2015.GIF)); how would I get that String
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
So I was given this code in a folder of bits and pieces that
I'm learning about pointers. Given this code: FILE* from = fopen("in.txt", "r"); FILE* to
I was given this Python code that would calculate an MD5 value for any

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.