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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:29:29+00:00 2026-06-13T12:29:29+00:00

I’m trying to create a method that will add a node to my linked

  • 0

I’m trying to create a method that will add a node to my linked list. The method takes an int (to specify where the new link should go) and String (because my linked list holds strings). I wrote some code that I thought would add a link at specific point in my list, however when I print my list after supposedly adding a new node, I see that the new node has not been added. I’m pretty surprised because I was careful about testing the behavior of my code as I was writing it, and the add method seems to do what I expect–but the newly printed list isn’t reflecting the changes after adding a new link. Can anyone tell where I’m going wrong :/

ps: the names of the classes and methods are not up for debate, my teacher chose them and that’s how they must stay.

thanks!

Test Linked List

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");

            list.add(3, "mouse");

            list.print();
    }   
}

Linked List 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;

           System.out.print(first.item + " ");
    }

    public String deleteFirst()
    {   
        Link temp = first;
        first = first.next;
        return temp.item;
    }

    public String get(int index)
    {
        Link current = first;
        while (index > 0) 
         {
             index--;
             current = current.next;
         }

         return current.item;           
    }

    public void add(int index , String someString)
    {

        Link current = first;

        while (index>0)
        {
            index--;
            current = current.next;
        }

    Link newLink = new Link(someString);
    newLink.next = current;
    current = newLink;

    }

    public void print()
    {
        System.out.println("-----------PRINTING LIST------------");
        Link current = first;
        while(!(current==null))
        {
            System.out.println(current.item);
            current = current.next;
        }
    }
}

Link Class

public class Link 
{   
    public String item;
    public Link next;

    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-13T12:29:30+00:00Added an answer on June 13, 2026 at 12:29 pm
    newLink.next = current;
    current = newLink;
    

    The above code in your add method of LinkedList class should be: –

    newLink.next = current.next;
    current.next = newLink
    current = newLink;
    

    I hope this might solve your problem. You need to set two next links to insert a node in the middle. One at the current node pointing the next node, and one at the prevoius node pointing to the current node.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
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'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace

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.