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

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




single linked list

 
Reply to this topicStart new topic

single linked list

shag
11 Oct, 2007 - 11:30 AM
Post #1

New D.I.C Head
*

Joined: 18 Sep, 2007
Posts: 4


My Contributions
i am working on a program that takes an interface and inplements it using single linked list --- after that is done, i write a tester program. i havent even started on the tester program yet, and i have some of the implementation program done, but i am not even sure if what i have done so far is correct, and the parts that i have nothing for yet, those are the ones i am unsure about ---------- any help is greatly appreciated --- here is what i have so far


CODE
import java.util.*;
import java.util.text;

public interface List {

     //Appends the specified element to the end of this list and returns the specific position at which it was added.
     public int add(Object o){
                 if (head == null){
                     head = new Node (o, null);
                     count++;
                 }
                 else
                 
                 Node current = head;
                 while (current.link != null){
                     current = current.link;
                 }
                 current.link = new Node (o, null);
                 count++;
     }
          
     //Inserts the specified element at the specified position in this list.
     public void add(int index, Object o){
         if (head == null){
                     head = new Node (o, null);
                     count++;
                 }
                 else
                 
                 Node current = head;
                 while (current.link != null){
                     current = current.link;
                 }
                 current.link = new Node (o, null);
                 count++;
     }
         
     }
          
     //Removes all of the elements from this list.
     public void clear(){
     }
    
    
          
     //Returns true if this list contains the specified element.
     public boolean contains(Object element){
                           return false;
     }
         
     //Compares the specified object with this list for equality.
     public boolean equals(Object element){
                           return false;
     }
          
     //Returns the element at the specified position in this list.
     public Object get(int index){
                             return info;
     }
          
     //Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
     public int indexOf(Object element){
     }
    
          
     //Returns true if this list contains no elements.
     public boolean    isEmpty(){
         if (head == null)
                           return false;
     }
          
     //Removes the element at the specified position in this list.
     public Object    remove(int index){
                          if (move == n) {
                   move = n.getNext();
               }
               else {
                 Node tmp = move;
               while (tmp.getNext() != n) tmp = tmp.getNext();
               tmp.setNext( n.getNext() );
      }
              
      
          
     //Replaces the element at the specified position in this list with the specified element.
     public Object    set(int index, Object o){
                             return null;
     }
          
     //Returns the number of elements in this list.
     public int    size(){
                     return -1;
     }
          
     //Returns an array containing all of the elements in this list in proper sequence
     public Object[]    toArray(){
                     return null;
     }
    
     // Returns a String representation of the list.
       public String toString()
        {
        String result = "";
        NumListNode current = NumListNode;
        while (current != null)
          {    
            result += current.numbers + "\n";
            current = current.next;
          }
          return result;

    
}
    

User is offlineProfile CardPM
+Quote Post

hyperionza
RE: Single Linked List
11 Oct, 2007 - 12:43 PM
Post #2

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 30


My Contributions
First up, you've defined the above code as an interface, not as a class... an interface only contains definitions of methods which when a class says it implements an interface, the class must contain the methods, with implementatiom, of the methods defined in the interface eg:
CODE

public interface myInterface
{
    int getInteger();
    String whoseYourDaddy(int NameIndex);
}

public class myClass implements myInterface
{
    public int getInteger() //implements the method defined in myInterface
    {
        return 43;
    }

    public String whoseYourDaddy(int NameIndex) //implements the other method in myInterface
    {
         return Names[NameIndex];
    }

    public myClass() //a contructor
    {
          //...
    }

    public void doNothing()
    {
        //a random method
    }

    String[] Names;
}


it would also help you to declare head, move, count and n. might also help you to correctly close it off after your tostring() method, youre missing a brace } at the end of it all.

Youve also cut and pasted the public void add(int index, Object o) straight out of the method above it. Instead you are going to want to keep track of the current index of the Node. If current == null before the requested index you just add it to the end of the list, otherwise you add it before the Node with the index ofindex.

Looking at the remove method i think you forgot to post the first half of the method... neither n nor move are defined and im pretty sure youre using them as local variables? No offence but this method is kinda bad. Ill point out some wrong points and why but i think this one needs you to think it through, which is why im telling you and not giving you the solution.

CODE

//Removes the element at the specified position in this list.
     public Object    remove(int index){
                          if (move == n) { // n and move are undefined this will break your program, im also going to assume that 'move'is going to be the node after 'n' which presumably is the node at position 'index'
                   move = n.getNext();
               }
               else {
                 Node tmp = move;
               while (tmp.getNext() != n) tmp = tmp.getNext();// youve assigned tmp to be the node after 'n' already so all this will do is search through the remainder of the List and then throw an Exception, i think what you were trying to do was find the node before 'n'? so try assigning tmp to 'head' instead of 'move' and go from there.
               tmp.setNext( n.getNext() ); //this line will work but if my earlier assumptions are correct saying [i]tmp.setnext(move);[/i] would be more efficient
      }



finally this method expects you to return something, which you dont do. assuming again that n is the node at position index then add in
CODE
return n;
at the end.

i dont think thats all that could be said about the code but unfortunately my time is up and i need to get back to work. hope that will give you a few pointers in the right direction.

If youre still working on it post an update when next you have one and someone will look at it if i dont.

good luck

This post has been edited by hyperionza: 11 Oct, 2007 - 02:03 PM
User is offlineProfile CardPM
+Quote Post

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

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