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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:27:07+00:00 2026-06-13T06:27:07+00:00

I’m trying to make a custom linked list, so far I’ve figured out how

  • 0

I’m trying to make a custom linked list, so far I’ve figured out how to make the general structure and the two easiest methods (insertFirst and deleteFirst). The next thing I want to do is make a get method that takes the index of a link and then returns the string at that location. I don’t have any index or address assigned to each link, so I don’t see how to refer to a specific location in my linked list. I see that if I write first.next I get the second item, and first.next.next I get the third item.. But I need to figure out how to make my index parameter (the one passed into the get method) correlate with the proper location in my list. How can I do this?

Here’s my code:

Test code:

 class LinkedListTest {
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();
        
        list.insertFirst("cat");
        list.insertFirst("dog");
        list.insertFirst("fish");
        list.insertFirst("cow");
        list.insertFirst("horse");
        list.insertFirst("pig");
        list.insertFirst("chicken");
        
        System.out.println(list.get(1));
        
    }
}

My class:

public class LinkedList
{
    private Link first;
    
    
    public LinkedList()
    {
        first = null;
    }
    
    public void insertFirst(String word)
    {
        Link link = new Link(word);
        link.next = first;
        first = link;
    }
    
    public String deleteFirst()
    {
        Link temp = first;
        first = first.next;
        return temp.toString();
    }
    
    public String get(int index)
    {
        // the following is just to show that I can access different links
        // by adding more .next's after first--- but i need a way to access
        // the correct link based on the index passed in
        
        // String second = first.next.item;
        String third = first.next.next.item;
        // String fourth= first.next.next.next.item
        
        return third;
    }
    
}



public class Link
{
    public String item;
    public Link next;
    
    
    //Link constructor
    public Link(String theItem)
    {
        item = theItem;
    }
}
  • 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-13T06:27:08+00:00Added an answer on June 13, 2026 at 6:27 am

    Assuming that get(0) returns you the first element:

    If you want to put this method inside public class LinkedList:

        public String get(int index)
        {
            assert( index >= 0 )
            Link current = this.first;
            while (index > 0) {
                index--;
                current = current.next;
                // Check to see if this is out of bounds of the links
                if (current == Null) {
                    // Since you are returning a String, you can also return
                    // some kind of a flag to say that the index is out of bounds
                    return Null;
                }
            }
            return current.item;       
        }
    

    Alternatively, you can also implement this inside public class Link:, but this is not advisable since it wastes space on your call stack:

        public String get(int index)
        {
            assert ( index >= 0 )
            if ( index == 0 ) {
                return this.item;
            } else {
                index--;
                if ( next == null ) {
                    return Null;
                }
                return next.get(index)
            }
        }
    

    and inside public class LinkedList:

        public String get(int index)
        {
             return first.get(index);
        }
    

    Hope this helps!

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I've tracked down a weird MySQL problem to the two different ways I was
I know there's a lot of other questions out there that deal with this

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.