資料結構原始碼筆記(C語言) 置換 選擇演算法

2021-10-08 21:57:17 字數 3812 閱讀 5202

//實現置換-選擇演算法

#include

#include

#include

#include

#define maxsize 20

typedef

int infotype;

//定義其他資料項的型別

typedef

int keytype;

//定義關鍵字型別為整型

struct redtype //記錄型別

;#define maxkey 32767

//最大關鍵字值

#define runend_symbol 32767

//段結束標記

#define w 5

//記憶體工作區可容納的記錄個數

#define m 6

//設輸出m個資料換行

#define n 18

//設大檔案有n個資料

typedef

int losertree[w]

;//敗者樹是完全二叉樹且不含葉子,可採用順序儲存結構

typedef

struct

rednode,workarea[w]

;//記憶體工作區,容量為w

void

select_minimax

(losertree ls,workarea wa,

int q)

ls[0]

=q;}

void

construct_loser

(losertree ls,workarea wa,file *fi)

}void

get_run

(losertree ls,workarea wa,

int rc,

int&rmax,file *fi,file *fo)

else

//輸入檔案非空時

else

//新讀入的記錄屬當前段

wa[q]

.rnum=rc;

}select_minimax

(ls,wa,q)

;//選擇新的minimax記錄}}

void

replace_selection

(losertree ls,workarea wa,file *fi,file *fo)

}void

print

(redtype t)

//輸出t記錄

intmain()

,,,,

,,,,

,,,,

,,,,

,}; file *fi,

*fo;

losertree ls;

workarea wa;

keytype tmp;

int i,k,j=runend_symbol,rno=1;

char s[3]

,fname[4]

; fo=

fopen

("ori"

,"wb");

//以寫的方式開啟大檔案ori

fwrite

(a,sizeof

(redtype)

,n,fo)

;//將陣列a寫入大檔案ori

fclose

(fo)

; fi=

fopen

("ori"

,"rb");

//以讀的方式重新開啟大檔案ori

printf

("\n大檔案的記錄為:\n\t");

for(i=

1;i<=n;i++

)printf

("\n");

rewind

(fi)

;//使fi的指標重新返回大檔案ori的起始位置,

//以便重新讀入記憶體,產生有序的子檔案

fo=fopen

("out"

,"wb");

//以寫的方式開啟初始歸併段檔案out

replace_selection

(ls,wa,fi,fo)

;fclose

(fo)

;fclose

(fi)

; fi=

fopen

("out"

,"rb");

//以讀的方式重新開啟初始歸併段檔案out

printf

("初始歸併段檔案的記錄如下:\n\n");

printf

("歸併段%d:"

,rno)

; i=

1;tmp=0;

do tmp=b.key;}}

while

(k==1)

;printf

("\n");

rewind

(fi)

;//使fi的指標重新返回大檔案ori的起始位置,以便重新讀入記憶體,產生有序的子檔案

k=0;while(!

feof

(fi)

)//按段輸出初始歸併段檔案out}}

while

(i==1)

;}fclose

(fi)

;printf

("\n共產生%d個初始歸併段檔案\n\n"

,k);

return0;

}

資料結構原始碼筆記(c語言描述)彙總:

資料結構原始碼筆記(c語言):英文單詞按字典序排序的基數排序

資料結構原始碼筆記(c語言):直接插入排序

資料結構原始碼筆記(c語言):直接選擇排序

資料結構原始碼筆記(c語言):置換-選擇演算法

資料結構原始碼筆記(c語言):huffman樹字元編碼

資料結構原始碼筆記(c語言):josephus問題之順序表

資料結構原始碼筆記(c語言):josephus問題之迴圈鏈結表

資料結構原始碼筆記(c語言):多項式合併

資料結構原始碼筆記(c語言):二叉樹之葉子結點旋轉銷毀

資料結構原始碼筆記(c語言):哈夫曼樹

資料結構原始碼筆記(c語言):集合的位向量表示

資料結構原始碼筆記(c語言):鏈結佇列

資料結構原始碼筆記(c語言):鏈結棧

資料結構原始碼筆記(c語言):線性表的單鏈表示

資料結構原始碼筆記(c語言):線性表的順序表示

資料結構原始碼筆記(c語言):棧的基本操作

資料結構原始碼筆記(c語言):中綴表示式

資料結構原始碼筆記(c語言):希爾插入排序

資料結構原始碼筆記(c語言):索引檔案建立和查詢

資料結構原始碼筆記(c語言):氣泡排序

資料結構原始碼筆記(c語言):快速排序

資料結構原始碼筆記(c語言):可變長度字串的快速排序

資料結構原始碼筆記(c語言):基數排序

資料結構原始碼筆記(c語言):二路歸併排序

資料結構原始碼筆記(c語言):堆排序

資料結構原始碼筆記(c語言):二叉樹搜尋樹kruskal

資料結構原始碼筆記(c語言):二叉搜尋樹prim

資料結構原始碼筆記(c語言):最短路徑弗洛伊德演算法

資料結構原始碼筆記(c語言):深度、廣度優先生成樹

資料結構原始碼筆記(c語言):鄰接矩陣轉化鄰接表

資料結構原始碼筆記(c語言):統計字串**現的字元及其次數

資料結構原始碼筆記(c語言):順序查詢

資料結構原始碼筆記(c語言):分塊法查詢

資料結構原始碼筆記(c語言):二分查詢

資料結構原始碼筆記(c語言):二叉樹遍歷

資料結構原始碼筆記(c語言):二叉平衡樹的相關操作演算法

資料結構原始碼筆記(c語言):二叉排序樹的基本操作演算法

資料結構原始碼筆記(C語言) 堆排序

實現堆排序演算法 include include define maxe 20 typedef int keytype typedef char infotype 10 typedef struct rectype void dispheap rectype r,int i,int n 按遞增有序進...

資料結構原始碼筆記(C語言) 鏈結棧

鏈結棧 型別和介面函式宣告 include include typedef int datatype struct node 單鏈表結點 typedef struct node pnode 指向結點的指標型別 struct node struct linkstack 鏈結棧型別定義 typedef ...

資料結構原始碼筆記(C語言) 直接選擇排序

實現直接選擇排序演算法 include include define maxe 20 typedef int keytype typedef char infotype 10 typedef struct rectype void selectsort rectype r,int n 直接選擇排序演...