11.十字链表 实现 稀疏矩阵 及 矩阵相加

#include<stdio.h>
#include<stdlib.h>

FILE *fp;

// 十字链表 实现 稀疏矩阵 及  矩阵相加
#define OK 0
#define ERROR 1
#define OVERFLOW 1
typedef bool Status;

typedef char ElemType;
typedef struct OLNode//节点信息
{
	int i, j;
	struct OLNode *right, *down;
	ElemType e;
}OLNode,*OLink;

typedef struct
{
	OLink *rhead, *chead;
	int mu, nu, tu;
}CrossList;


Status CreateSMatrix(CrossList &M)
{
	//if (M) free(M);
	int m, n, t;
	//init m n t

	//scanf("%d %d %d", &m, &n, &t);// getchar();
	fscanf(fp, "%d %d %d", &m, &n, &t);// getchar();
	
	M.mu = m; M.nu = n; M.tu = t;
	//init malloc
	if (!(M.rhead = (OLink*)malloc((m + 1)*sizeof(OLink))))  exit(OVERFLOW);
	if (!(M.chead = (OLink*)malloc((n + 1)*sizeof(OLink))))  exit(OVERFLOW);
	//init null
	for (int l = 0; l < m + 1; l++) M.rhead[l] = NULL;
	for (int l = 0; l < n + 1; l++) M.chead[l] = NULL;
	//get data
	int i, j, e;
	//while (scanf("%d %d %d", &i, &j, &e) != EOF && i)
	while (fscanf(fp,"%d %d %d", &i, &j, &e) != EOF && i)
	{
		//getchar();
		OLink p;
		if (!(p = (OLink)malloc(sizeof(OLNode)))) exit(OVERFLOW);
		p->e = e; p->i = i; p->j = j;
		//插入相应行 中
		if (M.rhead[i] == NULL || M.rhead[i]->j > j)
		{ 
			p->right = M.rhead[i];
			M.rhead[i]= p; 
		}
		else
		{
			OLink  q;
			for (q = M.rhead[i]; q->right && q->right->j < j; q = q->right);
			p->right = q->right;  q->right = p;
		}
		//接下来插入相应列中
		if (M.chead[j] == NULL || M.chead[j]->i>i) { p->down = M.chead[j]; M.chead[j] = p; }
		else
		{
			OLink  q;
			for (q = M.chead[j]; q->down && q->down->i < i; q = q->down);
			p->down = q->down;  q->down = p;
		}

	}
	return OK;
}

void InitFile()
{
	
	fp = fopen("data.txt", "r+");
	if (!fp)  {	printf("文件读取错误\n");  exit(ERROR);	}
}


int main()
{
	InitFile();
	CrossList A;
	CreateSMatrix(A);


	fclose(fp);
	return 0;
}



















  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用十字链表储存稀疏矩阵实现稀疏矩阵相乘的 C 代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义十字链表节点 typedef struct { int row; // 行下标 int col; // 列下标 int value; // 元素值 struct Node* right; // 指向右边的节点 struct Node* down; // 指向下方的节点 } Node; // 定义十字链表 typedef struct { int rows; // 矩阵的行数 int cols; // 矩阵的列数 Node* row_heads; // 行头节点数组 Node* col_heads; // 列头节点数组 } CrossList; // 创建稀疏矩阵对应的十字链表 CrossList* createCrossList(int rows, int cols) { CrossList* matrix = (CrossList*)malloc(sizeof(CrossList)); matrix->rows = rows; matrix->cols = cols; matrix->row_heads = (Node*)malloc(rows * sizeof(Node)); matrix->col_heads = (Node*)malloc(cols * sizeof(Node)); // 初始化头节点数组 for (int i = 0; i < rows; i++) { matrix->row_heads[i].right = NULL; matrix->row_heads[i].down = NULL; } for (int j = 0; j < cols; j++) { matrix->col_heads[j].right = NULL; matrix->col_heads[j].down = NULL; } return matrix; } // 向十字链表中插入元素 void insertElement(CrossList* matrix, int row, int col, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->row = row; newNode->col = col; newNode->value = value; // 插入新节点到行链表中 Node* row_head = &(matrix->row_heads[row]); Node* cur = row_head; while (cur->right != NULL && cur->right->col < col) { cur = cur->right; } newNode->right = cur->right; cur->right = newNode; // 插入新节点到列链表中 Node* col_head = &(matrix->col_heads[col]); cur = col_head; while (cur->down != NULL && cur->down->row < row) { cur = cur->down; } newNode->down = cur->down; cur->down = newNode; } // 打印稀疏矩阵十字链表表示 void printCrossList(CrossList* matrix) { for (int i = 0; i < matrix->rows; i++) { Node* node = matrix->row_heads[i].right; for (int j = 0; j < matrix->cols; j++) { if (node != NULL && node->col == j) { printf("%d ", node->value); node = node->right; } else { printf("0 "); } } printf("\n"); } } // 稀疏矩阵相乘 CrossList* multiplySparseMatrix(CrossList* matrix1, CrossList* matrix2) { if (matrix1->cols != matrix2->rows) { printf("Cannot multiply the matrices!"); return NULL; } CrossList* result = createCrossList(matrix1->rows, matrix2->cols); for (int i = 0; i < matrix1->rows; i++) { Node* row_head = &(matrix1->row_heads[i]); Node* node1 = row_head->right; for (int j = 0; j < matrix2->cols; j++) { Node* col_head = &(matrix2->col_heads[j]); Node* node2 = col_head->down; int value = 0; while (node1 != NULL && node2 != NULL) { if (node1->col < node2->row) { node1 = node1->right; } else if (node1->col > node2->row) { node2 = node2->down; } else { value += node1->value * node2->value; node1 = node1->right; node2 = node2->down; } } if (value != 0) { insertElement(result, i, j, value); } } } return result; } // 测试代码 int main() { CrossList* matrix1 = createCrossList(3, 3); insertElement(matrix1, 0, 0, 1); insertElement(matrix1, 0, 2, 2); insertElement(matrix1, 1, 1, 3); insertElement(matrix1, 2, 0, 4); insertElement(matrix1, 2, 2, 5); CrossList* matrix2 = createCrossList(3, 3); insertElement(matrix2, 0, 0, 1); insertElement(matrix2, 0, 1, 2); insertElement(matrix2, 1, 1, 3); insertElement(matrix2, 1, 2, 4); insertElement(matrix2, 2, 0, 5); CrossList* result = multiplySparseMatrix(matrix1, matrix2); printf("Result:\n"); printCrossList(result); return 0; } ``` 这段代码实现稀疏矩阵十字链表存储以及稀疏矩阵的乘法操作。你可以根据需要修改矩阵的大小和元素值进行测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值