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

  • SEARCH
  • Home
  • 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 7032193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:54:10+00:00 2026-05-28T00:54:10+00:00

Here’s the method public void sortStudentsAlphabeticallyByFirstName() { StudentNode unsorted = tail; StudentNode current =

  • 0

Here’s the method

public void sortStudentsAlphabeticallyByFirstName()
{
    StudentNode unsorted = tail;
    StudentNode current = header;
    while(current != null)
    {
        while(current != unsorted)
        {
            int result = (current.nextNode().getFirstName()).compareToIgnoreCase(current.getFirstName());
            if(result < 0)
            {
                StudentNode temp = current;
                current = current.nextNode();
                current.setNext(temp);
            }
        }
        current = current.nextNode();
        unsorted = unsorted.prevNode();
    }
}

The problem is that when executed it just keeps running and doesn’t stop and I’m not sure where the problem is.

  • 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-28T00:54:10+00:00Added an answer on May 28, 2026 at 12:54 am

    Consider our Link List has A, C, B and D nodes. say as you enter in your second while loop

    current = C;
    

    so using this code :

    temp = current; // i.e. temp = C as current = C
    current = current.next(); // say current = B now and temp = C
    current.setNext(temp); // here B's next is set to C
                           // but you forgot A's next is C in the example, now since B 
                           // is taking it's place so A's next must point to B
                           // B's next must point to C and C's next must point to D.
    

    So seems like you forgot these steps,

    When you are moving current to the next node after that, temp and current will swap. But the one previous to temp i.e. A in the example must point to B, which is being swapped with C. Since B was pointing to D before, now after swapping C must point to D (this part you missed) and B must point to C (that’s what you did on the third line.)

    EDIT
    Whole working code has been added for more information.

    import java.io.*;
    
    class Node
    {
    public Node previous;
    public String value;
    public Node next;
    }
    
    public class LinkedList
    {
    private BufferedReader br ;
    private String str; 
    private int totalNodes;
    
    private  Node current, previous, temp, head, tail; 
    
    public LinkedList()
    {
        br = new BufferedReader(new InputStreamReader(System.in));
        current = previous = temp = head = tail = null;
        totalNodes = 0;
    }
    
    public static void main(String[] args)
    {
        LinkedList ll = new LinkedList();
        ll.menu();
    }
    
    private void menu()
    {
        boolean flag = true;
        int choice = 0;
        while(flag)
        {
            System.out.println("--------------------------------------------------");
            System.out.println("---------------------MENU-----------------------");
            System.out.println("Press 1 : To ADD Node at the END.");
            System.out.println("Press 2 : To ADD Node at the BEGINNING.");
            System.out.println("Press 3 : To Add Node in BETWEEN    the List.");
            System.out.println("Press 4 : To  SORT the List");
            System.out.println("Press 5 : To DISPLAY the List.");
            System.out.println("Press 6 : To EXIT the Program.");
            System.out.println("--------------------------------------------------");
            System.out.print("Please Enter your choice here : ");
            try
            {
                str = br.readLine();
                choice = Integer.parseInt(str);
                if (choice == 6)
                {
                    flag = false;
                }
                accept(choice);
            }
            catch(NumberFormatException nfe)
            {
                System.out.println("OUCH!, Number Format Exception, entotalNodesered.");
                nfe.printStackTrace();
            }
            catch(IOException ioe)
            {
                System.out.println("OUCH!, IOException, entotalNodesered.");
                ioe.printStackTrace();
    
            }
        }
    }
    
    private void accept(int choice)
    {
        switch(choice)
        {
            case 1:
                addNodeToListAtStart();
                break;
            case 4:
                sortListBubble();
                break;
            case 5: 
                displayList();
                break;
            case 6:
                System.out.println("Program is Exiting.");
                break;
            default:
                System.out.println("Invalid Choice.\nPlease Refer Menu for further Assistance.");
        }
    }   
    
    private void addNodeToListAtStart()
    {
        if (head != null)
        {
            current = new Node();
            System.out.print("Enter value for the New Node : ");
            try
            {
                str = br.readLine();
            }
            catch(NumberFormatException nfe)
            {
                System.out.println("OUCH!, Number Format Exception, entotalNodesered.");
                nfe.printStackTrace();
            }
            catch(IOException ioe)
            {
                System.out.println("OUCH!, IOException, entotalNodesered.");
                ioe.printStackTrace();              
            }
            current.previous = tail;
            current.value = str;
            current.next = null;
            tail.next = current;
            tail = current;
        }
        else if (head == null)
        {
            current = new Node();
            System.out.print("Enter value for the New Node : ");
            try
            {
                str = br.readLine();
            }
            catch(NumberFormatException nfe)
            {
                System.out.println("OUCH!, Number Format Exception, entotalNodesered.");
                nfe.printStackTrace();
            }
            catch(IOException ioe)
            {
                System.out.println("OUCH!, IOException, entotalNodesered.");
                ioe.printStackTrace();              
            }
            current.previous = null;
            current.value = str;
            current.next = null;            
            head = current;
            tail = current;
        }
        totalNodes++;
    }
    
    private void displayList()
    {
        current = head;
        System.out.println("----------DISPLAYING THE CONTENTS OF THE LINKED LIST---------");
        while (current != null)
        {
            System.out.println("******************************************");
            System.out.println("Node ADDRESS is : " + current);
            System.out.println("PREVIOUS Node is at : " + current.previous);
            System.out.println("VALUE in the Node is : " + current.value);
            System.out.println("NEXT Node is at : " + current.next);
            System.out.println("******************************************");
            current = current.next;
        }
    }
    
    private boolean sortListBubble()
    {
        // For Example Say our List is 5, 3, 1, 2, 4
        Node node1 = null, node2 = null; // These will act as reference. for the loop to continue
        temp = head;    // temp is set to the first node.   
    
        if (temp == tail || temp == null)
            return false;
    
        current = temp.next; // current has been set to second node.
    
        for (int i = 0; i < totalNodes; i++) // this loop will  run till whole list is not sorted.
        {
            temp = head; // temp will point to the first element of the list.
            while (temp != tail) // till temp won't reach the second last, as it reaches the last element loop will stop.
            {
                if (temp != null)
                    current = temp.next;
                while (current != null) // till current is not null.
                {
                    int result = (temp.value).compareToIgnoreCase(current.value); 
                    if (result > 0) // if elment on right side is higher in value then swap.
                    {
                        if (temp != head && current != tail) // if nodes are between the list.
                        {
                            current.previous = temp.previous;
                            (temp.previous).next = current;
                            temp.next = current.next;
                            (current.next).previous = temp;                     
                            current.next = temp;
                            temp.previous = current;
                        }
                        else if (current == tail) // if nodes to be swapped are second last and last(current)
                        {
                            temp.next = current.next;
                            current.previous = temp.previous;
                            if (temp.previous != null)
                                (temp.previous).next = current;
                            else
                                head = current;
                            temp.previous = current;
                            current.next = temp;
                            tail = temp;
                        }
                        else if (temp == head) // if the first two nodes are being swapped.
                        {
                            temp.next = current.next;                       
                            (current.next).previous = temp;
                            current.previous = temp.previous;
                            temp.previous = current;
                            current.next = temp;
                            head = current;
                        }   
                        current = temp.next; // since swapping took place, current went to the left of temp, that's why
                                                       // again to bring it on the right side of temp.
                    }
                    else if (result <= 0) // if no swapping is to take place, then this thing
                    {
                        temp = current;  // temp will move one place forward
                        current = current.next; // current will move one place forward
                    }                                       
                }
                if (temp != null)
                    temp = temp.next;
                else // if temp reaches the tail, so it will be null, hence changing it manually to tail to break the loop.
                    temp = tail;
            }
        }
        return true;
    }
    }
    

    Hopefully that might help.

    Regards

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

Sidebar

Related Questions

Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
Here's my function: static Map AddFormation(Map _map, Tile tile, int x, int y, int
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see

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.