DS:稀疏矩阵-三元组表
2017-07-26 14:54
183 查看
用c++写了下,当作是练习吧XD
// STACK.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#include<string.h>
#define I 3
#define J 4
#define maxsize I*J
struct triple{
int i,j,value;
};
struct tritable{
triple data[maxsize];
int mu,nu,tu;
}tsmtype;
int create(int **&b){
b=(int **)malloc(sizeof(int)*J);
for(int i = 0;i<I; i++)
{
b[i]=(int *)malloc(sizeof(int)*I);
}
srand(time(NULL));
return 0;}
void initary(int **&b)
{ putchar(' ');putchar(' ');
for(int k = 0;k<J;k++)
{
printf("%d\t",k);
}putchar('\n');
for( int i = 0;i<I; i++)
{
printf("%d:",i);
for(int j = 0;j<J;j++)
{
if(rand()%2==0)b[i][j]=0;
else b[i][j]=i*j;
printf("%d\t",b[i][j]);
}putchar('\n');
}
}
int compress(int **&b)
{int k=0;
for( int i = 0;i<I; i++)
{ for(int j = 0;j<J;j++)
{
if(b[i][j]!=0)
{
tsmtype.data[k].i=i;
tsmtype.data[k].j=j;
tsmtype.data[k].value=b[i][j];
k++;
}
}
}
tsmtype.mu=I;
tsmtype.nu=J;
tsmtype.tu=k;
for(i=0;i<k;i++)
{
printf("%d:%d %d %d\n",i,tsmtype.data[i].i,tsmtype.data[i].j,tsmtype.data[i].value);
}
return 0;}
void displytriple(tritable * &A)
{
int k=0;
for( int i = 0;i<A->mu; i++)
{
printf("%d:",i);
for(int j = 0;j<A->nu;j++)
{
if(i==A->data[k].i&&j==A->data[k].j){printf("%d\t",A->data[k].value);k++;}
else printf("0\t");
}putchar('\n');
}
putchar('\n');
}
int rvsetriple(tritable * &A)
{
int k=0;
A=(tritable *) malloc(sizeof(tritable));
A->mu=tsmtype.nu;
A->nu=tsmtype.mu;
A->tu=tsmtype.tu;
for(int i=0;i<J;i++)
{
for(int j=0;j<tsmtype.tu;j++)
{
if(i==tsmtype.data[j].j)
{
A->data[k].i=tsmtype.data[j].j;
A->data[k].j=tsmtype.data[j].i;
A->data[k].value=tsmtype.data[j].value;
k++;
}
}
}
for(i=0;i<k;i++)
{
printf("%d:%d %d %d\n",i,A->data[i].i,A->data[i].j,A->data[i].value);
}
return 0;
}
int main()
{
int **b;
create(b);
puts("create array:");
initary(b);
puts("compress the array to triple:");
compress(b);
puts("display using the triple:");
tritable * A=&tsmtype;
displytriple(A);
puts("reverse the triple");
tritable * B;
rvsetriple(B);
displytriple(B);
return 0;
}
// STACK.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#include<string.h>
#define I 3
#define J 4
#define maxsize I*J
struct triple{
int i,j,value;
};
struct tritable{
triple data[maxsize];
int mu,nu,tu;
}tsmtype;
int create(int **&b){
b=(int **)malloc(sizeof(int)*J);
for(int i = 0;i<I; i++)
{
b[i]=(int *)malloc(sizeof(int)*I);
}
srand(time(NULL));
return 0;}
void initary(int **&b)
{ putchar(' ');putchar(' ');
for(int k = 0;k<J;k++)
{
printf("%d\t",k);
}putchar('\n');
for( int i = 0;i<I; i++)
{
printf("%d:",i);
for(int j = 0;j<J;j++)
{
if(rand()%2==0)b[i][j]=0;
else b[i][j]=i*j;
printf("%d\t",b[i][j]);
}putchar('\n');
}
}
int compress(int **&b)
{int k=0;
for( int i = 0;i<I; i++)
{ for(int j = 0;j<J;j++)
{
if(b[i][j]!=0)
{
tsmtype.data[k].i=i;
tsmtype.data[k].j=j;
tsmtype.data[k].value=b[i][j];
k++;
}
}
}
tsmtype.mu=I;
tsmtype.nu=J;
tsmtype.tu=k;
for(i=0;i<k;i++)
{
printf("%d:%d %d %d\n",i,tsmtype.data[i].i,tsmtype.data[i].j,tsmtype.data[i].value);
}
return 0;}
void displytriple(tritable * &A)
{
int k=0;
for( int i = 0;i<A->mu; i++)
{
printf("%d:",i);
for(int j = 0;j<A->nu;j++)
{
if(i==A->data[k].i&&j==A->data[k].j){printf("%d\t",A->data[k].value);k++;}
else printf("0\t");
}putchar('\n');
}
putchar('\n');
}
int rvsetriple(tritable * &A)
{
int k=0;
A=(tritable *) malloc(sizeof(tritable));
A->mu=tsmtype.nu;
A->nu=tsmtype.mu;
A->tu=tsmtype.tu;
for(int i=0;i<J;i++)
{
for(int j=0;j<tsmtype.tu;j++)
{
if(i==tsmtype.data[j].j)
{
A->data[k].i=tsmtype.data[j].j;
A->data[k].j=tsmtype.data[j].i;
A->data[k].value=tsmtype.data[j].value;
k++;
}
}
}
for(i=0;i<k;i++)
{
printf("%d:%d %d %d\n",i,A->data[i].i,A->data[i].j,A->data[i].value);
}
return 0;
}
int main()
{
int **b;
create(b);
puts("create array:");
initary(b);
puts("compress the array to triple:");
compress(b);
puts("display using the triple:");
tritable * A=&tsmtype;
displytriple(A);
puts("reverse the triple");
tritable * B;
rvsetriple(B);
displytriple(B);
return 0;
}
相关文章推荐
- 第九周项目三----稀疏矩阵的三元组表示的实现及应用1
- 假设nn的稀疏矩阵A采用三元组表示,设计一个程序exp6-4.cpp实现如下功能:
- 第九周--项目三--稀疏矩阵的三元组表示的实现及应用
- 第九周 项目3-稀疏矩阵的三元组表示的实现及应用
- 第9周项目3-稀疏矩阵的三元组表示的实现及应用(1)
- 第九周项目3-稀疏矩阵的三元组表示的实现及应用(2)
- 第九周项目三 稀疏矩阵的三元组表示的实现及应用(2)
- 第九周项目3-稀疏矩阵的三元组表示的实现与应用(1)
- 用三元组存储稀疏矩阵并实现转置
- 第9周项目3-(2)稀疏矩阵的三元组表示的实现及应用
- 第九周项目3-稀疏矩阵的三元组表示的实现及应用(1)
- 三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述)
- 第8周项目4-稀疏矩阵的三元组表示的实现及应用(2)
- 第八周项目4 稀疏矩阵的三元组表示的实现及应用
- 稀疏矩阵利用三元组相乘(c语言)
- 第八周项目四 稀疏矩阵的三元组表示的实现及应用(2)
- 顺序存储的稀疏矩阵(三元组)的转置
- 稀疏矩阵--三元组表示法和十字链表示法
- 第九周 项目3 稀疏矩阵的三元组表示及应用
- 第9周实践项目3-稀疏矩阵的三元组表示的应用(3.2)