我還沒有找到這方面的任何資訊,如何使用鏈接串列對字串進行插入排序。我已經能夠創建鏈接串列,但是我無法弄清楚如何實作排序部分。
我也希望能夠按名字和姓氏排序,但首先我需要弄清楚如何按名字排序......
這是我到目前為止所達到的。
public class LinkedlistIS
{
public CustomerNode head;
public CustomerNode sorted;
public class CustomerNode
{
public int val;
public string firstName;
public string lastName;
public CustomerNode next;
public CustomerNode(int val, string firstName, string lastName)
{
this.val = val;
this.firstName = firstName;
this.lastName = lastName;
}
public string FirstName
{
get { return firstName; }
}
public string LastName
{
get { return lastName; }
}
public int CompareTo2(CustomerNode another)
{
if (this.lastName.CompareTo(another.LastName) < 0)
return -1;
else
if (this.lastName.CompareTo(another.LastName) == 0)
return this.firstName.CompareTo(another.FirstName);
else
return 1;
}
}
public void push(int val, string firstName, string lastName)
{
/* allocate node */
CustomerNode newnode = new CustomerNode(val, firstName, lastName);
val = 1;
/* link the old list off the new node */
newnode.next = head;
/* move the head to point to the new node */
head = newnode;
}
// function to sort a singly
// linked list using insertion sort
public void insertionSort(CustomerNode headref)
{
// Initialize sorted linked list
sorted = null;
CustomerNode current = headref;
// Traverse the given
// linked list and insert every
// node to sorted
while (current != null)
{
// Store next for next iteration
CustomerNode next = current.next;
// insert current in sorted linked list
sortedInsert(current);
// Update current
current = next;
}
// Update head_ref to point to sorted linked list
head = sorted;
}
/*
* function to insert a new_node in a list. Note that
* this function expects a pointer to head_ref as this
* can modify the head of the input linked list
* (similar to push())
*/
public void sortedInsert(CustomerNode newnode)
{
/* Special case for the head end */
if (sorted == null || sorted.val >= newnode.val)
{
newnode.next = sorted;
sorted = newnode;
}
else
{
CustomerNode current = sorted;
/* Locate the node before the point of insertion */
while (current.next != null &&
current.next.val < newnode.val)
{
current = current.next;
}
newnode.next = current.next;
current.next = newnode;
}
}
/* Function to print linked list */
public void printlist(CustomerNode head)
{
while (head != null)
{
Console.Write(head.firstName " " head.lastName " " head.val " ");
head = head.next;
}
}
任何幫助將不勝感激。
謝謝
uj5u.com熱心網友回復:
有一些明顯的問題:
if (this.lastName.CompareTo(another.LastName) < 0)
return -1;
else
if (this.lastName.CompareTo(another.LastName) == 0)
return this.firstName.CompareTo(another.FirstName);
如果您想主要按名字排序,為什么要從比較姓氏開始?
sorted.val >= newnode.val
如果要按名稱排序,為什么要按值排序?如果您想按名字/姓氏比較節點,只需呼叫您的比較函式。
據我所知,其余的代碼看起來還可以作為學習練習。如果您有問題,我建議您
- 撰寫單元測驗!當您可以對旨在查找各種邊緣情況的演算法運行多組測驗資料時,查找錯誤會變得更加容易。特別是對于像排序這樣驗證結果很簡單的事情。
- 了解如何使用除錯器。當您可以在各個點停止并驗證變數是否符合您的期望時,程式的行為變得更容易理解。
有關詳細資訊,請參閱如何除錯小程式。
撰寫這樣的代碼作為學習練習非常有用,但請不要將這樣的代碼用于任何嚴肅的事情。框架中內置了非常精細的排序功能,既更快也更容易理解。另請注意,鏈表在現實生活中很少使用,我認為我在校外甚至沒有使用過一次。另請參閱我們必須避免使用鏈表。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494044.html
上一篇:未找到配置管理器
