氣泡排序(鍊錶實現)

2022-04-06 18:09:26 字數 743 閱讀 1841

暑假裡看到大神的一篇文章,在乙個大公司裡面試,面試氣泡排序鍊錶版,當時想了寫一下,不知該如何寫起,就放下了,最近資料結構又重新學習鍊錶,試著寫了下,對鍊錶的操作熟練運用吧。調的我很糾結啊。。。

1 #include 2 #include 3 #include 4 #include 5

using

namespace

std;

6struct

node

7list;

11struct node *create(int n)//

建立順序鍊錶

1226

return

head;27}

28void show(struct node *head)//

輸出函式

2940 printf("\n"

);41}42

void bubblesort(struct node *head,int n)//

氣泡排序

4351 end = str -> next;//

把末尾給存起來

52for(i = 1;i <= n-1;i ++)

5367 str = str ->next;

68 pos = pos ->next;69}

70 end =str;71}

72}73int

main()

74

用鍊錶實現氣泡排序!

我們需要把資料儲存在鍊錶中,然後呼叫排序函式就可以了。但必須要注意鍊錶與陣列的不同點,鍊錶沒有下標,要想訪問資料域必須通過節點來訪問。二 實現 include includetypedef int elemtype typedef struct node node,plist void init p...

鍊錶氣泡排序

2011 07 22 21 33 111人閱讀收藏 舉報 任意兩個相鄰節點p q位置互換圖示 假設p1 next指向p,那麼顯然p1 next next就指向q,p1 next next next就指向q的後繼節點,我們用p2儲存 p1 next next指標。即 p2 p1 next next,則...

鍊錶氣泡排序

任意兩個相鄰節點p q位置互換圖示 假設p1 next指向p,那麼顯然p1 next next就指向q,p1 next next next就指向q的後繼節點,我們用p2儲存 p1 next next指標。即 p2 p1 next next,則有 p q 排序前 p1 next p1 next nex...