稀疏矩阵的十字链表存储



1.结点类型

我们将结点分为两种类型,头结点和数据结点,头结点包含rightdownnext域,数据结点包含rightdownentry域。right域将同一行的结点链接起来,down域将同一列的结点链接起来,next域将头结点链接起来。因此使用union共用体创建合适的结点结构。

typedef struct entry_node{
	int row;
	int col;
	int value;
}Entry;

typedef struct node{
	struct node *right;
	struct node *down;
	union{
		struct node *next;
		Entry entry;
	}u;
}MatrixNode;

2.创建十字链表

*第i行的头结点也是第i列的头结点,头结点的个数是行数和列数的最大值

*需要一个总的头结点,它是数据结点,存储行数,列数和数据结点个数

*使用rlast存储当前行的尾节点,将当前列的尾节点存储在当前列头结点的next域,在程序的最后再进行头结点的链接

*在这里都使用的是循环链表

MatrixNode* seq[Max];  //头结点指针数组

MatrixNode* CreateMatrix(){
	MatrixNode *head;   //总的头结点
	MatrixNode *rlast,*temp;  //rlast存储当前行的尾结点
	int num_rows,num_cols,num_terms,num_heads;
	int row,col,value,current_row;  //current_row存储当前在进行链接的行数
	int i;
	
	scanf("%d%d%d",&num_rows,&num_cols,&num_terms);
	num_heads=(num_rows>num_cols)?num_rows:num_cols;  //确定头结点个数
	
	head=(MatrixNode*)malloc(sizeof(MatrixNode));  //创建总的头结点
	head->right=head->down=head;
	head->u.entry.row=num_rows;
	head->u.entry.col=num_cols;
	head->u.entry.value=num_terms;
	//将每个头结点先创建出来,它们是各自独立的,不进行链接
	for(i=0;i<num_heads;i++){
		seq[i]=(MatrixNode*)malloc(sizeof(MatrixNode));
		seq[i]->right=seq[i]->down=seq[i];
		seq[i]->u.next=seq[i];
	}
	
	//进行数据结点的链接
	current_row=0;
	rlast=seq[current_row];
	for(i=0;i<num_terms;i++){
		scanf("%d%d%d",&row,&col,&value);
		temp=(MatrixNode*)malloc(sizeof(MatrixNode));
		temp->u.entry.row=row;
		temp->u.entry.col=col;
		temp->u.entry.value=value;
		temp->right=temp->down=temp;
		
		if(row>current_row){
			current_row=row;
			rlast=seq[current_row];
		}
		
		rlast->right=temp;
		rlast=temp;
		rlast->right=seq[current_row];
		
		seq[col]->u.next->down=temp;  //进行列的链接
		seq[col]->u.next=temp;    //将当前列的尾节点存储到头结点的next域中
	}
	
	for(i=0;i<num_heads-1;i++){
		seq[i]->u.next=seq[i+1];  //链接头结点
	}
	
	for(i=0;i<num_cols;i++){
		seq[i]->u.next->down=seq[i];  //链接列的最后一个结点
	}
	
	head->right=head->down=seq[0];  //链接总的头结点
	seq[num_heads-1]->u.next=head;
	
	return head;
}

3.输出矩阵

void PrintMatrix(MatrixNode *head){
	MatrixNode *p,*nowh;
	int i;
	nowh=head->right;
	printf("\n%d\t%d\t%d\n",head->u.entry.row,head->u.entry.col,head->u.entry.value);
	for(i=0;i<head->u.entry.row;i++){
		for(p=nowh->right;p!=nowh;p=p->right){
				printf("%d\t%d\t%d\n",p->u.entry.row,p->u.entry.col,p->u.entry.value);
		}
		nowh=nowh->u.next;
	}
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值