我对 C 和指针比较陌生。我正在尝试排序然后打印结构的链接列表。要么我错过了一个逻辑错误,要么我没有完全理解指针。有人可以向我解释一下我在这段代码中缺少什么吗?预先感谢您!
// *** Sort Linked List ( Merge Sort ) ***
struct address_node *newRoot;
// ptr, rearPtr, and tempRoot are also struct Pointers
newRoot = root;
root = root->next;
while (root != NULL)
{
tempRoot = root;
ptr = newRoot;
rearPtr = newRoot;
while (ptr != NULL)
{
printf("here");
if ((root->frequency) == (ptr->frequency))
{ // SPECIAL CASE: To determine read hierarchy for repeated
// Entries
if ((root->read_order) < (ptr->read_order))
{
if (ptr == newRoot)
{
root = root->next;
tempRoot->next = newRoot;
newRoot = tempRoot;
ptr = NULL;
}
else
{
root = root->next;
tempRoot->next = ptr;
rearPtr->next = tempRoot;
ptr = NULL;
}
}
}
else if ((root->frequency) > ptr->frequency)
{ // To ADD BEFORE an ENTRY
if (ptr == newRoot)
{
root = root->next;
tempRoot->next = newRoot;
newRoot = tempRoot;
ptr = NULL;
}
else
{
root = root->next;
tempRoot->next = ptr;
rearPtr->next = tempRoot;
ptr = NULL;
}
}
else if (ptr->next == NULL)
{ // if END OF LIST
root = root->next;
ptr->next = tempRoot;
ptr = NULL;
}
else
{ // SPOT NOT FOUND YET< MOVE FORWARD THROUGH LIST
rearPtr = ptr;
ptr = ptr->next;
}
}
}
// *** PRINT ***
ptr = newRoot;
if (numOut == 0)
{
while (ptr != NULL)
{
printf("0x%zx :%d\n", ptr->address, ptr->frequency);
ptr = ptr->next;
}
}
else
{
while (ptr != NULL && numOut > 0)
{
printf("0x%zx :%d\n", ptr->address, ptr->frequency);
numOut--;
ptr = ptr->next;
}
}