Data structure--a cross-linked list representation method for sparse matrices

Source: Internet
Author: User
/************************ author ' s email:wardseptember@gmail.com sparse matrix representation of the cross-linked table ************************/#include
<iostream> using namespace std;
	#define MAXSIZE 4//Common node definition typedef struct OLNODE {int row, col;
	struct Olnode *right, *down;
float Val;

}olnode,*node;
	The head node structure defines the typedef struct {Olnode *rhead, *chead;

int m, n, K;
}crosslist;
int Initmatrix (crosslist *m);

int Createcrosslistmat (float a[][maxsize], int m, int n, int k, crosslist *m);
	int main () {float A[][maxsize] = {{0,0,0,1}, {0,0,3,2}, {1,0,0,0}, {0,2,0,0}};
	int m, n, K;
	m = n = maxSize;
	K = 5;
	Crosslist M;
	Initmatrix (&AMP;M);
	cout << "Elements in the cross-linked list are:" << Endl;
	Createcrosslistmat (A, M, N, K, &m);
return 0;
	} int Initmatrix (Crosslist *m) {(*m). Rhead = (*m). Chead = NULL;
	(*m). K = (*m). M = (*m). N = 0;
return 1;
	} int Createcrosslistmat (float a[][maxsize], int m, int n, int k, crosslist *m) {if (*m). Rhead) Free ((*m). Rhead);
	if ((*m). Chead) Free ((*m). Chead); (*m). m = m;
	(*m). N = n;
	(*m). K = k; if (! (
	(*m). Rhead = (Olnode *) malloc (sizeof (Olnode) *m))) return 0; if (! (
	(*m). Chead = (Olnode *) malloc (sizeof (Olnode) *n))) return 0;
		for (int i = 0; i < m; ++i) {(*m). Rhead[i].right = NULL;
	(*m). Rhead[i].down = NULL;
		} for (int i = 0; i < n; ++i) {(*m). Chead[i].right = NULL;
	(*m). Chead[i].down = NULL;
	}//Create a list of auxiliary pointers array Olnode *temp_r[maxsize];
	for (int j = 0; J < N; ++j) temp_r[j] = & ((*m). Chead[j]);
		for (int i = 0; i < m; ++i) {Olnode *c = & ((*m). Rhead[i]);
				for (int j = 0; J < N; ++j) {if (a[i][j]! = 0) {Olnode *p = (olnode*) malloc (sizeof (Olnode));
				P->row = i;
				P->col = j;
				P->val = A[i][j];
				P->down = NULL;
				P->right = NULL;
				C->right = p;
				c = P;
				Temp_r[j]->down = p;
				TEMP_R[J] = p;
			cout << p->val<< ' t ';
	}}} cout << Endl;
return 1; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.