[英]Bubble Sort on double linked list with
這是我的雙鏈表實現和冒泡排序。 其他功能運行良好,但在我對列表進行冒泡排序后,打印功能沒有給出任何輸出。
//
// 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() 中的第一個打印函數給出了正確的輸出,而第二個打印函數沒有給出任何輸出。 誰能幫我調試一下?
你的程序在一般形式上看起來很合理,但我看的越多,我在細節中發現的錯誤就越多。 特別是,
1) 在函數main()
,您聲明變量list
,一個指針,但從不初始化它。 然后您將這個不確定的值傳遞給init()
函數,該函數繼續取消對它的引用,就好像它是一個有效的指針一樣。 這會產生未定義的行為。 您可以為list
動態分配存儲空間,但在這種情況下,這樣做更容易:
int main() {
slist_t my_list;
slist_t *list = &my_list;
/* ... */
2) 您的bubblesort()
函數在執行涉及那些指向的節點的交換時無法更新list->head
和list->tail
。 這將在排序過程中和排序后引起問題。
3) 您的bubblesort()
函數未正確交換列表節點。 有不止一種方法可以做到,但您實際實施的不是其中一種。 它首先在temp->next->next=temp->prev
中斷,因為此時temp->next
已經更新,結果temp->next->next
不是您想要的指針之一調整。 構建這種交換的一種更簡單的方法是從列表中刪除節點temp
,然后在稍后重新插入一個位置。 仔細跟蹤哪個指針指向什么。 它可以幫助繪制它的圖表。
4) 您的bubblesort()
函數應避免在執行交換的內部循環的迭代期間設置temp = temp->next
。 在這種情況下,你不知道怎么temp
比較新的temp->next
,甚至是否有temp->next
了。 如果沒有(即如果新的temp->next
是NULL
),那么更新temp
將是災難性的。 如果沒有發生這種情況,您將很幸運地完成排序例程的運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.