小编Bec*_*Bec的帖子

在C中使用指针对冒泡排序单链接列表

我正在尝试使用C中的指针操作对单个链接列表进行冒泡排序。我已经在网站上查看了冒泡排序的其他一些实现,但是我觉得这里代码的逻辑应该是有道理的。即使这样,仍然进入无限循环。任何帮助将不胜感激!

int counter;
struct node* current = head;
struct node* previous = (struct node*) malloc(sizeof(struct node));
struct node* next = (struct node*) malloc(sizeof(struct node));

for (counter = 0; counter < num_nodes; counter++){
    current = head;
    next = current->m_next;

    while(next != NULL){
        int compare = strcmp(current->m_last_name, next->m_last_name);
        if (compare > 0){
            if (current == head){
                head = next;
            }
            previous->m_next = next;
            current->m_next = next->m_next;
            next->m_next = current; 

            previous = next;
            next = current->m_next;
        }
        else {
            previous = current;
            current …
Run Code Online (Sandbox Code Playgroud)

c sorting linked-list list bubble-sort

0
推荐指数
1
解决办法
2万
查看次数

标签 统计

bubble-sort ×1

c ×1

linked-list ×1

list ×1

sorting ×1