Current Position:Home > Problems sorting custommade linkedlist

Problems sorting custommade linkedlist

Update:11-30Source: network consolidation
Advertisement
Hi guys, I have a problem (obviously).
Its an assignment for school and I'm supposed to write my own LinkedList (called PhoneDirectory) instead of using the already defined one in java. After a fairly large amount of time I succeeded with that.
To get an A though I need to also create a subclass of the PhoneDirectory called SortedPhoneDirectory with the only difference that the insert method automatically places the new Person in the right place in the list based on name. I have now succeded with that as well, but something is wrong, which brings me to my problem:
When I access the methods in the PhoneDirectory class nothing happens - nothing at all. WHY :O:P
Here are all my classes
The class PhoneDirectory:
The class PhoneDirectory is a Linked List containing Person objects.
class PhoneDirectory
    private Node header;
    public PhoneDirectory()
        header = new Node(null );
    public void insert( Person o)
        header = new Node ( o, header);
    public void findWithName(String name)
        name = name.toLowerCase();
        Node runner = header;
        Person tmp;
        int i = 0;
        while(runner.element != null)
            tmp = get(i);
            if(tmp.getName().toLowerCase().contains(name))
                System.out.println(tmp);
            runner = runner.next;
            i++;
    public void findWithAdress(String adress)
        adress = adress.toLowerCase();
        Node runner = header;
        Person tmp;
        int i = 0;
        while(runner.element != null)
            tmp = get(i);
            if(tmp.getAdress().toLowerCase().contains(adress))
                System.out.println(tmp);
            runner = runner.next;
            i++;
    public void findWithPhone(String phone)
        phone = phone.toLowerCase();
        Node runner = header;
        Person tmp;
        int i = 0;
        while(runner.element != null)
            tmp = get(i);
            if(tmp.getPhoneNumber().toLowerCase().contains(phone))
                System.out.println(tmp);
            runner = runner.next;
            i++;
    public void remove(int val)
        if(header == null)
            System.out.println("Its empty!");
        else if(val == 0)
            header = header.next;
        else
            Node runner = null;
            Node previous = null;
            runner = header.next;
            previous = header;
            int i = 1;
            while(runner.element != null)
                if(i == val)
                    previous.next = runner.next;
                previous = runner;
                runner = runner.next;
                i++;
    public void removeFirst()
        if(header == null)
            System.out.println("Its empty!");
        else
            header = header.next;
    public void print()
        Node tmp = header;
        while(tmp.element != null)
            System.out.println(tmp.element);
            tmp = tmp.next;
    public Person get(int val)
        int i = 0;
        boolean abort = false;
        Node tmp = header;
        while(val != i)
            tmp = tmp.next;
            i++;
            if(tmp.next == null)
                abort = true;
                break;
        if(abort)
            return null;
        else
            return tmp.element;
    public int size()
        int size = 0;
        Node tmp = header;
        while(tmp.element != null)
            tmp = tmp.next;
            size++;
        return size;
}And here's the SortedPhoneDirectory:
class SortedPhoneDirectory extends PhoneDirectory
    private Node header;
    public SortedPhoneDirectory()
        header = new Node(null);
    public void insert(Person o)
        Node newNode = new Node();
        newNode.element = o;
        if(header.element == null)
            header = newNode;
        else if(header.element.getName().compareToIgnoreCase(o.getName()) >= 0)
            newNode.next = header;
            header = newNode;
        else
            Node runner = header.next;
            Node previous = header;
            while(runner != null && runner.element.getName().compareToIgnoreCase(o.getName()) < 0)
                previous = runner;
                runner = runner.next;
            newNode.next = runner;
            previous.next = newNode;
}The class Node that the linkedlist contains:
class Node
    public Person element;
    public Node next;
      // Constructors
    public Node()
        next = null;
        element = null;
    public Node( Person theElement )
        this( theElement, null );
    public Node( Person theElement, Node n )
        element = theElement;
        next    = n;
}The class Person that the Nodes are containing:
class Person implements Comparable
    private String name;
    private String address;
    private String phone;
    public Person( String n, String ad, String p )
        name = n; address = ad; phone = p;
    public String toString( )
        return getName( ) + "\t"+ getAdress()+ "\t" + getPhoneNumber( );
    public final String getName( )
        return name;
    public final String getAdress( )
        return address;
    public final String getPhoneNumber( )
        return phone;
    public int compareTo( Object other)
        Person p=(Person)other;
        if(name.compareTo(p.getName())<0)
            return -1;
        else if(name.compareTo(p.getName())>0)
            return 1;
        else
            return 0;
}And last my main program:
public class telefonKatalog
    static String name;
    static String address;
    static String phone;
    public static void main(String[] args)
        SortedPhoneDirectory catalog =new SortedPhoneDirectory();
        catalog.insert(new Person("Donald","Paris", "10245630"));    //I just use these for testing
        catalog.insert(new Person("Jacob","London", "63294063"));
        catalog.insert(new Person("Max","Rome", "63215371"));
        for(;;)
            System.out.println("\n\nMain Menu\n____________________\n");
            System.out.println("1. Add a new person to the Phone Directory.");
            System.out.println("2. Find a person in the Phone Directory.");
            System.out.println("3. Remove a person from the Phone Directory");
            System.out.println("4. Print the whole Phone Directory");
            System.out.println("5. Exit.\n");
            char choice = Keyboard.readChar();
            switch(choice)
                case '1':
                    System.out.println("\nAdd a new person to the Phone Directory\n______________________________________\n");
                    System.out.println("Name: ");
                    name = Keyboard.readString();
                    System.out.println("\nAdress: ");
                    address = Keyboard.readString();
                    System.out.println("\nPhone Number: ");
                    phone = Keyboard.readString();
                    catalog.insert(new Person(name, address, phone));
                    System.out.println("\nThe person has been added.\n");
                    break;
                case '2':
                    System.out.println("\nFind a person in the Phone Directory.\n_________________________________\n");
                    System.out.println("1. Find by name.");
                    System.out.println("2. Find by address.");
                    System.out.println("3. Find by phone number\n");
                    choice = Keyboard.readChar();
                    switch(choice)
                        case '1':
                            System.out.println("\nFind by name.\n___________________\n");
                            System.out.println("Write the name of the person you are looking for:\n");
                            name = Keyboard.readString();
                            System.out.println();
                            catalog.findWithName(name);
                            break;
                        case '2':
                            System.out.println("\nFind by address.\n___________________\n");
                            System.out.println("Write the address:\n");
                            address = Keyboard.readString();
                            System.out.println();
                            catalog.findWithAdress(address);
                            break;
                        case '3':
                            System.out.println("\nFind by Phone Number.\n___________________\n");
                            System.out.println("Write the phone number:\n");
                            address = Keyboard.readString();
                            System.out.println();
                            catalog.findWithPhone(address);
                            break;
                        default:
                            System.out.println("\nUnvalid character!\n");
                            break;
                    break;
                case '3':
                    System.out.println("\nRemove a person from the Phone Directory.\n____________________________________\n");
                    System.out.println("Write the name of the person you want to remove.\n");
                    String strChoice = Keyboard.readString();
                    boolean result = false;
                    for(int i = 0; i < catalog.size(); i++)
                        name = catalog.get(i).getName();
                        if(name.contains(strChoice))
                            catalog.remove(i);
                            System.out.println(name + " has been removed");
                            result = true;
                    if(!result)
                        System.out.println("There are no such person");
                    break;
                case '4':
                    System.out.println("\nThe whole Phone Directory: \n_____________________________\n");
                    catalog.print();
                    System.out.println("\n_____________________________\n");
                    break;
                case '5':
                    System.exit(0);
                default:
                    System.out.println("\nUnvalid character!\n");
}Its pretty long, sorry about that.. =)
In the main program it all workes perfectly apart from the sorting if you change
SortedPhoneDirectory catalog = new SortedPhoneDirectory(); into PhoneDirectory catalog = new PhoneDirectory();
What is wrong about this code?? :O:(
Edited by: YZF-R1 on 2008-maj-23 16:55

The Best Answer

Advertisement
YZF-R1 wrote:
now thats just mean =( Why is that mean? I mean, you didn't learn something soon enough, and I am mean for not spoon feeding you the answer so that you can cheat by handing in work that is not your own? You already did that you just told us! This is called plagiarism and could get you expelled from school.
I will sit down and learn all of that later when I have time, Sure. You should have learnt it sooner.
but my deadline is tomorrow and its the last project of the course, so the teacher thought he would make up something extra. When I asked him if he could help me with my problem he couldn't even fix it, he thought everything looked as it was supposed to and so on.. :'( Come on, pleease :'(Christ, stop that!
  • Problems sorting custommade linkedlist Update:11-30

    Hi guys, I have a problem (obviously). Its an assignment for school and I'm supposed to write my own LinkedList (called PhoneDirectory) instead of using the already defined one in java. After a fairly large amount of time I succeeded with that. To ge

  • Problem sorting data in a file? Update:10-11

    I have a problem sorting this file alphabetically by second name. Basically, my method sorts each column alphabetically but i would like to sort the file according to the second name. I really really need help. Thanks File: Moe     Carl Saul     Serg

  • Problem sorting list with virtual layout = false (and also with true) Update:10-11

    Hi, I've a problem sorting a list... or better... I've a problem showing the sorted list ;-) I've a list of xml item. The list is shown with an item renderer. my needs: a button to refresh data and a button to sort data. useVirtualLayout = false -> r

  • I'm having problems Sorting Objects Update:11-30

    Hello everyone! I'm having problems sorting objects. Here is what I got at moment... I'm trying using "Collections.sort" with objects, and I think my problem is there but I don't know how to do in other way the sorting. Some help will be appreci

  • Collections problem - Sorting object (not key) in treemap Update:11-30

    Hello everyone, I don't know if TreeMap is the right thing for what I want to do, but I haven't found anything better yet. What I want to do is have a tree map like that index -- value (Double) 1        500 2        450 3        500   (Multiple value

  • Problem sorting items in finder window Update:10-11

    I've searched other posts and seen that others are having similar issues however, I can't a solution to my problem. In a finder window in list view, in the upper part of the window, I have Name, Date Modified, Size and Kind.  Before I upgraded to Mav

  • Problems sorting larger sets of XMLType columns/tables Update:11-30

    Hi, Here's something I tried to wrap my head around: I'm using Oracle 11g r1 for Windows Server 2003. I've imported a fairly large set of XML files into a temporary XMLType table. Now I want to sort the contents of the table and put them into another

  • Problem Sorting Photos in Slideshow Update:11-30

    I created a slideshow, putting the photos in the desired order. Now, for a reason I cannot understand, that order is completely reversed--front to back. When I go to View-Sort, everything is grayed out. Is there a keystroke that will reverse the orde

  • Problem sorting JTable with custom cell editor Update:11-30

    Greetings, I have created a JTable with a JComboBox as the cell editor for the first column. However, I couldn't simply set the default cell editor for the column to be a JComboBox, since the values within the list were different for each row. So ins

  • Problem with making LinkedList public Update:11-30

    public LinkedList list= new LinkedList(); ???? I keep getting errors, like "statement expected" please helpDear tony874489, You cannot declare a public object in a method. You have to remove the public if you want it in the method. If you want t