Это моя реализация с двойным связующим списком и сортировка пузырьков на ней. Другие функции работают хорошо, но функция печати не выдавала никаких результатов после сортировки пузырьков в списке.Bubble Сортировать по двойному перечню с
//
// Double_linked_list.c
//
//
// Created by Dengke Liu on 9/7/15.
//
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "list.h"
// initialize the list structure;
void init(slist_t *list){
list->head=NULL;
list->tail=NULL;
}
// add a string to the end of the list
void append(slist_t *list, char *val){
// creating a newItem object
list_item_t* newItem = (list_item_t*)malloc(sizeof(list_item_t));
newItem->value = val;
newItem->next=NULL;
// if there are no elements, just use newItem as head
if (!list->head) {
newItem->prev=NULL;
list->head=newItem;
list->tail=newItem;
}else{
// otherwise append the node and point the tail at the end
newItem->prev=list->tail;
list->tail->next=newItem;
list->tail=newItem;
}
}
// print the elements of the list
void print(slist_t *list){
list_item_t* temp=list->head;
while (temp!=NULL) {
printf("%s\n", temp->value);
temp=temp->next;
}
}
// empty the list
void empty(slist_t *list){
list_item_t *temp=list->head;
while (temp->next!=NULL) {
temp=temp->next;
free(temp);
}
free(list->head);
}
// sort the elements in list in lexical order using bubble sort
void bubblesort(slist_t *list){
if (list->head==NULL) {
printf("this is an empty list");
}
// to record the comparision state
bool swapped=true;
list_item_t *temp;
while (swapped) {
swapped=false;
temp=list->head;
// iterate through the list to swap unordered elements
while (temp!=list->tail) {
// compare two elements, if they are disordered, then swap
if(strcmp(temp->value, temp->next->value)>0){
// swap the elements
if (temp->prev!=NULL) {
temp->prev->next=temp->next;
}
if (temp->next->next!=NULL) {
temp->next->next->prev=temp;
}
temp->next=temp->next->next;
temp->next->next=temp->prev;
temp->next->next=temp;
temp->prev=temp->next;
// change the swap record
swapped=true;
}
temp=temp->next;
}
print(list);
}
}
int main(){
slist_t *list;
init(list);
append(list, "blue");
append(list, "yellow");
append(list, "black");
append(list, "red");
append(list, "green");
print(list);
bubblesort(list);
print(list);
empty(list);
return 0;
}
Первая функция печати в main() выдала правильный выход, а вторая функция печати не выдавала никакого выхода. Может ли кто-нибудь помочь мне отладить его?
Ваш вид не работает. Как минимум, он не обновляет 'list-> head' и' list-> tail', чтобы указать на новый заголовок и хвост списка, который вы должны считать, будет отличаться после сортировки списка. –
Кроме того, ваш своп взломан. К тому времени, когда вы дойдете до 'temp-> next-> next = temp-> prev',' temp-> next' больше не указывает на тот же узел, который он изначально сделал, поэтому 'temp-> next-> next' нет указателя, который вы хотите изменить. –
Кроме того, вы должны установить 'temp = temp-> next' только в том случае, если вы не выполняете своп. После того, как вы * сделаете * выполнить swap, неизвестно, как 'temp' сравнивается с элементом, который является * затем *' temp-> next', поэтому вам не следует переходить к этому элементу. Bubblesort достаточно устойчив, чтобы оправиться от этого, но он менее эффективен таким образом. Это вообще. С вашей конкретной структурой списка, выполняя 'temp = temp-> next' после того, как swap может закончить настройку' temp' на 'NULL' и *, что * будет катастрофическим. –