简体   繁体   English

气泡排序链接列表

[英]Bubble sort for linked list

I am working on a bubble sort function for a linked list. 我正在为链接列表开发冒泡排序功能。 Here is the header for the function: 这是该函数的标题:

void sort(struct lnode** head,
          void (*swapPtr)(struct lnode** head, struct lnode* n1, struct lnode* n2),
          int (*comparePtr)(void* v1, void* v2))

I don't really understand the function pointers being used. 我不太了解所使用的函数指针。 swapPtr is a function pointer to a function that is used to swap two nodes in the list. swapPtr是指向函数的函数指针,该函数用于交换列表中的两个节点。 comparePtr is used as a pointer to one of three functions, all of which compare either the values in a certain member of the structure which is used to store the number of counts and line number of a given word. comparePtr用作指向三个函数之一的指针,所有这些函数都会比较结构中某个成员的值,该成员用于存储给定单词的计数和行号。 The possibilities are: 可能是:

  1. countComp — takes two nodes and compares the number of times the word shows up, returns 0 if equal, 1 if node1 > node 2 and -1 for the reverse. countComp —获取两个节点并比较单词出现的次数,如果相等则返回0,如果node1> node 2则返回1,反之则返回-1。

  2. wordComp — compares the words for the given nodes same return values as above. wordComp —比较给定节点的单词,其返回值与上述相同。

  3. lineComp — compares the line number the word occurs on same return values as above. lineComp —比较单词在与上述相同的返回值上出现的行号。

I understand how the bubble sort works and the general steps to achieve a sorted list. 我了解气泡排序的工作原理以及获得排序列表的一般步骤。 The area I am confused on is on how to call comparePtr and what do I need to pass to it? 我困惑的领域是如何调用comparePtr ,我需要传递给它什么? Also I have a test.c file used for testing my sorting methods. 我还有一个test.c文件,用于测试我的排序方法。 How would I go about calling the sort function? 我将如何调用sort函数? I'm not sure what to pass for the second and third arguments. 我不确定第二个和第三个参数要传递什么。

If someone could help explain this to me, it'd be great! 如果有人可以帮我解释一下,那就太好了!

If you have two functions: 如果您具有两个功能:

void intSwapPtr(struct lnode** head, struct lnode* n1, struct lnode* n2) {
    //definition
}

int intComparePtr(void* v1, void* v2) {
    //definition
}

then you can call sort by just passing those functions as arguments. 那么您可以通过将那些函数作为参数传递来调用sort。 The functions will be implicitly converted in to pointers to functions when used as arguments. 当用作参数时,这些函数将隐式转换为指向函数的指针。

swap(listHead, intSwapPtr, comparePtr)

Inside the definition of swap, you can call swapPtr and comparePtr in the same way as you would call any other function, they will be implicitly dereferenced. 在swap的定义内,您可以像调用任何其他函数一样调用swapPtrcomparePtr ,它们将被隐式取消引用。

It looks to me like whoever created this header wanted the comparePtr to take two pointers and return an int telling you which node is "greater" in the ordering of nodes. 在我看来,创建此标头的人都希望comparePtr接受两个指针并返回一个int,告诉您哪个节点在节点顺序中是“更大”的。

So you would call it like this: 因此,您可以这样称呼它:

lnode * a = ...;
lnode * b = ...;
int comparison = comparePtr(a, b);
if (comparison < 0)
{
    // we know a comes after b
}
else if (comparison > 0)
{
    // we know a comes after b
}
else
{
    // a and b are equal as far as ordering is concerned
}

This is like the ruby spaceship operator . 这就像红宝石飞船的操作员

void sortList(node *start) { head = start;

 for(ptr = head; ptr != NULL; ptr = ptr->next) { for(newptr = ptr->next; newptr != NULL; newptr = newptr->next) { if(ptr->data > newptr->data) { int temp = ptr->data; ptr->data = newptr->data; newptr->data = temp; } } } 

}

This C++ function for sorting a given link list is working fine. 用于排序给定链接列表的此C ++函数正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM