c语言单链表原地转置,.试分别用顺序表和单链表作为存储结构,实现将线性表(a0,a1,...an-1)就地逆置的操作,也就是反向。...

满意答案

dcebd7a0de6265b6ccae5ead692f1eab.png

fslsw

2013.07.27

dcebd7a0de6265b6ccae5ead692f1eab.png

采纳率:52%    等级:11

已帮助:8856人

#include

#include

#define MAXSIZE 20 /*矩阵中最大非零元的个数*/

typedef struct triple

{

int i; /*行标,本程序中从1开始的*/

int j; /*列标,本程序中从1开始的*/

int e; /*非零元*/

}Triple; /*三元组定义*/

typedef struct tabletype

{

int mu; /*矩阵的行数*/

int nu; /*列数*/

int tu; /*非零元个数*/

Triple data[MAXSIZE+1]; /*非零元的三元组表,*/

}Tabletype; /*三元组线性表*/

void out_matrix(Tabletype *); /*输出 矩阵*/

/*以下为转置程序,将a所指矩阵转置,将结果存入b所指的矩阵中*/

int TransposeSMatrix(Tabletype *,Tabletype *);

int main( void )

{

char ch;

Tabletype a= {6,7,8,{ {1,2,12},{1,3,9},{3,1,-3},{3,6,14},{4,3,24},{5,2,18},{6,1,15},{6,4,-7} }};

Tabletype b; /*声明矩阵b*/

printf("The source Matrix:\n");

out_matrix(&a);

if(TransposeSMatrix(&a,&b)) /*若a不为零矩阵则转置a,存入b中*/

{ printf("After TransposeSMatrix: \n");

out_matrix(&b);

}

else

{

printf("The matrix is zeros:\n");

out_matrix(&a);

}

do{

printf("Input 'q' to quit and ENTER run again:");

if((ch = getchar()) == 'q' || ch == 'Q')

exit(0);

}while(ch!='\n');

system("cls");

}

return 1;

}

void out_matrix(Tabletype *a) /* 打印矩阵*/

{

int i,j,k = 0;

for(i = 1 ;i <= a->mu; i++)

{

for(j = 1; j<= a->nu; j++)

{ /*判断是否为非零元*/

if((a->data[k].i == i)&&(a->data[k].j == j))

{

printf("%4d",a->data[k].e);

k++;

}

else

printf("%4d",0);

}

printf("\n");

}

}

int TransposeSMatrix(Tabletype *a,Tabletype *b)

{

int p,q,col;

b->mu = a->nu; /*原矩阵的行数为新矩阵的列数,愿列数为新行数,非零元个数不变*/

b->nu = a->mu;

b->tu = a->tu;

if(b->tu) /*若a不为零矩阵*/

{

q = 0; /*b->data下标*/

for(col = 1; col < a->nu; col++)

for(p = 0;p < a->tu;p++) /*p为a->data的下标*/

if(col == a->data[p].j) /*按b->data[q]中的列标对a->data[p]进行扫描*/

{

b->data[q].i = a->data[p].j;

b->data[q].j = a->data[p].i;

b->data[q].e = a->data[p].e;

q++;

}

return 1;

}

else /*a为零矩阵*/

return 0; getch();

}

17分享举报

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值