Question

I am struggling a bit on how to make multiple sub-lists in a singly-linked list in java, without using java generics. I've read through multiple questions in overflow and most of them implement generics to solve the problem. Essentially I want a structure that makes a linked list with values such as Dog, Cat, and Snake and then make a sub-list for each such as:

Dog ---> Poodle, Retriever

|

v

Cat ---> Siberian

|

v

Snake ---> Cobra, Python

I believe my main problem is in my add method:

public void add(String topList, String botList)
    {
        head = new Node(topList, botList, head);
    }

Where topList: [Dog, Cat, Snake] and botList:[Poodle,Retriever, Siberian, Cobra,Python].

I feel like with this addMethod im just branching off my head node into two different Lists and not actually linking my topList with elements of my botList.

I have also tried:

public void add(String topList, String botList)
    {
        head = new Node(topList, head);
        head = new Node(botList, topList);
    }

But I'm pretty sure that didn't work since I have two String variables in my void add method.

So my question is how would I go about linking my topList elements with my botList elements? Any help or references would be much appreciated.

This is what i have so far:

  import java.util.*;
  public class animal
  {
    Node head;
    public animal()
    {
        head = null;
    }
    public void add(String topList, String botList)
    {
        head = new Node(topList, botList, head);
    }
    public String toString()
    {
        String x;
        StringBuilder sb = new StringBuilder();
        Node p = head;
        while (p != null)
        {
            sb.append(p.topList + "\n " + p.botList +"\n");
            p = p.next;
        } 
        return new String (sb);
    }
    public static class Node
    {
        String topList;
        String botList;
        Node next;
        public Node (String t, String b, Node n)
        {
            topList = t;
            next = n;
            botList = b;
        }
    }
    public static void main(String args[])
    {
        animal list = new animal();
        list.add("Snake", "python");
        list.add("Dog", "poodle");
        list.add("Cat", "siberian");
        System.out.println(list);
    }
   } 

The output is somewhat the one desired, but i know i didn't linked the two lists together. Also i am only able to add one name to the botList and i want to be able to add more.

Was it helpful?

Solution

I Guess you need the following method in Animal class:

public String get(String animal)
    {
        Node temp = head;
        while (temp!=null)
        {
            if (animal.equals(temp.topList))
            {
                return temp.botList;
            }
            temp = temp.next;
        }
        return null;
    }

And in main body it would look something like this:

public static void main(String args[])
{
    Animal list = new Animal();

    list.add("Snake", "python,Cobra");
    list.add("Dog", "poodle,Retriever");
    list.add("Cat", "siberian");
    System.out.println(list);
    System.out.println(list.get("Dog"));//shows poodle,Retriever

}

OTHER TIPS

I may be misunderstanding, but it seems you are structuring this in a slightly odd way. You have two lists: one with major types (dog, cat, snake), and another with all different subtypes (Poodle,Retriever, Siberian, Cobra,Python).

I think, instead, each item in the first list should link to new list, meaning you have four lists overall:

  1. Top list: (Dog, Cat, Snake)
  2. 3 Sublists: (Poodle,Retriever) , (Siberian) , (Cobra,Python)

Think of it like this.

The contents of a singly linked list hold only data and a reference to the next node in the chain. If we treat the second linked list as the data, then we can engineer the nodes to be something like this.

  • At the top layer, every node we create will be of some Animal (Dog, Cat, and Snake).
  • At the data layer, every piece of data we insert will be a linked list containing some AnimalType information about it.

You wouldn't be using generics with this, even though it would be a fair bit cleaner if you did, but I do envision something like this:

public class Animal {
    private String type;
    private AnimalType classification;

    // usual constructor and accessor
}

public class AnimalType {
    private String typeName;

    // usual constructor and accessor
}

public class AnimalNode {
    private Animal name;
    private AnimalTypeList data;
    private AnimalNode next;

    // usual constructor, accessor and mutator
}

public class AnimalList {  // AnimalList and AnimalTypeList are super similar
    private AnimalNode head;
    // operations and logic on list
}


public class AnimalTypeList {
    private AnimalType head;
    // operations and logic on list
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top