Welcome to Dream.In.Code
Become a Java Expert!

Join 149,575 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,695 people online right now. Registration is fast and FREE... Join Now!




help with single linked list

 
Reply to this topicStart new topic

help with single linked list

itsjustme82
10 Oct, 2007 - 11:13 PM
Post #1

New D.I.C Head
*

Joined: 29 Sep, 2007
Posts: 16


My Contributions
Hi i was given an assignment to write a program to display single linked list using generics.
I am very new to java and want to work on it.

i have a code which should implement it
1) to add an element at the last i tried to work like this but gives me an error saying that "abstract method do not specify a body"
what should i do....?
CODE



public class LinkedList <E> {
    int node;
    
    private LinkedList<E> list;
    public LinkedList (){
        list = null;
        
    }
    public interface List <E> extends Iterable<E> {
        public void add(E obj){
        LinkedList node = new LinkedList(obj);
        LinkedList current;
        
        if(list==null)
            list=node;
        else{
            current = list;
            while  ( current.next !=null)
                current = current.next;
            current.next = node;
        }
        }
        
        Iterator<E> iterator();
            
    }

}      



User is offlineProfile CardPM
+Quote Post

1lacca
RE: Help With Single Linked List
11 Oct, 2007 - 01:38 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Could you explain what was your intention with the following lines?:
public interface List <E> extends Iterable<E> {
and
Iterator<E> iterator();
?
The second one is generating the error you encounter, and it is because the compiler thinks that it is a function declaration without a function body.
The first line does however something different than what you expect I think.
User is offlineProfile CardPM
+Quote Post

itsjustme82
RE: Help With Single Linked List
11 Oct, 2007 - 12:22 PM
Post #3

New D.I.C Head
*

Joined: 29 Sep, 2007
Posts: 16


My Contributions
Thanks for writing in..

I have modified the program as below but i have no clue where to start so i thought of starting
1) add elements at the bottom.

I am very new to java and trying to learn .
Thanks for your help.

CODE


import java.util.Iterator;

public interface lList <E> extends Iterable<E> {
    private int data;
    
    /**
     * Add new element to the end of the list.
     * @param obj
     * @postcondition obj is last element of list
     */
    void add(E obj);
    
    /**
     * Attempts to remove value from list.
    
     * @return true if val is an element of the list.
     */
    boolean remove(E val);
    
    /**
     * Test whether parameter is an element of the list.
     * @param obj value for which to test
     * @return true if object is an element of list
     */
    boolean contains(E obj);
    
    /**
     * Test whether list is empty
     * @return true if list has no elements
     */
    boolean isEmpty();
    
    
    int size();
    
    Iterator<E> iterator();
        
}






User is offlineProfile CardPM
+Quote Post

1lacca
RE: Help With Single Linked List
11 Oct, 2007 - 10:04 PM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Do you have to declare your own linked list interface?
Java already has a List interface, you could simply implement it with your linked list class. (As a hint: there is also a class called java.util.AbstractList<E> , that takes care most of the boilerplate code)
Apart from this, what you've written seems to be a good start. Now you have to create a class that Implements this interface (I would think about storing the size of the list in a variable, because it is redundant information, and as such can cause problems, but it can be used for optimization as well. Anyway, putting it into the interface as a variable is not a good idea, but if you define it as a method, like size(), then the implementing class can decide how to calculate it - storing in a variable, or counting the data stored every time).
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month