DataStructure_第五章 数组与广义表 ( 矩阵的压缩存储及转置 / 广义表的定义及实现 )

本文探讨了如何实现压缩存储的矩阵转置,以及广义表的定义和实现。对于压缩存储的矩阵,转置操作通过特定算法进行优化。同时,介绍了广义表的相关概念。
摘要由CSDN通过智能技术生成

压缩存储的矩阵的转置

这里只实现压缩存储的矩阵的转置. 未压缩的二维数组矩阵的转置没啥好写的, 二重循环赋值完事儿了.

/*** 
 * @Author      : acmaker
 * @Date        : 2020-04-07 09:32:53
 * @LastEditTime: 2020-04-07 12:52:18
 * @FilePath    : \myCPlusPlusCode\DataStructure\Matrix_And_GeneralizedList.cpp\Matrix.cpp
 * @Website     : http://csdn.acmaker.vip
 * @Description : 
 */


#include <bits/stdc++.h> 
using namespace std; 
#define rg register 
#define sc scanf 
#define pf printf 
typedef long long ll; 

typedef int State;
#define TRUE 1
#define FALSE 0 

typedef int ElemType;
typedef struct Triple {
    int row, col;
    ElemType e;
}Triple;
const int MAX_SIZE = 1e3;
typedef struct TripleMatrix {
    int m, n, len;
    Triple triple[MAX_SIZE];
}TripleMatrix;

/*** 
 * @description: 列序递增法, 转置压缩矩阵
 * @param : 
 * @return: 
 */
State transformTripleMatrix ( TripleMatrix A, TripleMatrix *B ) {
    if ( A.len<=0 ) return FALSE;
    B->m = A.n;
    B->n = A.m;
    B->len = A.len;
    int k = 0;
    for ( int  j = 1; j < A.n; ++j ) {
        for ( int i = 1; i < A.len; ++i ) {
            if ( A.triple[i].col==i ) {
                B->triple[k].row = A.triple[i].col;
                B->triple[k].col = A.triple[i].row;
                B->triple[k].e = A.triple[i].e;
            }
        }
    }
    return TRUE;
}

/*** 
 * @description: 一次定位法, 转置压缩矩阵
 * @param : 
 * @return: 
 */
State transformTripleMatrix_rapidPosition ( TripleMatrix A, TripleMatrix *B ) {
    if ( A.len<=0 ) return FALSE;
    B->m = A.n;
    B->n = A.m;
    B->len=A.len;
    int *cnt = new int[A.n];
    memset( cnt, 0, sizeof(int)*(A.n+1) );
    for ( int i  = 1; i <= A.len; ++i ) ++cnt[A.triple[i].col];
    // for ( int i  = 1; i <= A.len; ++i ) ++*(cnt+i);
    int *pos = new int[A.n];
    pos[1] = 1;
    for ( int i = 2; i <= A.n; ++i ) pos[i] = pos[i-1]+cnt[i-1];
    // for ( int i = 2; i <= A.n; ++i ) *(pos+i) = *(pos+i-1)+*(cnt+i-1);
    for ( int i = 1; i <= A.len; ++i ) {
        int col = A.triple[i].col;
        int k = pos[col];
        // int k = *(pos+A.triple[i].col);
        B->triple[k].row = A.triple[i].col;
        B->triple[k].col = A.triple[i].row;
        B->triple[k].e = A.triple[i].e;
        ++pos[col];
    }
    return TRUE;
}

int main ( ) {  // freopen( "F:\\in\\.txt" , "r" , stdin ); 

    TripleMatrix a, b;

    a.m = 4;
    a.n = 3;
    a.len = 4;
    a.triple[1].row = 1;
    a.triple[1].col = 1;
    a.triple[1].e = 1;

    a.triple[2].row = 2;
    a.triple[2].col = 2;
    a.triple[2].e = 2;

    a.triple[3].row = 3;
    a.triple[3].col = 3;
    a.triple[3].e = 3;

    a.triple[4].row = 4;
    a.triple[4].col = 3;
    a.triple[4].e = 4;

    cout << transformTripleMatrix_rapidPosition( a, &b ) << endl;

    return 0 ; 
} 

广义表

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值