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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:57:27+00:00 2026-06-15T19:57:27+00:00

Implementing a Linked List, store up to 10 names, ordered in first in First

  • 0

Implementing a Linked List, store up to 10 names, ordered in first in First Out. Then implement two methods, one of which to sort it alphabetically by last names. This is where I am having some trouble. Here is what I tried:

  1. Recursion. The method calls two nodes, compare them, swap if needed and then calls itself. Doesn’t work with odd number of names and tends to be full bugs.

  2. Collection but I don’t know enough about it to use it effectively.

  3. Sorting algorithms (ex. bubble sort): I can go though the list but have a hard time getting the nodes to swap.

My question is: What is the easiest way to do this?

public class List
{
    public class Link
    {
        public String firstName;
        public String middleName;
        public String lastName;
        public Link next = null;

    Link(String f, String m, String l)
    {
        firstName = f;
        middleName = m; 
        lastName = l;
    }
}

private Link first_;
private Link last_;

List()
{
    first_ = null;
    last_ = null;
}

public boolean isEmpty()
{
    return first_ == null;
}

public void insertFront(String f, String m, String l)
{
    Link name = new Link(f, m, l);
    if (first_ == null)
    {
        first_ = name;
        last_ = name;
    }
    else
    {
        last_.next = name;
        last_ = last_.next;
    }
}

public String removeFront()
{
    String f = first_.firstName;
    String m = first_.middleName;
    String l = first_.lastName;
    first_ = first_.next;
    return f + " " + m + " " + l;
}

public String findMiddle(String f, String l)
{
    Link current = first_;
    while (current != null && current.firstName.compareTo(f) != 0 && current.lastName.compareTo(l) != 0)
    {
        current = current.next;
    }
    if (current == null)
    {
        return "Not in list";
    }
    return "That person's middle name is " + current.middleName;
}
}

public class NamesOfFriends
{
    public static void main(String[] args)
    {
        List listOfnames = new List();
        Scanner in = new Scanner(System.in);

    for(int i = 0; i < 3; i++)
    {
        if(i == 0)
        {
            System.out.println("Please enter the first, middle and last name?");
            listOfnames.insertFront(in.next(), in.next(),in.next());
        }
        else
        {
            System.out.println("Please enter the next first, middle and last name");
            listOfnames.insertFront(in.next(), in.next(),in.next());
        }
    }

    System.out.println("To find the middle name, please enter the first and last name of the person.");
    System.out.println(listOfnames.findMiddle(in.next(),in.next()));
    }
}

Edit

After working on it a bit, I figured out how to go about sorting it. For that purpose, I am trying to implement a remove method that can remove a node anywhere on the list. While it does compile, it doesn’t do anything when I run the program.

public Link remove(String lastName)
{
    Link current_ = first_;
    Link prior_ = null;
    Link temp_ = null;
    while (current_ != null && current_.lastName.compareTo(lastName) != 0)
    {
        prior_ = current_;
        current_ = current_.next;
    }
    if (current_ != null)
    {
        if (current_ == last_)
        {
            temp_ = last_;
            last_ = prior_;
        }
        else if (prior_ == null)
        {
            temp_ = first_;
            first_ = first_.next;
        }
    }
    else
    {
        temp_ = current_;
        prior_.next = current_.next;
    }
    return temp_;
}
  • 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-15T19:57:28+00:00Added an answer on June 15, 2026 at 7:57 pm

    2: Collections is the easiest, but it seems to be not allowed in your homework

    3: BubbleSort is easy but the worst known sorting algo, however for your homework it probably is ok

    1: This is the same as bubble sort, but is prefered to be done without recursion

    In BubbleSort you loop through your elements again and again till no swaps are neeeded anymore, then you are ready.

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

Sidebar

Related Questions

I'm implementing a quadruple-linked list in Java as a matrix of Node objects, which
I've been implementing a linked list to scrape the rust out of my development
I got a problem here, implementing a Sorted Doubly Linked List of first and
For a project I'm working on, I'm implementing a linked-list data-structure, which is based
For a project I'm working on, I'm implementing a linked-list data-structure, which is based
I am implementing a function to recursively reverse a linked-list, but getting seg-fault. typedef
I'm having trouble implementing a Stack using a linked list with struct. The program
I am implementing my own linked list in Java. The node class merely has
I have an assignment that requires us to implement a doubly linked list class.
I'm implementing a generic Linked List in C struct Node { void* data; struct

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.