源代码——三元组实现稀疏矩阵及其转置
来源:互联网 发布:软件的开发 编辑:程序博客网 时间:2024/05/16 07:07
//三元组数值有一个特点:那就是在不同位置上的行值相同的元素//一定是按照列值升序出现的。#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct TRIPLE//三元组成员{int row;int col;int value;}TRIPLE;typedef struct TRIPLE_HEAD//三元组头{int rowCount;int colCount;int elementCount;TRIPLE *values;}TRIPLE_HEAD;TRIPLE_HEAD *initTriple();void destoryTriple(TRIPLE_HEAD *triHd);void showTriple(TRIPLE_HEAD triHd);TRIPLE_HEAD *revange(TRIPLE_HEAD trip);TRIPLE_HEAD *revange(TRIPLE_HEAD trip){TRIPLE_HEAD *ntrip = NULL; //newint t;//tempint *ass;//辅助数组int i;int index;ass = (int *)malloc(sizeof(int)* (trip.colCount + 1));memset(ass, 0, sizeof(int) * (trip.colCount + 1));//数组清零for (i = 0; i < trip.elementCount; i++){//统计列下标+1 的成员 的个数ass[trip.values[i].col + 1]++;}printf("数组ass元素值如下:(列 个数)\n");for (i = 0; i < trip.colCount + 1; i++)printf("%d, %d\n", i, ass[i]);for (i = 1; i < trip.colCount; i++)ass[i] += ass[i - 1];ntrip = (TRIPLE_HEAD *)malloc(sizeof(TRIPLE_HEAD));*ntrip = trip;//交换行列t = ntrip->rowCount;ntrip->rowCount = ntrip->colCount;ntrip->colCount = t;ntrip->values = (TRIPLE *)malloc(sizeof(TRIPLE)* ntrip->elementCount);for (i = 0; i < trip.elementCount; i++){index = ass[trip.values[i].col]++;ntrip->values[index] = trip.values[i];t = ntrip->values[index].col;ntrip->values[index].col = ntrip->values[index].row;ntrip->values[index].row = t;}free(ass);return ntrip;}void showTriple(TRIPLE_HEAD triHd){int i, j, t = 0;printf("\n");for (i = 0; i < triHd.rowCount; i++){for (j = 0; j < triHd.colCount; j++){if (t < triHd.elementCount && i == triHd.values[t].row && j == triHd.values[t].col){printf("%d ", triHd.values[t++].value);}else{printf("0 ");}}printf("\n");}}void destoryTriple(TRIPLE_HEAD *triHd){free(triHd->values);free(triHd);}TRIPLE_HEAD *initTriple(){TRIPLE_HEAD *th;int row;int col;int value;int i;th = (TRIPLE_HEAD *)malloc(sizeof(TRIPLE_HEAD));printf("请输入矩阵的阶数(行 列):");scanf("%d%d", &th->rowCount, &th->colCount);printf("请输入有效元素的个数:\n");scanf("%d", &th->elementCount);th->values = (TRIPLE *)malloc(sizeof(TRIPLE)* th->elementCount);for (i = 0; i < th->elementCount; i++){printf("请输入第%d个元素(行 列 值)(共%d个):", i+1, th->elementCount);scanf("%d%d%d", &row, &col, &value);th->values[i].row = row;th->values[i].col = col;th->values[i].value = value;}return th;}void main(void){TRIPLE_HEAD *trip, *revTrip;trip = initTriple();showTriple(*trip);revTrip = revange(*trip);showTriple(*revTrip);//转置之后的稀疏矩阵destoryTriple(trip);destoryTriple(revTrip);system("pause");}
0 0
- 源代码——三元组实现稀疏矩阵及其转置
- 用三元组存储稀疏矩阵并实现转置
- 稀疏矩阵基于“三元组”的转置算法实现
- 稀疏矩阵相乘——三元组稀疏矩阵
- 稀疏矩阵的三元组表示的实现及应用(2)——采用三元组存储稀疏矩阵,设计两个稀疏矩阵相加的运算算法
- 三元组表示矩阵,以及稀疏矩阵的转置
- 二维矩阵转三元组(稀疏矩阵)
- 三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述)
- 稀疏矩阵-三元组
- 稀疏矩阵的三元组表示的实现及应用(1)——建立稀疏矩阵三元组表示的算法库
- 稀疏矩阵三元组存储结构的定义及其有关算法的实现
- 稀疏矩阵的压缩存储与快速转置 三元组法 类与对象实现
- Sparse Matrix(稀疏矩阵三元组表示,三元组形式的矩阵转置)
- 数据结构Java实现——④数组——>稀疏矩阵三元组顺序存储
- 项目三——稀疏矩阵的三元组表示的实现及应用
- 第9周 项目3—稀疏矩阵的三元组表示的实现及应用
- 第九章项目3—稀疏矩阵的三元组表示的实现与应用
- 三元组稀疏矩阵的快速转置
- Android自定义View之仿微信小视频加载进度条
- Ubuntu Linux 环境变量PATH设置
- C#调用win32 api字典
- Mapped Statements collection already contains value for com.dao.personMapper.selectByPrimaryKey
- PHP学习:数据库(一)
- 源代码——三元组实现稀疏矩阵及其转置
- Redis命令
- hihocoder #1343 : Stable Members(支配树)
- 限制 TerichDB 的写速度
- 设计模式学习之路-外观模式
- android项目每次运行都进入debug模式
- HTML5中的跨文档消息传递
- 生成.a静态库 注意事项
- 生成java可执行文件(bat)