有序單鏈表的集合運算

2022-02-04 07:55:18 字數 1003 閱讀 3470

#include "stdafx.h"

#include "malloc.h"

typedef struct node

list;

// 建立單鏈表

void createlist(list *&l, char a, int n)

r->next = null;

}

// 使用了排序演算法中的插入排序

void sort(list *&head)

p->next = q->next;

q->next = p;

p = r;

} }}// 求兩個集合並集

void union(list *la, list *lb, list *&lc)

else if(la->datadata)

else

}} // 剩餘節點(la與lb必定有乙個是null)

if (lb != null)

while (la!=null)

tc->next = null;

}// 求兩個集合的並集

void intersect(list *la, list *lb, list *&lc)

if (lb != null && lb->data == la->data)

la = la->next;

} tc->next = null;

}// 求差集,即a-b,去掉a鍊錶中與b鍊錶的交集

void subs(list *la, list *lb, list *&lc)

// 去掉lb鍊錶與la鍊錶的交集部分

if (!(lb != null &&la->data == lb->data))

la = la->next;

} tc->next = null;

}

1、使用單鏈表對集合進行交、並、差的運算,重點在於對單鏈表進行排序,排序後的單鏈表在進行運算,可以減少節點的比較優化時間複雜度。

有序單鏈表

由標題就可以知道,這篇部落格我們使用的是在插入時就已經排好序的單鏈表.我把它命名為 orderedlist 這裡我們從小到大排序,下面我們以此看一下它的主要方法與 實現就好 主要方法 插入節點 override public void insert int key else else if p.ne...

有序單鏈表的合併

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

有序單鏈表的合併

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