【发布时间】:2019-05-14 21:36:44
【问题描述】:
我有一个任务,我必须创建一个链接列表,其中包含一个人姓名和他们进入一个大院的车牌号码。但我需要使用车牌号(例如:ABS1234)按字母顺序对列表进行排序。我一直在对排序进行一些研究,例如合并排序或使用 collection.sort,但我无法理解它。如果我能稍微推动一下如何做到这一点,那就太棒了。提前致谢。
public class Node {
//data fields
public String regPlate; // Registration Plate
public String firstName;
public String lastName;
//refrence link
public Node link;
//default constructor
public Node()
{
regPlate = "";
firstName = "";
lastName = "";
}//end of constructor.
}//end of node class
这是我创建的 Node 类。
public class LinkedList {
Node head;
Node tail;
Node current;
int listLength;
Scanner input = new Scanner(System.in);
//default constructor
public LinkedList ()
{
head = null;
listLength = 0;
}
//inserting new node in the beginning of the list
public void insertFirst(String fN, String lN, String rP)
{
Node newNode = new Node();
newNode.firstName = fN;
newNode.lastName = lN;
newNode.regPlate = rP;
//make newNode point to the first node in the life
newNode.link = head;
//makes head point to the new first node
head = newNode;
if(head == null)
tail = newNode;
++listLength;
}//end of insertFirst
public void displayDataLog()
{
Node current;
current = head;
while(current != null)
{
System.out.print("\n FullName: " + current.firstName + " " + current.lastName +
"\n Registration Plate Number: " + current.regPlate);
current = current.link;
}
}//end of display vehicles
public void totalVehicles()
{
System.out.print("\n Total Vehicle on the campus: " + listLength);
}//end of total vehicles
}//end of linked list
【问题讨论】:
-
什么是“BlinkList”?您只是说“链接列表”吗?
-
是的链表
-
必须实现排序算法吗?或者你可以使用 Collection.sort 吗?
-
我可以使用 collection.sort 但不知道怎么用
标签: java sorting singly-linked-list