我正在尝试使用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)