鍊錶的排序1

2021-07-25 17:12:35 字數 3208 閱讀 2062

已知兩個鍊錶head1和head2各自有序,請把它們合併成乙個鍊錶仍然有序,要求用遞迴 方法實現。

#include

#include

struct node

int num;

node *next;

node *merge(node *head1,node *head2)

if(head1==null)

return head2;

if(head2==null)

return head1;

node *head=null;

if(head1->numnum)

head=head1;

head->next=merge(head1->next,head2);

else

head=head2;

head->next=merge(head1,head2->next);

return head;

2、遞增有序的2個單鏈表合併成乙個遞增有序的單鏈表,不用任何庫函式呼叫

/* 說明: 如果採用一般的做法,直接申請新的空間,組成乙個新的

單鏈表,直接進行歸併就可以得到整體的有序序列,這個相對比較簡單。

如果不能用任何庫函式,也就是不能利用malloc分配記憶體,就是要利用

現有的2個鍊錶,進行元素交換得到最後有序的鍊錶。

#include

using namespace std;

/* 單鏈表節點 */

struct node

;/* 給單鏈表新增節點 */

void insertnode(node* head, int value)

while ( p->next != null )

node* tmp = new node;

tmp->value = value;

tmp->next = null;

p->next = tmp;

}/* 遍歷輸出鍊錶節點 */

void print(node* head)

cout << endl;

}/* 利用一般的方法進行合併,形成整體遞增有序*/

node* formalmerge(node* heada, node* headb)

if ( q == null )

while ( (p != null) && (q != null) )

else if ( p->value < q->value )

else if ( p->value > q->value )

}while ( p != null )

while ( q != null )

return head;

}/* 下面實現不使用任何庫函式, 利用交換的方法在原空間實現整體有序。 方法是先確定哪乙個鍊錶

的第乙個節點的值小,把這個鍊錶的頭結點作為合併後鍊錶的頭結點,然後比較2個有序鍊錶的當前節點

的值,如果代表最後合併鍊錶的值小,則不用交換,否則把兩個值交換,最後合併鍊錶始終保持兩個值中

的小值。另乙個鍊錶由於交換了乙個元素,當前元素可能影響該鍊錶的有序遞增,對其進行調整使其保持

遞增有序,然後重複上述動作,直到乙個鍊錶遍歷結束,然後把剩餘的鍊錶連線起來就行。*/

/* 調整鍊錶的第乙個節點,使其變成遞增有序*/

void chg2sort(node* head, node* &p)

node* s = head;

while ( s->next != p ) //s指向p的前乙個節點

//下面的一段找到第乙個大於p節點值的節點

node* q = p;

node* r = q;

while ( q != null )

else

}//下面調整指標,其實可以統一寫出來,為了閱讀清晰把q為null和非null分開寫出來

if ( q == null )

else if ( q != null )

//由於鍊錶進行了調換,當前鍊錶指標也需要改變

p = s->next;

}/* 兩個有序鍊錶進行合併 */

node* merge(node* head1, node* head2)

else if ( q == null )

//兩個都不為空,先確定哪個鍊錶作為合併後的鍊錶

if ( (p != null) && (q != null) )

else

}node* p_prior; //始終指向p節點的前乙個節點

node* q_prior;

while ( (p != null) && (q != null))

else if ( head == head2 )

}else if ( p->value == q->value )

else if ( p->value > q->value )

else if ( head == head2 )}}

if ( p != null )

if ( q != null )

return head;

}int main()

;node* heada = new node;

heada->next = null;

for (int i = 0; i < 5; ++i)

print(heada);

/* 建立有序鍊錶b */

int b[3] = ;

node* headb = new node;

headb->next = null;

for (int i = 0; i < 3; ++i)

print(headb);

/* 利用簡單合併的方法合併成整體有序 */

node* head = formalmerge(heada, headb);

print(head);

int c[3] = ;

node* headc = new node;

headc->next = null;

for (int i = 0; i < 3; ++i)

print(headc);

//test chg2sort

chg2sort(headc, headc->next);

print(headc);

head = merge(heada, headb);

print(head);

return 0;

}

鍊錶 排序鍊錶

樂扣原題 definition for singly linked list.public class listnode class solution 將鍊錶轉化為陣列 listlist new arraylist while null head 陣列遞增排序 collections.sort li...

鍊錶的排序

include stdio.h include malloc.h define len sizeof struct node typedef int datatype typedef struct node dlnode typedef dlnode dlist dlist create dlist...

鍊錶的排序

今天自己將職工管理系統用鍊錶的形式又重新寫了一遍,增刪改查的功都能實現,但是在對職工薪資進行排序的時候則遇到了問題,第一次碰到在煉表裡進行排序,想了很久也沒想出合適的方法,之後看了之前所用過的氣泡排序,發現完全可以用類似的方法來對薪資進行排序,下面則是鍊錶排序的具體 p temp7,max,n,m ...