數(shù)據(jù)結(jié)構(gòu)上機編程匯總
《數(shù)據(jù)結(jié)構(gòu)上機編程匯總》由會員分享,可在線閱讀,更多相關《數(shù)據(jù)結(jié)構(gòu)上機編程匯總(36頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、數(shù)據(jù)結(jié)構(gòu)上機編程匯總
2-1鏈表
#include
2、ype data; struct LNode *next; }LNode, *LinkList; Status GetElem_L(LinkList L, int i, ElemType &e){ //L涓哄甫澶寸粨鐐圭殑鍗曢摼琛ㄧ殑澶存寚閽堛€? //褰撶i涓厓绱犲瓨鍦ㄦ椂錛屽叾鍊艱祴緇檈騫惰繑鍥濷K錛屽惁鍒欒繑鍥濫RROR LinkList *p; int j; p = L->next; j = 1; //鍒濆鍖栵紝p鎸囧悜絎竴涓粨鐐癸紝j涓鴻鏁板櫒 while(p && j
3、鍚庢煡鎵撅紝鐭儔亾p鎸囧悜絎噼煱饏鷯绱犳垨p煱擑┖ p = p->next; ++j; } if(!p || j>i) return ERROR; //絎噼煱饏鷯绱鸞笉鄆姃湪 e = p->data; //鍙瀬i鍏冪礌 return OK; }// GetElem_L Status ListInsert_L(LinkList &L, int i, ElemType e){ //鍦ㄥ甫澶寸粨鐐圭殑鍗曢摼綰挎€觥↙涓i涓綅緗箣鍓嶆彃鍏儑殑鍏檖礌e LinkList *p, *s; p = L;
4、j = 0;
while(p && j
5、i, ElemType &e){
//鍦ㄥ甫澶寸粨鐐圭殑鍗曢摼綰挎€觥↙涓紝鍒犻櫎絎琲涓厓绱狅紝騫舵湁e榪斿洖鍏跺€? LinkList *p,*q;
int j;
p = L; j= 0;
while(p->next && j
6、? e = q->data; free(q); return OK; }// ListDelete_L void CreateList_L(LinkList &L, int n){ //閫嗕綅搴忚緭鍏涓厓绱犵殑鍊鹼紝寤虹珛甯﹁〃澶寸粨鐐圭殑鍗曢摼綰挎€觥↙ LinkList *p; L = (LinkList)malloc(sizeof(LNode)); L->next = NULL;//鍏堝緩绔嬩竴涓甫澶寸粨鐐圭殑鍗曢摼琛? for(i = n; i> 0; --i){ p=(LinkList)mall
7、oc(sizeof(LNode));//鐢熸垚鏂扮粨鐐? scanf("%d",&p->data); p->next = L->next; L->next = p; } }//CreateList_L void MergeList_L(LinkList &La, LinkList &Lb, LinkList &Lc){ //宸茬煡鍗曢摼綰挎€觥↙a鍜孡b鐨勫厓绱犳寜鍊奸潪閫掑噺鎺掑垪 //褰掑茍La鍜孡b寰楀埌鏂扮殑鍗曢摼綰挎€觥↙c錛孡c鐨勫厓绱犱篃鎸夊€奸潪閫掑噺鎺掑垪 LinkList *pa , *pb , *
8、pc; pa = La->next; pb = Lb->next; Lc = pc = pa;//鐢↙Aa鐨勫ご緇撶偣浣滀負Lc鐨勫ご緇撶偣 while(pa && pb){ if(pa->data <= pb->data){ pc->next = pa; pc = pa; pa = pa->next; } else{pc->next = pb; pc = pb; pb = pb->next; } } pc->next = pa ? pa:pb;//鎻掑叆鍓╀綑孌?
9、 free(Lb);//閲婃斁Lb鐨勫ご緇撶偣 }//MergeList_L int LocateELem_SL(SLinkLIst S, ElemType e) { i = S[0].cur; while (i && S[i].data != e)i = S[i].cur; return i; } 2-2線性表 #define LIST_INIT_SIZE 100 //綰挎€觥ㄥ瓨鍌ㄧ┖闂寸殑鍒濆鍒嗛??#define LISTINCREMENT 10//綰挎€觥ㄥ瓨鍌ㄧ┖闂寸殑鍒嗛厤澧為??#define OK 1 typede
10、f int Status; typedef int ElemType; typedef struct { ElemType *elem;//瀛樺偍絀洪棿鍩哄潃 int length;//褰撳墠闀垮害 int listsize;//褰撳墠鍒嗛厤鐨勫瓨鍌ㄥ閲?}SqList; SqList L , newbase; SqList *q , *p , *pa , *pb , *pc , *pa_last , *pb_last; Status InitList_Sq(SqList &L)//鏋勯€犱竴涓┖鐨勭嚎鎬觥↙ { L.elem=(E
11、lemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L.elem) exit(OVERFLOW); L.length = 0; L.listsize = LIST_INIT_SIZE; return OK; } //InitList_Sq Status ListInsert_Sq(SqList &L, int i, ElemType e) //鍦ㄩ『搴忕嚎鎬觥↙涓i涓綅緗箣鍓嶆彃鍏ユ柊鐨勫厓绱爀銆? //i鐨勫拰娉曞€間負1銆?i銆?ListLength_
12、Sq(L)+1 { if((i<1)||(i>L.length+1)) return ERROR;//i鍊間笉鍚堟硶 if(L.length>=L.listsize){//褰撳墠瀛樺偍絀洪棿宸叉弧錛屽鍔犲垎閰? newbase = (ElemType*)malloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(OVERFLOW); //瀛樺偍鍒嗛厤澶辮觸 L.elem = newbase; //鏂板熀鍧€
13、L.listsize+ = LISTINCREMENT; //澧炲姞瀛樺偍瀹歸噺 } q = &(L.elem[i-1]); //q涓烘彃鍏ヤ綅緗? for(p = &(L.elem[L.length-1]);p >= q; --p) *(p+1) = *p; //鎻掑叆浣嶇疆鍙婁箣鍚庣殑鍏冪礌鍙崇 *q = e; //鎻掑叆e ++L.length; //琛ㄩ暱澧? return OK; }//ListInsert_Sq Status ListDelete_Sq(SqList &L, int i, ElemType &e){
14、 //鍦ㄩ『搴忕嚎鎬鰜VL煱胥垹闄傚馣i煱厓绱狅紝騫剁敤e榪斿洖鍏跺€? //i鐨勫悎娉曞€間負1銆?i銆?ListLength_Sq(L) if((i<1)||(i>L.length)) return ERROR; //i鍊間笉鍚堟硶 p = &(L.elem[i-1]); //p涓鴻鍒犻櫎鍏冪礌鐨勪綅緗? e = *p; //琚垹闄傘鷯绱犵殑鍊艱祴緇檈 q = L.elem+L.length-1; //琛ㄥ鄴鍏檖礌鐨勪綅緗? for(++p;p<=q;++p) *(p-1)=*p; //琚垹闄ゅ厓绱犱箣鍚庣殑鍏冪礌宸︾
15、 --L.length; //琛ㄩ暱鍑? return OK; } //ListDelete_Sq int LocateElem_Sq(SqList L, ElemType e, Status (*compare)(ElemType , ElemType)){ //鍦ㄩ『搴忕嚎鎬觥↙涓煡鎵劇1涓€奸亣e婊凍compare()鐨勫厓绱犵殑浣嶅簭 //鑻儐氒鍒幫紝鍒烺繑鍥炲叾鍦↙煱皴殑煰嶅簭錛屽惁鍒烺繑鍥? int i = 1; //i鐨勫垵鍊間負絎?煱饏鷯绱犵殑潒忎綅 p = L.elem; //p鐨勫垵鍊間負絎?煱厓绱犵殑瀛樺偍
16、浣嶇疆 while(i<=L.length && !(*compare)(*p++,e)) ++i; if(i<=L.length) return i; else return 0; }//LocateElem_Sq void MergeList_Sq(SqList La, SqList Lb, SqList &Lc){ //宸茬煡欏哄簭綰挎€觥↙a鍜孡b鐨勫厓绱犳寜鍊奸潪閫掑噺鎺掑垪 //褰掑茍La鍜孡b寰楀埌鏂扮殑欏哄簭綰挎€觥↙c錛孡c鐨勫厓绱犱篃鎸夊€奸潪閫掑噺鎺掑垪 pa = La.elem; pb = Lb.elem;
17、 Lc.listsize = Lc.length = La.length + Lb.length; pc = Lc.elem = (ElemType*)malloc(Lc.listsize*sizeof(ElemType)); if(!Lc.elem) exit(OVERFLOW); //瀛樺偍鍒嗛厤澶辮觸 pa_last = La.elem + La.length-1; pb_last = Lb.elem + Lb.length-1; while(pa <= pa_last&&pb <= pb_last){ //褰掑茍
18、 if(*pa <= *pb) *pc++ = *pa ++; else *pc++ = *pb++; } while(pa <= pa_last) *pc++ = *pa++; //鎻掑叆La鍓╀綑鐨勫厓绱? while(pb <= pb_last) *pc++ = *pb++; //鎻掑叆Lb鍓╀綑鐨勫厓绱?} //MergeList_Sq 3-2迷宮 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 //寤轟竴涓┖鏍? void initstack( sqstack
19、 &s){ s.base=(int *)malloc(SIZE*sizeof(int)); if(!s.base){printf("\n鍐呭瓨涓嶈凍\n");exit(0);} s.top=s.base; s.stacksize=SIZE; } int pop( sqstack &s,int *e){ if(s.top==s.base)return 0; *e=*(--s.top); return 1; } void push( sqstack &s,int e){ if(s.top-s.base>=s.stacksize){ s.base
20、=(int *)realloc(s.base, (s.stacksize+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 &s,int *e){ if(s.top==s.base)return 0; *e= *(s.top-1); return 1; } struct migong{ int flag; int dir
21、; }; struct migong a[10][10]; int masepath(int i,int j,int m,int n,sqstack &s){ if(a[i][j].flag==1){printf("\n鍏儏彛閿烺顰\n");return 0;} do { if (i==m&&j==n){push(s,i);push(s,j);return 1;} else { if(a[i][j].dir==1){ if(a[i][j+1].flag==1) a[i][j].dir++; else { push
22、(s,i);push(s,j);a[i][j].flag=1;j++;continue; } } if(a[i][j].dir==2){ if(a[i+1][j].flag==1) a[i][j].dir++; else { push(s,i);push(s,j);a[i][j].flag=1;i++;continue; } } if(a[i][j].dir==3){ if(a[i][j-1].flag==1) a[i][j].dir++; else {
23、 push(s,i);push(s,j);a[i][j].flag=1;j--;continue; } } if(a[i][j].dir==4){ if(a[i-1][j].flag==1) a[i][j].dir++; else { push(s,i);push(s,j);a[i][j].flag=1;i--;continue; } } else{ a[i][j].flag=1; pop(s,&j);pop(s,&i); a[i][j].flag=0;
24、 } } } while(s.top!=s.base); printf("\n娌亷湁鍑鴻礬\n"); return 2; } void main(){ int m,n,x,y,i,j,r=1; sqstack s1,s2; initstack(s2); initstack(s1); int b[10][10]={ {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},
25、 {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}, {1,1,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}, }; printf("馭€寤鴻糠瀹涓嬪浘錛屽叾涓?琛ㄧ偢澧欙紝0琛ㄧ偢閫
26、氳礬\n"); printf(" 0 1 2 3 4 5 6 7 8 9\n\n"); for(i=0;i<10;i++){ printf(" %d ",i); for(j=0;j<10;j++){ a[i][j].flag=b[i][j]; printf("%d ",b[i][j]); a[i][j].dir=1; } printf("\n"); } printf("璇瘋緭鍏儏嚭鍙倓綅緗甛n"); scanf("%d",&m); scanf("%d",&n); m
27、asepath(1,1,m,n,s1); while(s1.top!=s1.base){ pop(s1,&x); push(s2,x); } printf("\n幃斅嚭榪曯鐨勮礬寰勪負:\n"); while(s2.top!=s2.base){ pop(s2,&x);pop(s2,&y); printf("< %d,%d> ",x,y); if(r++%5==0) printf("\n"); } } 4-1括號匹配 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10
28、 typedef char SElemType; typedef int Status; typedef struct { SElemType *base; SElemType *top; int stacksize; }SqStack; Status InitStack(SqStack &S) { S.base=(SElemType * )malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S.base)exit(OVERFLOW); S.top=S.base;
29、 S.stacksize=STACK_INIT_SIZE; return OK; } Status Push(SqStack &S,SElemType e) { if(S.top-S.base>=S.stacksize) { S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType)); if(!S.base)exit(OVERFLOW); S.top=S.base+S.sta
30、cksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; } Status Pop(SqStack &S,SElemType &e) { if(S.top==S.base)return ERROR; e=*--S.top; return OK; } Status StackEmpty (SqStack S) { if(S.top==S.base) return TRUE; else
31、 return FALSE;
}
Status Match(char a[],SqStack &S)
{
int i ,n;
n=strlen(a);
for(i=0;i 32、if(*(S.top-1)==( && a[i]==))
Pop(S,*(S.top-1));
}
if(StackEmpty (S))
return TRUE;
else return FALSE;
}
int main()
{
SqStack S;
InitStack(S);
char c[STACK_INIT_SIZE];
gets(c);
if(Match(c,S)==1) printf("RIGHT");
else printf("WRON 33、G");
return 0;
}
4-2行編輯
#define STACK_INIT_SIZE 100 //瀛樺偍絀洪棿鍒濆鍒嗛厤閲?#define STACKINCREMENT 10 //瀛樺偍絀洪棿鍒嗛厤澧為噺
typedef int Status;
typedef char SElemType;
typedef struct{
SElemType *base; //鍦ㄦ爤鏋勯€犱箣鍓嶅拰閿€姣佷箣鍚庯紝base鐨勫€間負NULL
SElemType *top; //鏍堥《鎸囬拡
int stacksize; // 34、褰撳墠宸插垎閰嶇殑瀛樺偍絀洪棿錛屼互鍏冪礌涓哄崟浣?}SqStack;
Status InitStack(SqStack &S){
//鏋勯€犱竴涓┖鏍圫
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base) exit(OVERFLOW); //瀛樺偍鍒嗛厤澶辮觸
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//InitStack
Status Push(Sq 35、Stack &S,SElemType e){
//鎻掑叆鍏冪礌e涓烘柊鐨勬爤欏跺厓绱? if(S.top-S.base>=S.stacksize){//鏍堟弧錛岃拷鍔犲瓨鍌ㄧ┖闂? S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base) exit(OVERFLOW);//瀛樺偍鍒嗛厤澶辮觸
S.top=S.base+S.stacksize;
S.stacksize+=STACK 36、INCREMENT;
}
*S.top++=e;
return OK;
}//Push
Status Pop(SqStack &S,SElemType &e){
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}//Pop
Status ClearStack(SqStack &S){
S.top = S.base;
return OK;
}
Status DestroyStack(SqStack &S){
free(S.base);
37、
S.base = NULL;
S.top = NULL;
S.stacksize = 0;
return OK;
}
Status StackTraverse(SqStack S,Status(* visit)())
{
return OK;
}
Status visit(SqStack &S)
{
if(S.base==S.top) return ERROR;
while(S.base!=S.top){
printf("%c",*(S.base));
S.base+ 38、+;
}
return OK;
}
int LineEdit()
{
SqStack S;
char ch,c;
InitStack(S);
while(ch!=EOF){
while(ch!=EOF && ch!=\n)
{
switch(ch)
{
case # : Pop(S,c);break;
case @ : ClearStack(S);break;
default 39、: Push(S,ch);break; }
ch=getchar( ); }
visit(S);
ClearStack(S);
if(ch!=EOF)
ch=getchar( );
}
DestroyStack(S);
return 0;
}// LineEdit
5-1數(shù)組
#define MAX_ARRAY_DIM 8
typedef int ElemType;
typedef int Statu 40、s;
typedef struct
{
ElemType *base;
int dim;
int *bounds;
int *constants;
}Array;
Status InitArray(Array &A,int dim,...)
{
int elemtotal,i;va_list ap;
if(dim<1||dim>MAX_ARRAY_DIM)
return ERROR;
A.dim=dim;
A.bounds=(int *)malloc(dim*sizeof(int)) 41、;
if(!A.bounds) exit(OVERFLOW);
elemtotal=1;
va_start(ap,dim);
for(i=0;i 42、emType));
if(!A.base) exit(OVERFLOW);
A.constants=(int *)malloc(dim*sizeof(int));
if(!A.constants)exit(OVERFLOW);
A.constants[dim-1]=1;
for(i=dim-2;i>=0;--i)
A.constants[i]=A.bounds[i+1]*A.constants[i+1];
return OK;
}
Status DestroyAttay(Array &A)
{
if( 43、!A.base)
return ERROR;
free(A.base);
A.base=NULL;
if(!A.bounds)
return ERROR;
free(A.bounds);A.bounds=NULL;
if(!A.constants) return ERROR;
free(A.bounds);A.constants=NULL;
return OK;
}
Status Locate(Array A,va_list ap,int &off)
{
int i 44、nd,i;
off=0;
for(i=0;i 45、;
if((result=Locate(A,ap,off))=0)return result;
e=*(A.base+off);
return OK;
}
Status Assign(Array &A,ElemType e,...)
{
va_list ap;int result,off;
va_start(ap,e);
if((result =Locate(A,ap,off))<=0)return result;
*(A.base+off)=e;
return OK;
}
int main()
{ 46、
int DIM=2,dim1=3;
Array A;
int x,y,i,j,k,e;
int a,b,c;
printf("請輸入二維數(shù)組的維數(shù):");
scanf("%d %d",&x,&y);
InitArray(A,DIM,x,y);
for(i=0;i 47、二維數(shù)組為:\n");
for(i=0;i 48、 for(k=0;k 49、 }printf("\n");
}
return 0;
}
6-1快速轉(zhuǎn)至
#define MAXSIZE 12500
typedef int ElemType;
typedef int Status;
typedef struct{
int i,j;
ElemType e;
}Triple;
typedef struct{
Triple date[MAXSIZE+1];
int mu,nu,tu;
}TSMatrix;
Status FastTransposeSMatrix(TSMatrix 50、 M,TSMatrix &T){
int col,p,t,q;
int num[M.nu],cpot[M.nu];
T.mu=M.mu;
T.nu=M.nu;
T.tu=M.tu;
if(T.tu){
for(col=1;col<=M.nu;++col) num[col]=0;
for(t=1;t<=M.tu;++t) ++num[M.date[t].j];
cpot[1]=1;
for(col=2;col<=M.nu;++col) cpot[col]=cpo 51、t[col-1]+num[col-1];
for(p=1;p<=M.tu;++p){
col=M.date[p].j;
q=cpot[col];
T.date[q].i=M.date[p].j;
T.date[q].j=M.date[p].i;
T.date[q].e=M.date[p].e;
++cpot[col];
}
}
return 1;
}
int main()
52、
{
TSMatrix M;
TSMatrix T;
int p;
scanf("%d,%d,%d",&M.mu,&M.nu,&M.tu);
for (p=1;p<=M.tu;p++)
{
scanf("%d %d %d",&M.date[p].i,&M.date[p].j,&M.date[p].e);
}
FastTransposeSMatrix(M,T);
for (p=1;p<=M.tu;p++)
{
printf("%d,%d,%d",&T.date[p].i 53、,&T.date[p].j,&T.date[p].e);
printf("\n");
}
}
6-2矩陣加法
#define MAXSIZE 12500
typedef int Status;
typedef int ElemType;
typedef struct
{
int i,j;
ElemType e;
}Triple;
typedef struct
{
Triple date[MAXSIZE+1];
int mu,nu,tu;
}TSMatrix;
Sta 54、tus CreatSMatrix(TSMatrix *M)
//建立三元組
{
int row,col,date,k;
printf("請輸入行數(shù)列數(shù)和非零元個數(shù)\n");
scanf("%d,%d,%d",&(*M).mu,&(*M).nu,&(*M).tu);
while( (*M).mu <= 0 || (*M).nu <= 0 || (*M).tu > ( (*M).mu * (*M).nu ) || (*M).tu > MAXSIZE)
{
printf("輸入不正確,請重新輸入\n");
ff 55、lush(stdin);
scanf("%d,%d,%d",&(*M).mu,&(*M).nu,&(*M).tu);
}
(*M).date[0].i = 0;
for( k = 1; k <= (*M).tu ; k++)
{
printf("請輸入每個非零元素的行號,列號,數(shù)值\n");
scanf("%d,%d,%d",&row,&col,&date);
(*M).date[k].i = row;
(*M).date[k].j = col;
56、(*M).date[k].e = date;
}
printf("輸入非空元素組成的三元組完畢!\n");
return OK;
}
Status comp( int a, int b)
//比較兩個數(shù)字的大小AddSmatrix函數(shù)使用
{
int i;
if( a < b)
i = 1;
if( a == b)
i = 0;
if( a > b)
i = -1;
return i;
}
Status AddSMatrix( TSMatri 57、x &A, TSMatrix &B, TSMatrix *C)
//矩陣的相加
{
Triple *Ap,*Bp,*Ae,*Be,*Ch,*Ce;
int i;
if( A.mu != B.mu || A.nu != B.nu) //矩陣相加條件是行列必須相等
{
printf("\nA and B is not compared\n");
return ERROR;
}
if( (*C).date )
free( (*C).date );
58、(*C).mu = A.mu;
(*C).nu = A.nu;
Ap = &A.date[1]; //Ap的初值指向矩陣A的非零元素的首地址
Bp = &B.date[1]; //Bp的初值指向矩陣B的非零元素的首地址
Ae = &A.date[A.tu]; //Ae指向矩陣A的非零元素尾地址
Be = &B.date[B.tu]; 59、 //Be指向矩陣B的非零元素尾地址
Ch = Ce = (*C).date; //Ch Ce的初值指向矩陣C的非零元素首地址的前一地址
while( Ap <= Ae && Bp <= Be)
{
Ce++;
switch( comp( Ap->i, Bp->i ) )
{
case 1:
{ *Ce = *Ap; Ap++;}break;
case -1:
60、 {*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 1: { *Ce = *Ap; Ap 61、++;}break;
case -1: { *Ce = *Bp; Bp++;}} }break;
}
}
if( Ap > Ae) //矩陣A的元素全部處理完畢
while( Bp <= Be )
{
Ce++;
*Ce = *Bp;
Bp++;
}
if( Bp > Be) 62、 //矩陣B的元素全部處理完畢
while( Ap <= Ae)
{
Ce++;
*Ce = *Ap;
Ap++;
}
(*C).tu = Ce - Ch; //矩陣C的非零元素個數(shù)
return OK;
}
Status Print(TSMatrix &N)
{int q;
for(q=1;q<=N.tu;q++)
printf("%4d%4d%4d\n", N.date[q].i, 63、N.date[q].j,N.date[q].e);
return OK;
}
Status DestroySMatrix( TSMatrix &M) //必須使用&接收M,否則銷毀不了
//銷毀稀疏矩陣的三元組順序表
{
if( M.mu <= 0 || M.nu <= 0 || M.tu > M.mu * M.nu) //負負相乘能得正數(shù)
{
printf("\n不存在矩陣!\n");
return ERROR;
}
for( int k = 1; k < M.tu + 1; 64、 k++)
{
M.date[k].i = 0;
M.date[k].j = 0;
M.date[k].e = 0;
}
M.mu = 0;
M.nu = 0;
M.tu = 0;
printf("\n銷毀完畢!\n");
return OK;
}
int main()
{
TSMatrix A, B, C;
printf("建立矩陣A");
CreatSMatrix( &A );
printf("\n建立矩陣B");
65、 CreatSMatrix( &B );
printf("\nA+B\n");
printf("\n矩陣M和B相加為:\n");
AddSMatrix( A, B, &C );
Print(C);
DestroySMatrix( A );
DestroySMatrix( B );
DestroySMatrix( C );
return 0;
}
7-1二叉樹查找
typedef int TElemType;
typedef int Status;
#define MAX_TRE 66、E_SIZE 100
typedef TElemType SqBiTree[MAX_TREE_SIZE];
SqBiTree bt;
Status SreateBiTree(SqBiTree &T,int y)
{
int i,x;
i=0;
do
{
scanf("%d",&x);
bt[i]=x;
i++;
}while(x!=-1);
y=i;
return y;
}
Status Parent(SqBiTree T,int i,int e)
{
if(i==1) e=0;
else e=i/2;
return e;
}
Status LeftChild(SqBiTree T,int i,int n,int e)
{
if(2*i>n) e=0;
else e=2*i;
return e;
}
Status RightChile(SqBiTree T
- 溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。