当前位置:编程学习 > C/C++ >>

数据结构学习(稀疏矩阵的实现,三元组)

使用三元组实现稀疏矩阵的建立、逆转、乘法。

乘法没有使用附加向量,是自己想得。有错误请谅解。性能可能没有书上介绍的好。

代码如下:(本代码只经过本人的简单测试,可能存在问题。请相信自己的能力,敢于质疑。欢迎提供更好的、更快、更简洁的代码或者方法和指出错误。在ubuntu12.04使用gcc4.6.3版本编译,在vc中如出现错误,请谅解。)

[cpp] 
/*
*created by Reage at 2012 November 29
*description: 稀疏矩阵实现,包含乘法、逆转
*
*blog:http://blog.csdn.net/rentiansheng
*/ 
#include <stdio.h> 
#include <stdlib.h> 
 
#define LINE print_split(20) 
 
typedef struct triple{ 
    int i,j;//行列 
    int value; 
}triple;  
 
typedef struct matrix{  
    triple * head; 
    int row,column,count;//矩阵的行、列、稀疏矩阵的元素个数 
}matrix;  
 
/* 
*description:判断某一个位置是否已经存在元素
*parameter:
*   x:行坐标
*   y:列坐标
*   count:要查找的元素个数,如果为0表示全部,否则是count的值
*result:找到返回1,未找到返回0
*/ 
int find_ item (matrix *m, int x, int y, int count ){ 
    triple *tp = m->head; 
    if ( !count || count > m->count ) count =m->count; 
    int i; 
    for (i  = 0; i < count - 1; i++){ 
        if ( tp->i == x && tp->j == y) return 1;  
        tp++; 
    } 
    return 0; 

 
/*
*初始化稀疏矩阵
*/ 
void  init_matrix (matrix *m, int row, int column, int count){ 
//  if (m->head) free(m->head);//如果矩阵已经存在,释放原有的数据空间 
    m->head = (triple *) malloc ( count * sizeof (triple)); 
    if (!m->head){ 
        printf("Allocate memory failed!application will exit."); 
        exit(0); 
    } 
    m->row = row; 
    m->column = column; 
    m->count = count; 
 
}  
 
/*
*输入的稀疏矩阵的可能没有按照指定的排列顺序,用此来整理
*首先按行从小到大,当行等时候按列从小到大
*/ 
void sort_matrix (matrix *m){ 
    int i, j, count = m->count; 
    int loc; 
    triple * tp = m->head; 
    triple tmp; 
    for ( i = 0; i < count-1; i++){ 
        loc = i; 
        for (j = i+1; j < count; j++){ 
            if ( tp[loc].i > tp[j].i){//按行排序 
                loc = j; 
            } 
            else if (tp[loc].i == tp[j].i && tp[loc].j > tp[j].j){ 
                loc = j; 
            } 
        } 
        memcpy (&tmp, &tp[loc], sizeof(triple)); 
        memcpy (&tp[loc], &tp[i], sizeof(triple)); 
        memcpy (&tp[i], &tmp, sizeof(triple)); 
    } 

 
/*
*创建一个使用数组存储的稀疏矩阵
*/ 
void creat_matrix (matrix *m){ 
    int row,column,count;//存储稀疏矩阵的行、列、非零元素的个数 
    int i; 
    int x,y; 
    triple * use_triple; 
    int value; 
    printf ("please enter sparse matrix row,column:\n"); 
    scanf ("%d%d", &row, &column); 
    printf ("please enter a number as the number of non-zero elements of the sparse matrix:\n"); 
    scanf ("%d", &count); 
    init_matrix (m, row, column, count); 
    use_triple = m->head; 
    for (i = 0; i < count; i++){ 
        repeat:printf ("please enter  %d elements x and y coordinate values:", i+1); 
        printf ("and a number as  element  value:"); 
        scanf ("%d%d%d", &x, &y, &value); 
        if ( find_item (m, x, y, i+1) ) {printf("The position has been there exists an elemetn.\n");goto repeat;} 
        use_triple->i = x; 
        use_triple->j = y; 
        use_triple->value = value; 
        use_triple ++; 
    } 
    sort_matrix (m); 
}  
 
/*
*起到分割符的作用,输出count个‘—’符号
*/ 
void print_split (int count){ 
    int i; 
    for (i = 0; i < count; i++) 
            printf("-"); 
    printf("\n"); 

 
/*
*数据稀疏矩阵中的内容
*/ 
void print_matrix (matrix m){ 
    int len = m.count; 
    int i; 
    printf("\n"); 
    triple * sm = m.head; 
    LINE; 
    for (i = 0; i < len; i++){ 
        printf("%4d | %4d | %d\n", sm->i, sm->j, sm->value); 
        sm++; 
        LINE; 
    } 

 
/*
*description: 对稀疏矩阵逆转了,想将矩阵逆转,然后进行排序。
*paramtere:
*   sm:源稀疏矩阵
*   dm:目的稀疏矩阵
*/ 
void transpose_matrix1 (matrix *sm, matrix *dm){ 
    int i, j, count = sm->count; 
    triple *dt, *st = sm->head; 
    init_matrix (dm, sm->row, sm->column, sm->count);//对目的稀疏矩阵进行初始化。 
    dt = dm->head 
    ;for (i = 0; i < count; i++){ 
  &

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,