【问题标题】:Sorting a LinkedList and implementing a find method对 LinkedList 进行排序并实现 find 方法
【发布时间】:2015-09-27 20:12:10
【问题描述】:

我有一个提供 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 方法让 find("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;
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 这是一个大学项目,教授限制我们使用 Comparator 或 Comparable。我们还没有完成树集。
    • 即便如此,让Contact1 类实现Comparable 将使您无需将比较器传递给方法。另一种方法是将比较器定义为您的 LinkedList 中的一个字段,因为您的列表仅包含相同类型的元素,您可以使用相同的比较器。最好的办法可能是让列表的内部表示成为Contact1 的类型列表
    • public void sort(Node node) { Node present = first; Node previous = null; for (present = first; present!=null; present = present.next) { previous = present; compare(previous.data, present.data); } }` 并且还修改了比较方法。列表仍然没有排序
    猜你喜欢
    • 2019-05-23
    • 1970-01-01
    • 2016-03-11
    • 2012-09-20
    • 2022-06-11
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多