1

I have this school assignment that I'm a little confused about.

Here's what it's saying:

"Write a program that uses the technique of 'chaining' for hashing. The program will read in the length of an array which will contain the reference to each linked list that will be generated. Furthermore, all values that are to be stored, is read. The program shall have a separate function for hashing where the index exists. When the program have generated the linked lists, the theoretical 'load factor' is to be calculated and printed out. The whole array should be easily printed out."

The thing that I'm confused about, is the part about the program will read in the length of an array which will contain the reference to each linked list that will be generated. Is it possible to generate multiple linked lists? In that case, how do you do that?

This is the classes I'm told to use:

public class EnkelLenke { 

    private Node head = null; 
    private int numOfElements = 0; 


    public int getNum() 
    { 
        return numOfElements; 
    } 

    public Node getHead() 
    { 
        return head; 
    } 

    public void insertInFront(double value) 
    { 
        head = new Node (value, head); 

        ++numOfElements; 
    } 

    public void insertInBack(double value) 
    { 
        if (head != null) 
        { 
            Node this = head; 

            while (this.next != null) 
                this = this.next; 
                this.next = new Node(value, null); 
        } 

        else 
            head = new Node(value, null); 
            ++numOfElements; 
    } 

    public Node remove(Node n) 
    { 
        Node last = null; 
        Node this = head; 

        while (this != null && this != n) 
        { 
            last = this; 
            this = this.next; 
        } 

        if (this != null) 
        { 
            if (last != null) 
                last.next = this.next; 
            else 
                head = this.next; 
                this.next = null; 
                --numOfElements; 
                return this; 
        } 

        else 
            return null; 
    } 

    public Node findNr(int nr) 
    { 
        Node this = head; 

        if (nr < numOfElements) 
        { 
            for (int i = 0; i < nr; i++) 
                this = this.next; 

            return this; 

        } 

        else 
            return null; 
    } 

    public void deleteAll() 
    { 
        head = null; 
        numOfElements = 0; 
    } 

    public String printAllElements() {
        String streng = new String();

        Node this = head;
        int i = 1;

        while(this != null)
        {
            streng = streng + this.element + " ";
            this = this.findNext(); 

            i++;
            if(i > 5)
            {
                i = 1;
                streng = streng + "\n";


            }

        }

        return streng;
    } 

    public double getValueWithGivenNode (Node n) 
    {

        Node this = head; 

        while (this != null && this != n) 
        { 
            this = this.next; 
        } 

        if (this == n) 
            return this.element;
        else 
            return (Double) null; 

    } 
}

public class Node { 

    double element; 
    Node next; 

    public Node(double e, Node n) 
    { 
        element = e; 
        next = n; 

    } 

    public double findElement() 
    { 
        return element; 
    } 

    public Node findNext() 
    { 
        return next; 
    }

}
3
  • 2
    it would help if you provided an english translation of your code. Commented Apr 18, 2012 at 17:03
  • @Colleen Translating the code now. Thank you for helping! Commented Apr 18, 2012 at 17:09
  • Thanks Alexander, although it looks like Camilla got the help she needed before I saw this :) Commented Apr 18, 2012 at 18:29

1 Answer 1

4

Your data structure will look something like this (where "LL" is a linked list):

i    |  a[i]
-------------------------------
0    |  LL[obj1 -> obj5 -> obj3]
1    |  LL[obj2]
2    |  LL[]
...  |  ...
N-1  |  LL[obj4 -> obj6]

At each array index, you have a linked list of objects which hash to that index.

Is it possible to generate multiple linked lists? In that case, how do you do that?

Yes. Create your array, and initialize each element to a new linked list.

 EnkelLenke[] a = new EnkelLenke[N];
 for ( int i = 0; i < N; i++ ) {
     a[i] = new EnkelLenke();
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.