How to implement my own LinkedList<LinkedList> data structure in Java?

coneyhelixlake

I am working on something which would make it easier if several of the objects I use would be written by me (instead of using Java's libraries). I am currently stuck at implementing an ArrayList<ArrayList> object (which we can call MyHashTable) using my LinkedList class and my Node class. Here is my correctly implemented LinkedList class so far:

public class LinkedList{
    Node start;
    int length;

    public LinkedList(){
        start = new Node();
        length= 0;
    }

    public void addNode(Node node){
        Node s= start;
        while (s.next != null){
            s= s.next;
        }
        s.next= node;
        length++;
    }

    public int getLength(){
        return length;
    }

    public boolean findNode(Node node){
        Node s= start;
        while(s.next != null){
            if (s == node){
                return true;
            }
            s.next= node;
        }
        return false;
    }

}

So I am having trouble modifying this class to accept Java generics (so I could make a Linked List of Linked Lists instead of simply Linked List of Nodes). Let me know if I should provide how my Node class looks.

Radiodef

A generic LinkedList just substitutes the type of the value. You don't show your Node class so I don't understand how you're using it. Here is a generic linked list:

class LinkedList<E> {
    static class Node<E> {
        E value;
        Node<E> next;

        Node(E value) {
            this.value = value;
        }
    }

    Node<E> head = new Node<E>(null);
    Node<E> tail = head;
    int size;

    void add(E value) {
        tail = tail.next = new Node<E>(value);
        size++;
    }

    E get(int index) {
        if(index < 0 || size <= index)
            throw new OutOfBoundsException(index);

        Node<E> node = head.next;
        while(index > 0) {
            node = node.next;
            index--;
        }

        return node.value;
    }
}

But I'm not sure what you mean by make that in to an ArrayList. An ArrayList is completely different, it is an array that resizes itself automatically:

class ArrayList<E> {
    Object[] array = new Object[10];
    int size;

    void add(E value) {
        if(size >= array.length) {
            array = Arrays.copyOf(array, (int)(size * 3L / 2L));
        }

        array[size++] = value;
    }

    E get(int index) {
        return (E)array[index];
    }
}

And while I suppose you could hack together a hash table by using a multidimensional array, I don't recommend it. You cannot just go and instantiate an array with 2^32 elements so that means you have to manage intersections. I don't see how an ArrayList<ArrayList> could ever be a working hash table. Here is a simple hash table implementation similar to the Java one. The table is an array of linked lists.

class HashTable<K, V> {
    static class Entry<K, V> {
        K key;
        V value;

        Entry<K, V> next;

        Entry(K key, V value) {
           this.key = key;
           this.value = value;
        }
    }

    Entry[] table = new Entry[8];
    int size;

    void put(K key, V value) {
        Entry<K, V> entry = table[indexFor(key)];
        while(entry != null) {
            if(entry.key.equals(key)) {
                entry.value = value;
                return;
            }

            entry = entry.next;
        }

        resizeIfNeeded();

        addEntry(new Entry<K, V>(key, value));
        size++;
    }

    void addEntry(Entry<K, V> newEntry) {
        int index = indexFor(newEntry.key);
        Entry<K, V> entry = table[index];

        if(entry == null) {
            table[index] = newEntry;

        } else {
            while(entry.next != null)
                entry = entry.next;

            entry.next = newEntry;
        }
    }

    void resizeIfNeeded() {
        if(size < table.length)
            return;

        Entry[] old = table;
        table = new Entry[old.length << 1];

        for(Entry<K, V> entry : old) {
            while(entry != null) {
                addEntry(entry);
                Entry<K, V> next = entry.next;
                entry.next = null;
                entry = next;
            }
        }
    }

    V get(K key) {
        Entry<K, V> entry = table[indexFor(key)];
        while(entry != null) {
            if(entry.key.equals(key))
                return entry.value;

            entry = entry.next;
        }

        return null;
    }

    int indexFor(K key) {
        return key.hashCode() & table.length - 1;
    }
}

As I mentioned in my comment, this sounds like an XY problem. I don't see how this is easier than using the Java data structures. Perhaps you have a different question to ask.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Traversing,Insertion and Deletion in LinkedList data structure in java

From Dev

Java linkedlist - How to copy data?

From Dev

How can I implement append and deleteNode methods for a LinkedList implementation in Java?

From Dev

how can I implement iterable for LinkedList<LinkedList<obj>>

From Dev

how can I implement iterable for LinkedList<LinkedList<obj>>

From Dev

My LinkedList structure isn't functioning in C

From Dev

Void Method to Reverse Own Implementation of Java LinkedList

From Dev

Save data from linkedlist in java?

From Dev

How to implement priority queue with unordered linkedlist

From Dev

How to use LinkedList class in java?

From Dev

How to create my own efficient data structure?

From Dev

Java insert LinkedList into existing LinkedList

From Dev

why my java LinkedList is added by the same values

From Dev

How to sort own set that based on linkedList without any standard java libraries?

From Dev

Is linkedList a stack? what is the best implementation of stack data structure

From Dev

how to choose or write my own java data structure allowing multi attributes search

From Dev

How to efficiently remove an element from java LinkedList

From Dev

java - how to delete a node from linkedlist?

From Dev

how do I find the average in a Java LinkedList?

From Dev

java - how to delete a node from linkedlist?

From Dev

How to implement a Set Data Structure in Java?

From Dev

How can I implement my own generic collection in java?

From Dev

How would I find the length of a linkedlist IN a linkedlist?

From Dev

How to apend a linkedlist onto a specific node of a linkedlist?

From Dev

Java multithreading and linkedlist operations

From Dev

Java LinkedList Search

From Dev

Pointer into Java LinkedList Node

From Dev

Generic LinkedList clone in Java

From Dev

Iteration sum in a linkedlist in java

Related Related

  1. 1

    Traversing,Insertion and Deletion in LinkedList data structure in java

  2. 2

    Java linkedlist - How to copy data?

  3. 3

    How can I implement append and deleteNode methods for a LinkedList implementation in Java?

  4. 4

    how can I implement iterable for LinkedList<LinkedList<obj>>

  5. 5

    how can I implement iterable for LinkedList<LinkedList<obj>>

  6. 6

    My LinkedList structure isn't functioning in C

  7. 7

    Void Method to Reverse Own Implementation of Java LinkedList

  8. 8

    Save data from linkedlist in java?

  9. 9

    How to implement priority queue with unordered linkedlist

  10. 10

    How to use LinkedList class in java?

  11. 11

    How to create my own efficient data structure?

  12. 12

    Java insert LinkedList into existing LinkedList

  13. 13

    why my java LinkedList is added by the same values

  14. 14

    How to sort own set that based on linkedList without any standard java libraries?

  15. 15

    Is linkedList a stack? what is the best implementation of stack data structure

  16. 16

    how to choose or write my own java data structure allowing multi attributes search

  17. 17

    How to efficiently remove an element from java LinkedList

  18. 18

    java - how to delete a node from linkedlist?

  19. 19

    how do I find the average in a Java LinkedList?

  20. 20

    java - how to delete a node from linkedlist?

  21. 21

    How to implement a Set Data Structure in Java?

  22. 22

    How can I implement my own generic collection in java?

  23. 23

    How would I find the length of a linkedlist IN a linkedlist?

  24. 24

    How to apend a linkedlist onto a specific node of a linkedlist?

  25. 25

    Java multithreading and linkedlist operations

  26. 26

    Java LinkedList Search

  27. 27

    Pointer into Java LinkedList Node

  28. 28

    Generic LinkedList clone in Java

  29. 29

    Iteration sum in a linkedlist in java

HotTag

Archive