数据结构上机编程汇总_第1页
数据结构上机编程汇总_第2页
数据结构上机编程汇总_第3页
数据结构上机编程汇总_第4页
数据结构上机编程汇总_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、数据结构上机编程汇总21链表#includeviostream. h>#include<malloc. h>#includestdlib. h>#i nclude<stdio.h>define ture 1define false 0sdefine ok 1#define error 0define infeasible -1define overflow -2define null 0typedefint status;typodofint elcmtype;typedefstruct lnodcelemtype data;struct lnode *ne

2、xt;lnode, *linklist;statusgetelem_l(linklist l, int i, e) /l涓哄甫瀆寸栢繚圭兢缔瞭捏琛丨兢瀆存塞閻js?/褰扌華i涓 压编豺班魏儿禄镇肝書鍊艰械缁根骞惰緬锲籥k镇肝慙蹂樂縞锲濫rrorlinklist *p;intj;p =l->next; j=l;/蹂潰鎚椀紅p鐫冏惶纭靖涓粘繚癸紐j涓鸿錬板擦秽烘塞閻堀惶錫屋堆錄撅紅鏤儔lap鐫冏煌纸哪姻餐鵬细狗埒p%咼撮lp二 p->next;+j;if(!p| i j>i) return error; /纸聊爛餐鶴绡鸞算鄆妊濛e= p->data; /鑒瀬i鐺幕碼retu

3、rn ok;/ getelem_lstatus listinsert_l(l, int i, elemtype e)镰厶甫渔寸粕繚圭視缔瞭扌堅细挎觥/涓i涓 縁细 籀鉗嘈彈鏈儡視鏈檯5mclinklist *p, *s;p二 l ; j = 0 ;whilel) p = p->next; +j;/i糞绘毕纭嘩-1%咼 粕缭? if(!p i i j>i-l)rcturnerror; /i澈忑篦1總艳唯免浜痔閹?1s = (linklist)malloc(sizeof (lnode) ;/鑒熠査軀扮粕繚?s->data =e; s->next=p->next;/鑽損

4、緩 i,涓?p->next= s;return ok;/ listlnscrt lstatus listdelete_l(linklt i, elemt/縱厶甫瀆寸栢繚圭兢鋪瞭捏结挎觥/涓 紅蹂猫權纸琲涓 压细狂紅骞舵潴e杷旃 洪錨跺?linklist *p, *q;int j;p= l; j= 0;wh订e/漢绘壘纭琲涓 粘繚癸紐骞朵护p鐫冏t呈鎳跺埠瓒?p= p->next;+j;)if (! (p->next) | | j>il) return error;/金柔市横浣嘔疆涓熬皓鎔?q =p->next; p->next = q->next; /

5、金柔彳市檣骞堕噴鏈剧粕繚?e = q->data; free(q);return ok;/ listdelete_lvoidcreatelist_l(linklin) 聞蔣綬搴牠絹鐺 涓 屋纟尚旌:兢鍊殓紅寤虹箱甯澧寸栢繚圭兢錦嚓揑绍挎觥/linklist*p;l =(linkli st)mal1oc(sizeof(lnode);l->next二nu1丄;/鐺堀缓绪娛猜涓 甫瀆寸粕繚圭歹克鋪瞭樫琛? for(i = n; i> 0;i)(p=(linklist)malloc (sizeof (lnode) ; /婪熠査謳扮粕缭?scemf ("%d"p-&

6、gt;ncxt = l->ncxt; l->next = p;/createlist_lvoid mergelist_l(linlinklist宸茬堆錦隙揑绡挎觥/a鍛孑台b鑽勸屋纟尚狗寧鍊奸浴闖損噺鈿掠坍/褰拱苟s鍛孑台b寰權垠鍋扮視錦瞭翹细挎觥/c镑孑台c鐵番力屋纟尚犹暦鐫夂奸潜闔捺 噺齟拱块linklist *pa, *pb , *pc;pa= la>next; pb = lb>next;lc = pc =pa;/aamr缁捶倍浣浦负lc鑽勸乙'' 缁捶倍if(pa-b->data)pc>next= pa; pc =pa; pa =pa

7、->next;elsepc->next= pb; pc =pb; pb =pb->next;pc->next =pa ? pa:pb;/鎖棋後鉗+緬娈?free (lb) ;/閲嫁睪攵lb鑽翻二''缁拯倍/mergelist_lint locateelem_sl(slinklists, elemtype e) i = s0.cur;whii.data != e)i = si. cur;return i;2- 2线性表define list_init_size100 /绍挎觥厶班鎏丨1-閹寸歹克鍊漬 鍊賺?#defi nelistincrement 10绡

8、挎觥厶项鑒丨1-閹寸兢金剰兼麻澧為?#defineok 1 typedef int status;typedefint elemtype;typedef structelemtype *elem;/瀛樺促细洪視錯哄淄int length;/褰擞埠閹垮害intlistsize;/褰扌欽埠鎳賺麻鑽勸项鎏厶 閲?sqlist;sqlistl , newbase;sqlist *q , *p , *pa , *pb,*pc , *pa_last , *pb_last;status initlist_sq(sqli蹣勖猊灣涓1-鑽勤嚎鎬觥/l. e1em=(e1emtype*)mal1oc(list_i

9、nit_size*sizeof(elemtype);if(!l. elem)exit(overflow);l length 二0;l.listsize =list_init_size;roturn ok;/initlist_sqstatuslistinsert_sq(sqlisi, elemtype e)魏口搴快嚎鎬觥/涓 i涓綬细舸鉗嘈弓畢鐺工摟鑽勧屋纟附赫鉅?/i鑽勧扌任娉 瞩 间负 1 金白?i金白?listlength_sq(l)+l1) | | (i>l. length+l)return error;/!鍊间药錫塚碣if (l. lenglh>=l. listsize)

10、/褰撅埠瀛樺促纟出洪扌兒宸叉弧侨肝 鍔豺壊間?newbase二 (elemtype*)malloc(l. elem, (l. listsize+listincrement)*sizeof(elemtype); if(!newbase) exit (overflow) ; /瀛樺促錄賺麻漕辫触l. el cm = newbase; /鍋板水晃鑽|l. listsize+ = listincrement; /澧怡姑瀛樺促渝归噺)qmit);/q涓烘弓畢鐺a7綬纟相?for(eml. length-1) ;p >=q; -p) *(p+l) = *p;鎖拱鍰浣嘔韻鑒婁il錫廃兢鏈幕碼鑒崇u*q

11、二e; /鎖損緩e+l. length; /琛u曜澧?return ok;/listinsert_sqstatuslistdelete_sq(sqlist, elemtype魏口塞快嚎鎬嫌vl嫡胥i邦閩徼醃过咼 匡纟尚狂紐骞剁粒甘马旃渓鏈跺g?/i鐵 勧皓娉曠间负1鉅?i鉅?listlength_sq(l) if>l. length) return error; /i鍊间筲錫塚碑 pmi-l); /p涓鸿 錄肺權鐺幕碼鑽勣綾纟相?e二*p; 塀 坍關傘鵝纟尚克兢鍊艰械缁檬q= l.elem+llength-1; /琛厶鄴鎳檯碼鑽勵綾纟相?fq;卄p) *(p-l)=*p; /踞土邦闖屋纟

12、尚猊笳錫疣夕克鍵幕曙宸cj-l.length;/ 琛口 曜鏤?return ok; /listdelcte sqintlocateelemsq(sqlist l, elemtype e, status (compare)(elemtype , elem type) 魏口搴伙嚎鎬觥/涓 堆錄剧1涓 奸/fe嬢。冻compare ()鑽勧屋纟尚克兢浣幣 礬/蹿償毕蹂帮糸王錄畑縞锲怡書魏纣咼皴視媪幣礬镑肝慙錄畑縞锲?int i= 1; /i鑽勧垓鍊间负纭?爛豎鶴纟尚葩娩濛忑綾p二【八elem;/p鑽勸t安鍊间负纸?x咼 匡纟尚彳乞視瀛樺促浣嘔疆whi*comp8re)(*p+, e) +i;if(h

13、) returni;olscreturn 0;/locateelemsqvoidmergelist_sq(sqlistla, sqlist lb, sqli宸茬燈楞哄筹绍挎觥/&鍛冶b鑽勧屋绡狗寧鍊奸漕聞淇噺齟換坍/褰損苟la鍛孑台b寰耦垠軀扮視秽哄礬绡挎觥/c镑孑台c鑽勸崖纟尚猊簷鐫夂奸潜闖損 噺齟熙并pa =la. elem;pb = lb. elem;lc.listsize =lc. length= la. length + lb. length;pc= lc. elem二(elemtype*)malloc(lc. listsize*sizeof(elemtype);if(!lc

14、. elem) exit (overflow) ;/瀛樺促錄賺麻漕辫触pa last 二 la. clem + la. lengtht;pb_ast二 lb. elem+ lb. lengtht ;whit) /褰淇苟=*pb) *pc+ = *pa +;else *pc+二 *pb+;whilelast) *pc+ = *pa+;/鎖損緩la鎭+總鑽肋崔缎?while(pb) *pc+ =*pb+; /鎖損複lb鉗+縱鑽勧屋绡? /mergelist.sq3- 2迷宫#define stack_init_size 100ttdefinestack increment 10寤轰猜涓匸鎳?voi

15、d initstack( sqstacks. basc=(int *)malloc(sizesizcof(int);if (!s. base) printf ("n銭唯ik涓寤冻n") ;exit (0) ;s. top=s. base;s.stacksize二size;intpop( sqst*c) if (s. tops. base) return 0;*e二*(-s. top);return 1;voidpush ( sqste) if (s. top-s. base二s.stacksize) s. base=(int *)roalloc (s. base,(s. s

16、tacksize+incr)*sizeof (int);if (!s. base)exit (0);s.top二s. base+s- stacksize;s.stacksize+=incr;*(s. top)=e;(s. top) +;)int gettop(sqstack if (s. top=s. baso)return 0;*e=*(s. top-1);return 1;struct migongint flag;int dir;struct migong a1010;int masepath(inti, int j, int m, int n,) if (ai j. flag=l) p

17、rintf ("n鐘傑彝閹煨颦¥);roturn0;do ifn)push(s, i);push(s, j);returnl;else if(aij.dir=l)if(aij+lflag=l) aijdir+;else push(s, i);push(s, j);aijflag二1;j+;continue;if(aijdir=2) if (ai+lj. flag=l) aij. dir+;else push(s, i);push(s, j);aij. flag二1;i+;continue; if (a订j dir 二二 3) if (aij j-l flag=l) ai j

18、. dir卄;else push(s, i);push(s, j);aij. flag=l;j-;continuc;)if(aij.dir=4)if (ai-lj. flag=l) aij. dir+;else push(s, i);push(s, j);aij. flag=l;i-;continue;elsoa订j. flag=l;poppopaij flag=0;while(s. top!二s.base); printf(zz¥n®廉淞銭鸿矶¥n); return 2;void main () int m, n, x, y, i, j, r=l ;sqstac

19、k si,s2;initstack(s2);initstack (si);int b1010 = 1,1,1,1,1,1,1,1,1,1),1,0,0, 1,0, 0,0, 1,0, 1,1,0, 0, 1,0, 0, 0, 1,0, 1,1,0, 0, 0, 0, 1, 1,0, 0, 1,1,0, 1,1, 1,1, 1,0, 0,1),1,0,0, 0,1,0, 0, 0, 0,1,1,0, 1,0, 0,0, 1,0,0, 1,1,0, 1,1, 1,0, 1,1,0, 1,l,l,o,o, 0, 0, 0, 0, 0, 1,1,1,1,1,1,1,1,1,1,1),printf(”馭

20、g寤鸿糠漁涓嬪混镑肝石涓?琛丨愀澧檬紆0琛丨愀闖氤矶¥); printf(z,0 1 2 34 5 6 7 89¥n¥n);fo;i+) printf (z,%d”,i);for(j=0;aijflag二bij;printf (,z%d “, bi j);ai j. dir=l;printf ("¥n");printfc璇疯緡鏈傑齬鑿傑綾纟相骷n);sea);scanf("%masepath (1, 1, m, n, si);while (si.top!=sl.base) );push (s2, x);)printf (&q

21、uot;¥n帏毀齬杞i矚 鑽劇j矶寰匐丿负:rt);while(s2. top!=s2. base) po%d, %d> “,x, y);if(r+%5=0)printf can");4- 1括号匹配define stack_init_size 100ttdefine stackincrement 10typedef char selcmtype;typedefint status;typedefstruct(selemtype *base;selcmtypc *top;int stacksize;jsqstack;status initstack(sqstacks.

22、 base=(selemtype *)malloc(stack init stzesizeof(seiemtype); if(!s. base)exit(overflow);s.top二s base;s.stacksizc二stack init size;mmreturn ok;statuspush(sqstemtype e)if(s. top-s. base>=s. stacksize)(s. base=(selemtype*)realloc(s. base, (s. stacksize+stackincrement)*sizeof(sei emtype);if(!s. base)ex

23、it(overflow);s. top=s. base+s. stacksize;s.stacksize+=stackincrement;*s.top+二e;return ok;statuspop(sqstaif (s. top=s. base)roturnerror;e=*s. top;return ok;)status stackempty (sqstack s) if (s. top二二s. base)return true;elseroturn false;status match(char a,sqsint i, n;n二strlen (a);n;i+)if (ai=, 'l

24、l ai=,(')push(s, ai);else if(s .top=s. base)return false;else if(*(s.top-1)二二'二二'')pop (s, *(s. top-1);else if(*(s. topt)二二二二')')pop(s, *(s. top-1);if(stackempty (s)returntrue;else returnfalse;int n)3in()sqstack s;initstack(s);char cstack_init_stze;gets(c);if (match (c,s)=l)

25、printf (,zright/z);else printf("wrong");return 0;4 -2行编辑sdefine stack_init_size 100 /瀛樺促细洪視錄漬 錄賺麻閲?#define st ackincrement 10/瀛樺促细洪檢蹂賺麻澧為噺typedefint status;typedefchar selemtype;typedef structselemtype *base;/鏡儿爛錨劃j猊舸鉗幣扌任関姣很匍錫庸紐base鑽勸间负null selemtype *top; /鎳垂鐫0拡int stacksize;/褰撇埠宸插培閻岖夕克瀛樺

26、促细洪猊镌矶互鐺幕稱涓哄崟浣?sqstack;status initstack(s/镰勖犹溥涓s. base=(selemtype *)malloc(stack init stzesizeof(selemtype);if (!s. base) exit (overflow) ;/瀛樺促鏗賺麻瀆辫触s.top=s base;s. stacksize=stack_init_size;roturn ok;/initstackstatuspush(sqslamtype e) 鎖損複鐺幕碼e涓烘機鑽麹爛楞跺屋纟尚? if(s. top-s. base>=s. stacksize) / 塚弧镇豹拷鍔

27、豺项鑒 i 1-閹?s. base= (selemtype *) realloc (s. base, (s. stacksize+s tackincrement)*sizoof(selemtype);if(!s. base) exit (overflow) ;/瀛樺促鎂嚥麻漕辫触s. top=s. base+s. stacksize;s. stacksize+二stackincrement;*s.top+二e;returnok;/pushstatus pops, selemtypif (s. top=s. base) roturn error; e=*-一s. top;return ok;/p

28、opstatus clearstack(sqstacs. top = s. base;return ok;)status destroystack(sqsfree(s. base);s. base = null;s. top 二 null;s. stacksize = 0;return ok;status stacktraverse(sqstack s, status(* visit)() (return ok;status visit(sqstaif (s.base=s. top) return error;while(s. basc!=s. top)printf(“c,*(s. base)

29、;s.base+;returnok;int lineedit ()(sqstack s;char ch, c;initstack(s);while(ch!二eof) while(chh!= n )switch (ch)case ':pop (s, c);break;case ':clearstack(s);break;defauit : push(s, ch);break; ch=getchar ( ); visit (s);clearstack (s);if (ch!二eof) ch=getchar ();destroystack (s); return 0;/ lineed

30、it5- 1数组define max_array_dim8typedef int elemtype; typeclef int stat us;typedef struet(elemtype*base;int dim;int *bounds;int *constarits;array;status initarray (arrdini,)int elemtotal, i;va_list ap; if(di>max array dim) return error;a.dim二dim;a. bounds二(int *)nelloc(dim*sizeof (int); if(!a. bound

31、s)exit(overflow);elemtotal=l; va_start (ap, dim); for (+i)a. boundsi=va_arg(ap, int); if (a bounds return underflow; elemtota1*二a. boundsi;va end (ap);a. base=(elemtype*)malloc(elemtota1*sizeof (elemtype); if(!a. base) exit(overflow);a. constants二(int *)nuilloc (dim*sizeof (int);if(!a. constants)exi

32、t(overflow);a. constantsdim-l=l;for(i=dim-2;i>=0;-i)a. constantsi二a. boundsi + l*a constantsi + l;return ok;status destroyattay(arif (!a. base)return error;free (a.base);a. basc=null;if (!a. bounds)return error;free (a.bounds);a. bounds二null;if (!a. constants) returnerror;free (a.bounds);a consta

33、nts二null;return ok;status locate (array a, va_list ap,ind int , i;off=0;for (i;+i)(incl=va_arg(ap, int);indif(nd>=a. boundsi)return overflow;indoff+二a. constantsi*;return ok;statusvalue(array a, elemtyva_list ap;intoff, resuit;va start (ap, e);if(resuit二locato(a, ap, off)=0)return resuit;e=*(a. b

34、ase+off);returnok;statusassign(armtype e,.)(va_list ap;int result, off; va start(ap, e);if (resuit 二locate(a, ap, oturn resuit;*(a. base+off) =e;return ok;int main()int dim=2, diml=3;array a;int x, y, i, j, k, e;int a, b, c;printfc请输入二维数组的维数:);seanf("%d;initarray (a, dim, x, y);foi+)for(j=0;jse

35、a n;assign (a, e, i, j);printfc输岀的二维数组为:n);for (i=(for(j)value (a, e, i, j); printf (,z%d “,e);jprintf (,n,z);)printf (z,请输入维三数组的维数:);scanf (,z%d %dinitarray (a, diml, a, b, c);f;i+)for(j=0;f;k+)scanf(assign (a, e, i, j, k);printfc输出的三维数组为:n);f;i+)(for(j=0;j for(k)value (a, e, i, j, k);printf ”,e);j

36、printf (zznz,);printf(n);return 0;)6- 1快速转至ttdefinemaxsize 12500typedef int elemtype;typedef int status;typedef struct int i, j;elemtype e;triple;typedef struct tripledatemaxsize+1;int mu, nu, tu;tsmatrix;statusfasttransposcsmatrix(tsmatrixm, tsmatrixint col, p, t, q;intnumm. nu, epotm. nu;t. mu=m.

37、mu;t. nu二m. nu;t.tu=m. tu;if(t. tu) for(co. nu;+col)numcol二0;=m. tu;+t)+numm. datet. j;epot1=1;for(=m. rm;+col) epotcol=cpotcoll+numcol-l;for (p;卄p) col=m. datep. j;q二cpotcol;t. dateq. i=m. datep. j;t. dateq. j=m. datep. i;t dateq e二m. datep e;+cpotcol;return 1;)int main()(tsmatrix m;tsmatrixt;intp;

38、scanf ("%d, %d, u);for. tu; p+)seanf("%ddatep i, p e);fasttranspososmatrix(m, t);for (p二p+)(printf("%d, %d. datep.j.e);printf (rt);6-2矩阵加法sdefine maxsize12500typedef int status;typedef int elemtype;typedef struct(inti,j;elemtype e;triple;typedef struct(triple datemaxsize+l;intmu, nu,

39、tu;tsmatrix;status creatsmatrix(tsmatrix *m)/建立三元组int row, col, date, k;printf (请输入行数列数和非零元个数n);scanf ("%d, %d, %d", *m). tu);whle( (0 | | (*m)(*m).tu >( (*m)mu* (*m).nu) | | (*m). tu > maxsize) printf (,z输入不正确,请重新输入n); fflush (stdin);scanf ("%d). tu);(*m). dateo. i= 0;for ( k =

40、 1;tu ; k+)printfc请输入每个非零元素的行号,列号,数值n);seanf("%date);(*m). datek. i=row;(*m)datek. j = col;(*m). datek. e = date;printf (输入非空元素组成的三元组完毕! n);returnok;status comp( int a, int b)/比较两个数字的大小addsmatrix函数使用int i;)i= 1;if( a = b)i= 0;if( a > b)i =-l;return i;status addsmatrix( tsma, tsmatrixc)/矩阵的相加

41、(triple *ap, *bp, *ae, *be, *ch, *ce;int i;if ( a. mu!= b. mu | |a. nu != b. nu)/矩阵相加条件是行列必须相等 (printf(na andb is notcompared'n");return error;if( (*c). date )free( (*c). date );(*c) mu = a. mu;(*c) .nu 二 a. nu;apl;/ap的初值指向矩阵a的非零元素的首地址 tel;/bp的初值指向矩阵b的非零元素的首地址 .da.tea. tu; /ae指向矩阵八的非零元素尾地址

42、dateb. tu;/bo指向矩阵b的非零元素尾地址 ch = ce =(*c). date;/ch co的初值指向矩阵c的非零元素首地址的前一地址 wh i1e ( ap ce+; switch( comp( ap->i, bp->i ) case 1: *ce = *ap; ap+;break;case -1:*ce =*bp; bp+; break;case 0:switch( comp(ap->j, bp->j)case 0:*ce = *ap;ce->e += bp->e;if(!ce->e ) ce;ap+;bp+; break;case

43、1: *ce = *ap; ap+;break;case -1: *ce = *bp; bp+; break;if( ap >ae)/矩阵a的元素全部处理完毕while( bpco+;*ce二 *bp;bp+;if (bp > be)/矩阵b的元素全部处理完毕while(ce+;*ce = *ap;ap+;(*c).tu = ce- ch;矩阵c的非零元素个数return ok;status print(t)(int q;for(q=l;q)printf("%4d%4d%4dn, n. dateq. i, n. dateq. j, n. dateq. e);return

44、ok;status destroysmatrix ( tsmatr/必须使用&接收否则销毁不了销毁稀疏矩阵的三元组顺序表if ( m. m| | m. tu >m. mu * m. rm) /负负相乘能得正数(printf (z,n不存在矩阵! rt);return error;for ( int k =tu + 1; k+)m. datek. i = 0;m. datek. j= 0;m. datek. c = 0;m. mu = 0;m. nu 二 0;m.tu =0;printf cn 销毁完毕!n);return ok;)int niainotsmatrix a, b,

45、c;printfc建立矩阵a);creatsmatrixprintf (/zn建立矩阵b");creatsm);printf (,zna+bn,/);printf cn矩阵m和b相加为:n");addsmatrix (;print (c);destroysmatrix(a );destroysmatrix( b );destroysmatrix( c);return 0;7- 1二叉树查找typedef int telemtype;typedef int status;ttdcfine max tree size 100typedef telemtype sqbitreem

46、ax_tree_stze;sqbitree bt;status sreatebitree (sqbity)int i, x;i=0;doscanf("%d"bti=x;i+;while(x!=-l);y=i;return y;status parent(sqbitreet, int i, int e)(if(i=l) e=0;else e=i/2;return e;status leftchid(sqbitree t, int i, intn, int e)(if(2*i>n) e二0;else o=2*i;return e;)status rightch订e(sqb

47、itree t, int i, int n, inte) if(2*i+l>n)e=0;else e=2*i+l;return e;int main ()sqbitree t;int i, e, y, t;/t代表结点数 t=sreatebitree (t,t);printfc请输入位置:);seanf(y 二 par ent (t, i, e);printfc该结点的双亲是:”);printf ("%dn", bt yt);y=leftchild(t, i, t, e);printfc该结点的左孩子是:); printf ("%dn", btyt

48、); y二rightchile(t, i,t,e);printf c该结点的右孩子是:);printf ("%dn", bty-1);return 0;)8- 1二叉树ttdefinestack init size 100define stackincrement 10typedef char telemtype;typedef charselemtype;typedef int status;typodof struct bitnode(telemtype data;struct bitnode*lchild, *rchild;bitnode, *bitree;typod

49、of struetselemtype *base;selemtype *top;int stacksize;sqstack;status createbitree(bitre(char ch;scanf("%if (ch = , ') t =null;elseif(! (t = (bitnode *)malloc(sizeof(bitnode) exit(overflow);createbitree(t->lchiid);createbitree(t->rchiid);returnok;status initstack(sqstacks. base=(selemt

50、ype *)malloc(stack init size* sizcof(selemtype); i f (!s. base)ex it(overflow);s top=s. base;s. stacksize=stack_init_size;returnok;status push(sqslemtype e)if (s. top - s. base >= s. stacksizo)s. base= (selemtype*)realloc(s. base, (s.stacksize+stackincrement) * si zeof(selemtype);if (!s. base)exi

51、t(overflow);s. top=s. base+s. stacksize;s. stacksize+=stackincrement;*s.top+二e;return ok;status gettop(sqstacks,selemtypif (s. top=s. base) roturn error;e=*(s top-1);returnok;statuspop(sqsta if (s. top=s. base) returnerror; e二*-s. top;return ok;status stackempty (sqsteic if (s.base=s. top) return er

52、ror;statusinordcrtravcrse(bitree t, status (* visit)(telemtypc c) sqstack s;bitreet;initstack(s);push(s, t);while (!stackempty(s)while (gettop(s, ppush(s, p->lchild);pop(s, p);if (!stackempty(s)pop(s, p);if (!visit(p->data)return error;push(s, p->rchild);)return ok;int 】nain()bitnode root;b

53、itree tree;tcreatebitree(troe);return 0;9 -1哈夫曼树intfunctionl(char ch, char *s) int i;for(i=0;si!二'0, ; i+)if(ch=sij)return 0;return 1;typedef struet unsigned intweight; unsigncdint parent,lchild,rchild; htnode, *huffmantree;typedef char *huffmancode; intmin(huffmantree t, int i) int j, flag;unsigned int

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论