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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:35:46+00:00 2026-06-15T08:35:46+00:00

My Node class, representing a node of a linked list, is defined like this:

  • 0

My Node class, representing a node of a linked list, is defined like this:

public class Node
{
    Node next;
    String data;

    public Node (String data)
    {
        this.data = data;
    }
}

and I’m using it like this:

Node node, head, tail;
String name; // name to be entered
int count = 0;

// initialize the head to null

head = null;

do
{
    System.out.print ("Enter a name. Type q to end.");
    name = stdin.readLine ();

    // create a new node if the user doesn't type q
    if (!name.equals ("q"))
    {
        node = new Node (name);
        node.next = head;
        count++;

        // update the head to point to the new front of the list
        head = node;
    }
}
while (!name.equals ("q"));  // loop continues until "quit" selected
node = head; 

Suppose I wanted to back the names up to a method in case I modify the original list. How can I do this? Without writing it to a file.

Name is the variable that gets stored in the linked list and after the user presses q I want to modify the list while keeping what the user stored as a back up in case he/she wants to backtrack or see the original list.

  • 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-15T08:35:47+00:00Added an answer on June 15, 2026 at 8:35 am

    So it sounds as though you want to keep a history of the prior names for each element in the linked list. I would suggest that you store an array or linked list within each node of the linked list that shows the prior history of that item. For instance:

    public class Node
        {
        Node next;
        String data;
        LinkedList<String> history;
    
        public Node (String data)
        {
            this.data = data;
        }
    }
    

    You could populate this in many ways, that all really depend upon your use case.

    Also, why implement your own linked list? Java already comes with a linked list implementation (java.util.LinkedList). I’d suggest using this if you need an ordered list of the linked list variety. If you did this, then create a new data structure to be contained within it that has a name and history, then just maintain the history within that, such as:

    public class DataItem
        {
        String data;
        LinkedList<String> history = new LinkedList<>();
    
        public DataItem (String data)
        {
            this.data = data;
        }
    
        public void setData (String data)
        {
            this.history.add(0, this.data);
            this.data = data;
        }
    }
    

    Ultimately, remember that strings are immutable in Java. So, a string cannot be modified. You only need to keep a reference to the prior string somewhere, you don’t need to copy the value.

    To ultimately copy a tree of objects, you need to do what’s called a deep copy, basically going through the full structure and all collections, and cloning each object into a new object.

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

Sidebar

Related Questions

I have a class like the following: class node { public: node* parent; std::list<node*>
I'm creating a doubly-linked list in Objective-C. I would like my node class to
if i have the following node class class Node { public: Node() { next
Here is my Node class. class Node { private: public: T data; Node<T>* left;
I have a Node class: public class Node { private string name; private Point3D
I am trying to implement a class Node representing a node in a directed
class Node(object): def __init__(self, data): self.data = data if __name__ == __main__: a =
public class Node { private Node nextNode; @Override public int hashCode() { //How to
I have a class: class node { public: node& parent; } I want to
I have a C++ class representing a hierarchically organised data tree which is very

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.