有序單鏈表的合併

2021-08-10 08:04:18 字數 1355 閱讀 7122

接上兩篇,乙個是遞迴與非遞迴的區別,這裡能明顯的看出來,遞迴**簡潔,易懂,但層層呼叫會消耗棧空間。另乙個這是單鏈表的乙個小應用,具體例項的乙個東西……

#include "stdafx.h"

#include

#include "stdlib.h"

using namespace std;

typedef struct node

node;

/*建立單鏈表

日期:2023年11月3日 21:46

*/node * create()

p = new node();

p->data = x;

if (++i == 1)

else

q = p;

}q->next = null;

return head;}/*

測單鏈表長度

日期:2023年11月3日 21:53

*/int length(node * head)

return len;}/*

單鏈表列印

日期:2023年11月3日 21:53

*/int print(node * head)

p = head->next;

while (p != null)

return index;

}//遞迴方法 有序單鏈表合併 

//按公升序排序,至於降序一樣的道理

node * combineusedigui(node * heada, node* headb)

else if (headb == null)

if (heada->data <  headb->data)

else

return head;

}//非遞迴方法 有序單鏈表合併 

//按公升序排序,至於降序一樣的道理

node *  insert_node(node * head, node*item);

node * combineusefeidigui(node * heada, node* headb)

else if (headb == null)

if (length(heada)>length(headb))

else

while (p!=null)

return head;

}node *  insert_node(node * head, node*item)

//插入到頭節點之前

if (p == head)

//插入到q與p之間

q->next = item;

item->next = p;

return head;

}int main()

有序單鏈表的合併

1 非遞迴方式 cpp view plain copy 單鏈表.cpp 定義控制台應用程式的入口點。單鏈表 include stdafx.h include include using namespace std typedef struct node node 單鏈表的正向排序 node inse...

C有序單鏈表的合併

include include include include list.h 建立和列印鍊錶的函式在標頭檔案中 將鍊錶 list1與 list2合併成 list3 引數 兩個要合併的鍊錶的頭結點位址 node combinelist node list1,node list2 else 反之基本同上...

合併兩個有序的單鏈表,合併後依然有序

分析過程 首先我要合併有序的鍊錶,合併後依然有序。我就要有兩個指標,分別指向兩個鍊錶,觀察所給的兩個鍊錶是公升序還是降序的來確定合併後的鍊錶是否有序。這裡預設的認為鍊錶時公升序的 用指標分別指向兩個鍊錶的第乙個節點,比較大小,那個大,將元素尾插進入我所要返回的新鍊錶中去。一次比較迴圈往復,直至某乙個...