單鏈表的插入排序

2021-06-22 18:25:30 字數 474 閱讀 4575

單鏈表的插入排序與陣列的插入排序不同,我用了一種簡單的方法來實現鍊錶的插入排序,**如下:

#include

using namespace std;

struct listnode

};class solution

return q;

}listnode *insertionlist(listnode *head,int currentval)

if (currentvalval)  //如果插入的值比頭結點還要小,那就插在頭結點前面

else        //將節點插入在合適的位置

p = p->next;

}p->next = tmp;    //若是到了最後,就把此節點連線在最後即可

tmp->next = null;

return head;}}

};void main()

while(1);

}

單鏈表插入排序

單鏈表插入排序演算法基本思想是構建乙個新的有序煉表頭結點,並將原來的鍊錶節點依次插入到新建的有序鍊錶中。如下 include stdafx.h include using namespace std struct listnode class solution listnode tmpnext pt...

單鏈表插入排序

include include typedef int elemtype 元素的資料型別 typedef struct lnode linknode 單鏈表結點型別 尾插法建立單鏈表,細節不表 void createlistr linknode l,elemtype a,int n 輸出線性表,細節...

單鏈表插入排序

鍊錶插入排序的基本思想是 在每個物件的結點中增加乙個鏈域link。對於存放於陣列中的一組物件v 1 v 2 v n 若v 1 v 2 v i 1 已經通過鏈結指標link,按其排序碼的大小,從小到大鏈結起來 現在要插入v i i 2,3,n,則必須在前面i 1個鏈結起來的物件當中,循鏈順序檢測比較,...