数据结构矩阵

#include <stdio.h>
#include <stdarg.h>
#define MAXSIZE 10000

static const int  MAX_DIM = 100;
typedef int element;
typedef struct {element *base; int dim; int *bounds;int *constants;} array;

//矩阵
typedef struct{ int i,j ; element e ; } triple;
typedef struct{ int mu,nu,tu ; triple *data ; } smatrix;

void init(array * , int , ...);
void destory(array *);
int locate(array * , va_list );
element value(array * , ... );
void assign(array * , element , ...);

//矩阵函数 
void jz_init(smatrix * , array * );
void jz_print(smatrix * );
void jz_trans(smatrix * , smatrix *);
void jz_trans1(smatrix * , smatrix *);
int main()
{
 array arr;
 init(&arr , 2 , 10 , 10 );
 
 assign(&arr , 22 , 0 , 0);
 assign(&arr , 14 , 0 , 1);
 assign(&arr , 3 , 1 , 1);
 assign(&arr , 4 , 2 , 2);
 assign(&arr , -1 , 5 , 4);
 assign(&arr , 2 , 6 , 3);
 assign(&arr , 18 , 9 , 0);

 printf("-------------------------------------------------\n");
 smatrix smx;
 jz_init(&smx , &arr);
 jz_print(&smx);
 printf("-------------------------------------------------\n");
 smatrix smxt;
 jz_trans(&smx , &smxt);
 jz_print(&smxt);
 printf("-------------------------------------------------\n");
 smatrix smxtt;
 jz_trans(&smx , &smxtt);
 jz_print(&smxtt);
 printf("-------------------------------------------------\n");
 
 
 
 getchar();
 return 0;
}

void init(array *arr ,int dim , ...)
{
 va_list ap;
 if(dim < 1 || dim > MAX_DIM) { printf("error dim");getchar();}
 arr->dim = dim;
 arr->bounds = (int *)malloc(dim * sizeof(int));
 
 int ele_total = 1;
 va_start(ap,dim);
 int i;
 for(i=0 ; i<dim ; i++ )
 {
  *(arr->bounds+i) = va_arg(ap , int);
  if( *(arr->bounds+i) < 0 )
  {
   printf("error bounds");
   getchar();
  }
  ele_total *= *(arr->bounds+i);
 }
 arr->base = (element *)malloc(ele_total * sizeof(element));
 
 for (i = 0 ; i < ele_total ; i++ )
 {
  *( arr->base+i ) = 0;
 }
 
 arr->constants = (int *)malloc(dim * sizeof(int));
 *( arr->constants+(dim-1) ) = 1;
 for (i = dim -2 ; i >= 0 ; --i)
 {
  *( arr->constants+i ) = (*( arr->bounds+i+1 )) * (*( arr->constants+i+1 ));
 }
}

void destory(array *arr)
{
 free(arr->base);
 free(arr->bounds);
 free(arr->constants);
}

int locate(array *arr , va_list ap )
{
 int off =0;
 int i;
 int ind;
 for(i = 0 ; i < arr->dim ; ++i)
 {
  ind = va_arg(ap , int );
  if(ind < 0 || ind >= *(arr->bounds+i) )
  {
   printf("unmatch -- array bounds");
   getchar();       
  }
  off += (*(arr->constants+i)) * ind; 
 }
 return off;
}

element value(array *arr , ... )
{
 va_list ap;
 va_start(ap,arr);
 return *(arr->base + locate(arr , ap));
}

void assign(array *arr , element e , ...)
{
 va_list ap;
 va_start(ap,e);
 *(arr->base + locate( arr , ap ) ) = e ;
}


//矩阵方法 
void jz_init(smatrix * smx , array * arr)
{
 smx->data = (triple *) malloc ( sizeof(triple) * MAXSIZE );
 smx->mu = *(arr->bounds);
 smx->nu = *(arr->bounds+1);
 
 int i , j , count;
 count = 0 ;
 for(i=0 ; i < *(arr->bounds) ; i++)
 {
  for(j=0 ; j < *(arr->bounds+1) ; j++)
  {
   if(value(arr , i , j) != 0)
   {
    triple tpe;
    (&tpe)->i = i;
    (&tpe)->j = j;
    (&tpe)->e = value(arr , i , j);
    *(smx->data+count) = tpe;
    count++;
   }
  }
 }
 
 smx->tu = count;
}

void jz_print(smatrix * smx)
{
 int i;
 for (i = 0 ; i < smx->tu ; i++)
 {
  printf("|   row:%6d  |   col:%6d  |   val:%6d  |\n" , (smx->data+i)->i , (smx->data+i)->j , (smx->data+i)->e );
 }
}

void jz_trans(smatrix *smx , smatrix *smxt)
{
 smxt->data = (triple *) malloc ( sizeof(triple) * MAXSIZE );    
 smxt->tu = smx->tu;
 smxt->mu = smx->nu;
 smxt->nu = smx->mu;
 
 int i,col,count;
 count = 0 ;
 for(col = 0 ; col < smx->nu ; col++)
 {
  for(i = 0 ; i < smx->tu ; i++)
  {
   if((smx->data+i)->j == col )
   {
    (smxt->data+count)->i = (smx->data+i)->j;
    (smxt->data+count)->j = (smx->data+i)->i;
    (smxt->data+count)->e = (smx->data+i)->e;
    ++count;
   }
  }
 }
}

void jz_trans1(smatrix *smx , smatrix *smxt)
{
 smxt->data = (triple *) malloc ( sizeof(triple) * MAXSIZE );    
 smxt->tu = smx->tu;
 smxt->mu = smx->nu;
 smxt->nu = smx->mu;
 
 int i,*num , *pos;
 for(i = 0 ; i < smx->nu ; i++) *(num+i) = 0;
 for(i = 0 ; i < smx->tu ; i++) ++(*(num+(smx->data+i)->j));
 *(pos) = 1;
 for(i = 1 ; i < smx->nu ; i++) *(pos+i) = *(pos+i-i) + *(num+i-1);
 for (i = 1 ; i < smx->tu ; i++)
 {
  int tmp = (smx->data+i)->j;
  (smxt->data+tmp)->i = (smx->data+i)->j;
  (smxt->data+tmp)->j = (smx->data+i)->i;
  (smxt->data+tmp)->e = (smx->data+i)->e;
  ++((smx->data+i)->j);
 }
}

该矩阵的初始化基于二维数组模型

jz_trans 和 jz_trans1 为两种转置矩阵的方法

jz_trans1的算法复杂度在非零元素越多的情况下,越接近经典转置

以上全是个人理解,呵呵。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值