Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help on Linked List

807600Nov 3 2007 — edited Nov 4 2007
Hey people, i need a small help on my linked list.

Ok, what i'm trying to do is have a search function with a linked list using strings. First i will have a string, i will then compute the ASCII value for that and take MOD 71 lets say. For example, a string has an ASCII value of 216, MOD 71 of this gives me 3. I will then have an array of 71, and each array will point to a linked list. There will be as many linked list as there are of arrays (71). I will then store this string at index [3] in the linked list in which the array points to.

My problem for now is, how do i for example create 71 linked list? So far i can create only 1. To create another is easy, but if i have 71 the code will be too much. Is there an iterator i could use that easily creates 71? Sorry about this, i'm not really good with linked list with Java. Anyway, here is what i have now :).


public class MyLinkedListElement {    
        
        String string;
        MyLinkedListElement next;

        public MyLinkedListElement(String s) {    
            string = s;
            next = null;    
        }

        public MyLinkedListElement getNext() {
            return next;        
        }

        public String getValue() {
            return string;
        }
}
public class MyLinkedList {
      
    public static MyLinkedListElement head;
    
    public MyLinkedList() {    
        head = null; 
    }

   public static void addToHead(String s) {
        MyLinkedListElement a = new MyLinkedListElement(s);
        if (head == null) { 
            head = a; 
        } else {
            a.next = head;
            head = a;        
        }
    }
    
    public static void main(String[] args) {
          String[] arr = {"Bubble", "Angie", "Bob", "Bubble", "Adam"};
          for (int i = 0; i < arr.length; i++) {
                      addToHead(arr);
}
}
MyLinkedListElement current = head;
while (current != null) {
System.out.println(current.getValue());
current = current.next;
}
}
}
I can have an array of strings and easily add them to a linked list.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2007
Added on Nov 3 2007
43 comments
383 views