-2

I'm working on a linked list code and wanted to access head.next.next.data. Is there a means where I can store .next.next.data to a variable x so that I can make the call head.x and retrieve the result provided I have data populated in the list? I want to implement

public class CustomSOL<E> implements SOL<E>  
{
    private static class customNode<E>
    {
        private customNode<E> next;
        private E data;

        public customNode(E dataItem)
        {
            this.data = dataItem;
            this.next = null;
        }

        private customNode(E dataItem, customNode<E> nodeRef)
        {
            this.data = dataItem;
            this.next = nodeRef;
        }
    }

    private customNode<E> head;
    private int size;

    public CustomSOL()
    {
        head = null;
        size = 0;
    }


    public void solLookup(int amount)
    {
        String s = "next";
        StringBuilder sb = new StringBuilder(14);
        for(int i = 0; i < amount; i++)
            sb.append(".").append(s);

        System.out.println(head.s.data);
    }
}

so in main if I have

        CustomSOL<String> list = new CustomSOL<String>();
        list.solAdd("Apple");
        list.solAdd("Orange");
        list.solAdd("Banana");
        list.solAdd("Strawberry");
        list.solAdd("Papaya");

and I call list.solLookup(3):

String s = "next.next.next";   
System.out.println(head.s.data);

I want to see Strawberry printed out, which it obviously won't because s is string. But is there a way, just like how javascript and PHP have variable functions, to replace all those next calls with a variable s? I'm using a string because I can use the amount parameter in solLookup to create the appropriate next call notations. Thank you!

  • What `head` implementation is? – ivanjermakov Oct 12 '19 at 17:52
  • head is a pointer to the first node in the linked list. – Abenezer Ayana Oct 12 '19 at 17:55
  • There is no pointers in Java. What type of `head` is? – ivanjermakov Oct 12 '19 at 17:56
  • I know, head is a node (but when you learn linked lists it is drawn at the beginning like a pointer. Have you worked with linked lists before? – Abenezer Ayana Oct 12 '19 at 17:58
  • As I understand your question, with this code you want to print in console field of object `head` called `x`. You misunderstanding whole concept of OOP. See the difference between local variables and fields: https://stackoverflow.com/questions/20671008/what-is-the-difference-between-a-local-variable-an-instance-field-an-input-par – ivanjermakov Oct 12 '19 at 18:00
  • I'm implementing a custom generic-type single linked list (not Java's double linked list). So if my linked list has {"a","b","c"}, head.next.data should give me a "b". – Abenezer Ayana Oct 12 '19 at 18:01

2 Answers2

1

head.next.next.data is (presumably) a reference to some data. The Java compiler turns that phrase into executable code to fetch the data you want.

".next.next.data" is just a sequence of Unicode characters in the program, whether literally presented or built at runtime. It is not (in simple programming) possible to turn that into a runtime reference to some data that just happens to be named similarly to the characters in the string. Doing that is a job for a Java compiler, and the compiler has finished its job before your program runs.

There are advanced techniques ("reflection") but I would not recommend them to you at what I assume to be your level of expertise.

To get the k'th level object you simply iterate down the list 'k' times:

public Node get(int k) {
    Node n = head;
    while (k > 0) {
       n = n.next;  // or n.next() if need be
       k--;
    }
    return n;
}

This is simpler than building a string with 'k' occurrences of 'next'.

0

Assume such implementation of linked list:

class List<T> {
    static class Head<T> {
        public T value;
        public Head<T> next;

        public Head(T value) {
            this.value = value;
        }
    }

    public Head<T> head;
}

You can use it like this:

List<String> list = new List<>();

list.head = new List.Head<>("a");
list.head.next = new List.Head<>("b");

System.out.println(list.head.next.value);

There is many ways how you can improve this, but you get the idea.

ivanjermakov
  • 1,131
  • 13
  • 24