对LinkedList进行排序并实现find方法

时间:2015-09-27 20:12:11

标签: java linked-list singly-linked-list

我有一个提供Node类的链表类。

我需要实现一个排序类,按字母顺序排序链表类的内容。

我还需要实现一个find方法,以便find方法返回它匹配的索引的所有数据。

list.addFirst(new Contact1("John Harvey" ,6000, "jh@gmail.com"));
list.addFirst(new Contact1("Cris Irish",2000, "cI@gmail.com"));
"John Harvey" and "Cris Irish" are the names. I want to compare Cris Irish John Harvey and arrange them in alphabetical order.

然后我想使用find方法,以便找到(" Cris Irish")返回:

" Cris Irish",2000," cI@gmail.com"

我想用比较器来做。但是我不知道如何将它作为方法中的参数传递。

这是我的比较班

public class ContactComparator implements Comparator<Contact1>{
@Override
 public int compare(Contact1 a, Contact1 b)
{
    return b.getName().charAt(0)-(a.getName().charAt(0));
}
}

我有一个contact1类,用于定义链表的内容。

import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException; 


package project3;


import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException

public class LinkedList
{
private static Node first;
private int currentSize;
private static LinkedList list = new LinkedList();
/**
 * Constructs an empty linked list
 */
public LinkedList()
{
    first = null;
    currentSize=0;

}

/**
 * Returns the first element in the linked list.
 * @return the first element in the linked list
 */
public Object getFirst()
{
    return first;
}

/**
 * Removes the first element in the linked list.
 * @return the removed element
 */
public Object removeFirst()
{
    if (first == null) { throw new NoSuchElementException(); }
    Object element = first.data;
    first = first.next;
    currentSize--;
    return element;
}

/**
 * Adds an element to the front of the linked list.
 * @param the element to add
 */
public void addFirst(Object element)
{
    Node newNode = new Node();
    newNode.data = element;
    newNode.next = first;
    first = newNode;
    currentSize++;
}

/**
 * A linked list node, holding the data (an object) and a pointer to
 * the next node in the list.
 */
class Node
{
    public Object data;
    public Node next;
    }


/** Reverses Contents in the linked List */  
public void reverse()   {           
    if (first ==null)   {return;}
    Node primary = first;
    Node current = primary;
    Node previous = null;
    while (current!= null)  {
        Node next = current.next;
        current.next= previous;
        previous = current;
        current = next;
    }
    first = previous;
}

/** Gives the size of the linked List*/
public void size()  {
    Node element = first;
    int elementIndex = 0;
    if (first == null)  {System.out.println(0);}
    else {
        while (element!= null){
        element = element.next;
        elementIndex++;}
        }
    System.out.println(elementIndex);
    }
/**  Helper method for get(int n) method */
private Node getNode(int n) {
    int index;
    Node element = first;
    for (index =0; index < n; index++)  {
        element = element.next;
    }
    return element;
}

/** Returns the nth element in the linked list */
public Object get(int n)    {
    return getNode(n).data;     
}

/** Sorts the linked List*/
public  void sort(Node node)        {

}



public Object findElement(Object element){
    }

public void print(Node node)    {
    if (node == null)   {node =first;}
while (node!= null) {
    System.out.println(node.data);
    node = node.next;
}
}

public class ContactComparator implements Comparator<Contact1>{
    @Override
     public int compare(Contact1 a, Contact1 b)
    {
        return b.getName().charAt(0)-(a.getName().charAt(0));
    }
}

public static void main (String [] args)    {
    list.addFirst(new Contact1("John Harvey" ,6000, "jh@gmail.com"));
    list.addFirst(new Contact1("Cris Irish",2000, "cI@gmail.com"));
    list.addFirst(new Contact1("Tom Jhonson",2400,"tj@gmail.com" ));
    System.out.println("Current List:");
    list.print(null);
    list.reverse();
    System.out.println("Reverse List: ");
    list.print(null);
    System.out.println("Size of the list");
    list.size();
    System.out.println("Current Size of the List");
    System.out.println(list.currentSize);   
    list.get(1);

}
}

这是联系人类:

package project3;

public class Contact1 {

private String name;
private int number;
private String mail;

public Contact1(String n, int pn, String e){
    this.name= n;
    this.number = pn;
    this.mail =e;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}


public int getNumber() {
    return number;
}
public void setNumber(int number) {
    this.number = number;
}

public String getEmail() {
    return mail;
}
public void setEmail(String name) {
    this.mail = name;
}
public String toString(){
    return "Name: "+this.name+" "+" Number "+this.number+" Email: "     +this.mail;
}

1 个答案:

答案 0 :(得分:0)

从它的外观来看,你的列表有一个自然的顺序,那么为什么不使用TreeSet而不是列表呢?在集合中,任何条目都必须是唯一的,并且订购了树集。然后,您的Contact1类可以实现Comparable并实现Comparator本身。