如何使用'键'进行排序:C
问题描述:
我正在学习C.我创建了一个程序来计算文本文件中单词的频率。 我的结构包含三个键(频率,词,nextLink)。如何使用'键'进行排序:C
事情是我有使用键排序的结构数组,但不知道如何去做这件事。任何指导,链接都会很棒。
我提供我arrayOfStructs排序的代码
void sortArray(int array[], int count)
{
int i,j,temp;
for (i = 0; i < count; ++i)
{
for (j = i + 1; j < count; ++j)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
这将是排序的LinkedList
void sortList(struct Node *head)
{
struct Node *i, *j, *temp;
for (i = head; i != NULL ; i->next)
{
for (j = head->next; j != NULL; j->next)
{
if (head->frequency < head->next->frequency)
{
temp = head;
head = head->next;
head->next = temp;
}
}
}
}
struct Node
{
int frequency;
char word[50];
struct Node *next;
};
答
使用i = i->next
而不是i->next
和j = j->next
代替j->next
的可能途径。
void sortList(struct Node *head)
{
struct Node *i, *j, *temp;
for (i = head; i != NULL ; i = i->next) // i = i->next
{
for (j = head->next; j != NULL; j = j->next) // j = j->next
{
if (head->frequency < head->next->frequency)
{
temp = head;
head = head->next;
head->next = temp;
}
}
}
}
负面反馈的原因是什么? :o –