用链表实现矩阵的逆置,旋转(简单创新解法)

这篇博客介绍了一种创新方法,通过在链表节点中添加x和y坐标来模拟矩阵,从而实现矩阵的转置和旋转。这种方法简化了在链表中处理矩阵变换的复杂性,适用于C++初学者。
摘要由CSDN通过智能技术生成

正常实现矩阵的变换我们都是通过将矩阵通过二维数组的方式存储,在输出端控制好输出的内容来实现矩阵的相应变换,并不会改变数组的存放。但当题目要求使用链表而不是数组方式存储时,题目就会变难很多。

但本文提出一种思想将链表标上数组的序号即可讲问题简单化
在构建结构体时多设两个整型变量x,y来标号

typedef struct LNode{
   
	int x;
	int y;
	int data;
	struct LNode *next;
}LNode, *LinkList;

并在输出时完成如下操作,即可讲链表表示成数组的形式

void init(LinkList &L,int m,int n)
{
   
	L=(LinkList)malloc(sizeof(LNode));
	LinkList current,prev;
	prev=L;
	for(int i=0;i<m;i++)
	{
   
		for(int j=0;j<n;j++)
		{
   
			current=(LinkList)malloc(sizeof(LNode));
			cin>>current->data;
			current->x=i;
			current->y=j;
			prev->next=current;
			prev=current;
		}
	}
	current->next=NULL;
}

例如此时p->x,p->y已经相当于数组a[x][y]的x,y标号。

源代码如下:
1.用链表表示矩阵并实现矩阵的转置。

#include"iostream"
#include "cmath"
using namespace std;
//数组:
/*
int main()
{	
	cout<<"please input the two dimensiomns of the matrix:"<<endl;
	int m,n;
	cin>>m>>n;
	cout<<"please input the elements of the matrix:"<<endl;
	int a[10][10];
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
	cout<<"The original matrix you inputed is:"<<endl;
	for(i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			cout<<a[i][j]<<"  ";
		}
		cout<<endl;
	}
	cout<<"The transposed matrix is:"<<endl;
	for(i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cout<<a[j][i]<<"  ";
		}
		cout<<endl;
	}
	return 0;
}
*/
//链表
typedef struct LNode{
   
	int x;
	int y;
	int data;
	struct LNode *next;
}LNode, *LinkList;
void init(LinkList &L,int m,int n)
{
   

	L=(LinkList)malloc(sizeof(LNode));
	LinkList current,prev;
	prev=L;
	for(int i=0;i<m;i++)
	{
   
		for(int j=0;j<n;j++)
		{
   
			current=(LinkList)malloc(sizeof(LNode));
			cin>>current->
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值