Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 链接列表帮助的气泡排序_C_Sorting_Linked List - Fatal编程技术网

C 链接列表帮助的气泡排序

C 链接列表帮助的气泡排序,c,sorting,linked-list,C,Sorting,Linked List,我的代码有一些问题,需要你帮忙。 我正在尝试对我的房间结构进行冒泡排序,代码如下。 差不多,1号房间的名字:z。。。房间2名称:a,我想对这些进行排序,以便显示a、B等 问题是:当我输出链表时,这种排序会使某些节点消失。。例如,在对Z>A进行排序并将其置于顶部之后,当我打印出链接列表时,A现在消失了 谢谢你的帮助 void sortRoomsByName(int count, ROOM *head) { int i; int j; for(i = count ; i > 1 ; i--

我的代码有一些问题,需要你帮忙。 我正在尝试对我的房间结构进行冒泡排序,代码如下。 差不多,1号房间的名字:z。。。房间2名称:a,我想对这些进行排序,以便显示a、B等

问题是:当我输出链表时,这种排序会使某些节点消失。。例如,在对Z>A进行排序并将其置于顶部之后,当我打印出链接列表时,A现在消失了

谢谢你的帮助

 void sortRoomsByName(int count, ROOM *head)
{
int i;
int j;

for(i = count ; i > 1 ; i-- ) //outer loop
{
    ROOM *temp;
    ROOM *swap1;

    swap1 = head;
    for(j = 0 ; j < count-1 ; j++ ) //inner loop
    {
        if(strcmp(swap1->roomName,swap1->nextRoom->roomName)<0) //compares names and swaps
        {
            ROOM *swap2;
            swap2 = swap1->nextRoom;
            swap1->nextRoom = swap2->nextRoom;
            swap2->nextRoom = swap1;
            if(swap1 == head)
            {
                head = swap2;
                swap1 = swap2;
            }
            else
            {
                swap1 = swap2;
                temp->nextRoom = swap2;
            }
        }
        temp = swap1;
        swap1 = swap1->nextRoom;
    }
}
}

若并没有进行交换,那个么你们必须跳转到下一个节点,我希望这有帮助

编辑: 将Head从函数参数中移除,并使其成为全局的或按如下方式传递

void sortRoomsByName(int count, ROOM **head)
{
int i;
int j;

for(i = 0 ; i < count - 1 ; i ++ ) //outer loop
{
    ROOM *swap1;

    swap1 = *head;
    for(j = 0 ; j < count - i - 1 ; j ++ ) //inner loop
    {
        if(strcmp(swap1->roomName,swap1->nextRoom->roomName)<0) //compares names and swaps
        {
            ROOM *swap2;
            swap2 = swap1->nextRoom;
            swap1->nextRoom = swap2->nextRoom;
            swap2->nextRoom = swap1;
            if(swap1 == head)
            {
                *head = swap2;
            }
        }
        else
        {
            swap1 = swap1->nextRoom;
        }
    }
}

int main (void)
{
    sortRoomsByName(count /* your variable */, &head /* your head pointer */);
}

所以这个代码有什么问题?错误的结果或一些内存问题?当我输出我的链表时,它几乎把Z放在顶部,而A是不相似的。看起来你是在错误地构建循环。它仍然给出相同的问题,删除节点?你能不能放一些代码,说明你是如何调用它的。。在这里声明头指针。
void sortRoomsByName(int count, ROOM **head)
{
int i;
int j;

for(i = 0 ; i < count - 1 ; i ++ ) //outer loop
{
    ROOM *swap1;

    swap1 = *head;
    for(j = 0 ; j < count - i - 1 ; j ++ ) //inner loop
    {
        if(strcmp(swap1->roomName,swap1->nextRoom->roomName)<0) //compares names and swaps
        {
            ROOM *swap2;
            swap2 = swap1->nextRoom;
            swap1->nextRoom = swap2->nextRoom;
            swap2->nextRoom = swap1;
            if(swap1 == head)
            {
                *head = swap2;
            }
        }
        else
        {
            swap1 = swap1->nextRoom;
        }
    }
}

int main (void)
{
    sortRoomsByName(count /* your variable */, &head /* your head pointer */);
}