資料結構鍊錶氣泡排序

2021-10-23 12:23:02 字數 4742 閱讀 6132

結束語

6000字文章,建議先自上而下的劃分函式,讀者先自己實現一下功能,

然後再看該文章才會目標明確,有方向感。

這裡採用鍊錶氣泡排序,需要有二級指標和鍊錶的基礎。

/*	單鏈表 排序 

@author: 趙雨騰

@data: 12th march 2020

@input: 49 38 65 97 76 13 27 49 -1

@output: the new list is:13 27 38 49 49 65 76 97

*/#include

#include

#define elementtype int

struct listnode

;typedef

struct listnode listnode;

typedef listnode * listnodeptr;

int getsize (listnodeptr currentptr)

;//長度

void

bubblesort

(listnodeptr headptr,

int len)

;//冒泡

void

createlisthead

(listnodeptr * headptrptr)

;//建立空頭

void

insertend2

(listnodeptr *lastptrptr, elementtype value)

;//尾插2

void

destroylist

(listnodeptr headptr)

;//free

void

printflist

(listnodeptr currentptr)

;//列印

intmain

(void)

len =

getsize

(headptr)

;//長度

bubblesort

(headptr,len)

;//冒泡

printf

("the new list is:");

printflist

(headptr)

;//列印

destroylist

(headptr)

;//free

}//長度

int getsize (listnodeptr currentptr)

return

(num);}

//冒泡 排序

void

bubblesort

(listnodeptr headptr,

int len)

else}}

}//空頭

void

createlisthead

(listnodeptr * headptrptr)

//尾插

void

insertend2

(listnodeptr *lastptrptr, elementtype value)

}//free

void

destroylist

(listnodeptr headptr)

}//列印

void

printflist

( listnodeptr currentptr)

}}

標頭檔案
#include

#include

結構體
#define elementtype int

struct listnode

;typedef

struct listnode listnode;

typedef listnode * listnodeptr;

// listnode 定義結構體

// listnodeptr 定義結構體指標

主函式

int

main

(void)

len =

getsize

(headptr)

;//長度

bubblesort

(headptr,len)

;//冒泡

printf

("the new list is:");

printflist

(headptr)

;//列印

destroylist

(headptr)

;//free

}

void createlisthead(listnodeptr * headptrptr);//建立空頭

// 函式介面:listnodeptr * 是二級指標

// 函式呼叫:createlisthead(&headptr);//建立空頭

// 函式定義如下:

//空頭 

void

createlisthead

(listnodeptr * headptrptr)

void insertend2(listnodeptr *lastptrptr, elementtype value);//尾插2

//函式介面:

// listnodeptr *是二級指標

// elementtype value 待排序的元素,這裡是int型別

// 函式呼叫:insertend2(&lastptr,num);//尾插建立列表

// 函式定義如下:

//尾插 

void

insertend2

(listnodeptr *lastptrptr, elementtype value)

}

// 該函式在主函式中的呼叫如下:

listnodeptr headptr,lastptr,currentptr;

createlisthead

(&headptr)

;//建立空頭

lastptr=headptr;

//尾指標就是空頭

int num,len;

scanf

("%d"

,&num)

;//鍊錶建立

while

(num!=-1

)

int getsize (listnodeptr currentptr);//長度

// 函式介面: listnodeptr 一級指標

// 函式呼叫: len=getsize(headptr); //長度

// 函式定義如下:

//長度 

int getsize (listnodeptr currentptr)

return

(num)

;}

void bubblesort(listnodeptr headptr,int len);//冒泡

// 函式介面: listnodeptr 一級指標

// 函式呼叫: bubblesort(headptr,len); //冒泡

// 函式定義如下:

//冒泡	排序 

void

bubblesort

(listnodeptr headptr,

int len)

else}}

}

// 該函式在主函式中的呼叫如下:

len =

getsize

(headptr)

;//長度

bubblesort

(headptr,len)

;//冒泡

void printflist(listnodeptr currentptr);//列印

// 函式介面:listnodeptr 一級指標

// 函式呼叫: printflist(headptr);//列印

// 函式定義如下:

//列印 

void

printflist

( listnodeptr currentptr)

}}

// 該函式在主函式中的呼叫如下:

printf

("the new list is:");

printflist

(headptr)

;//列印

void destroylist(listnodeptr headptr);//free

// 函式介面:listnodeptr 一級指標

// 函式呼叫: destroylist(headptr);//free

// 函式定義如下:

//free 

void

destroylist

(listnodeptr headptr)

}

// 該函式在主函式中的呼叫如下:

destroylist

(headptr)

;//free

不妥之處,還請讀者斧正~

資料結構 鍊錶排序

問題 我們需要將乙個無序的單鏈表在 o n log n 時間複雜度和常數級空間複雜度下,對鍊錶進行排序 時間複雜度為穩定o n log n 的排序只有堆排序與歸併排序,但是堆排序需要建堆,那麼鍊錶有n個節點我們就需要開闢長度為n的陣列來存放節點,所以我們選擇歸併排序,我們知道普通的歸併排序需要開闢乙...

資料結構 氣泡排序

起泡排序的過程很簡單。首先將第乙個記錄的關鍵字和第二個記錄的關鍵字進行比較,若為逆序,則將兩個記錄交換之,然後比較第二個記錄和第三個記錄的關鍵字。依次類推,直至第n 1個記錄和第n個記錄的關鍵字進行比較為止。上述過程稱作第一趟起泡排序,其結果使得關鍵字最大的記錄被安置到最後乙個記錄的位置上。然後進行...

資料結構 氣泡排序

氣泡排序演算法的基本思想是 假設待排序表長為n,從後往前 或從前往後 兩兩比較相鄰元素的值,若為逆序 即a i 1 a i 則交換它們,直到序列比較完。我們稱它為一趟冒泡,如果將最小的元素交換到待排序序列的第乙個位置 關鍵字小的元素往上 漂浮 這就是氣泡排序名字的由來 下一趟冒泡時,前一趟確定的最小...