Data structure---a cross-linked list of sparse matrices implemented in C + +

Source: Internet
Author: User

First, here is what is the matrix of the cross-linked list, we can understand that the sparse matrix is stored sequentially, then this is chained storage.


Store The Matrix



Then it should be in the following format:




We know that the implementation of the ternary storage of sparse matrices is simple, with each element having three fields Col,row, E. Represents the line number, column number, and value for this non-$ 0. Then in the cross-linked list of storage, the first of these three domains is definitely not, or in a lot of operations to use their own counters, very troublesome. And the form of a cross-linked list can be understood as each row is a linked list, and each column is a linked list


As we can see from the above diagram, there is more than one node to store row, Col, E. It also holds the address of the next node in its landscape and the address of the next node in the portrait. The structure of a linked list resembling a cross-shaped form.

Note the types of Rhead and chead, which are pointers to pointers, that is, pointers to pointers to the nodes of the OLNODE structure we define.

Two versions of C language:

Cross-linked list storage representation for sparse matrices//Xin Yang # include <stdio.h> #include <stdlib.h>typedef int elemtype;typedef struct olnode{ Elemtype e;int I, j;struct olnode *right, *down;} Olnode, *olink;typedef struct{olink *rhead, *chead;int mu, nu, tu;} Crosslist;int Create_smatrix (crosslist *m) {int I, J, M, N, T;int K, flag; Elemtype E;olnode *p, *q;if (m->rhead) {Do{flag = 1;printf ("Enter the number of rows to create the matrix, the number of columns, and the number of non-0 elements: \ n"); scanf ("%d%d%d", &m, &n, &t); if (M < 0 | | n < 0 | | T < 0 | | t > M * N) flag = 0;} while (!flag); M->mu = m; M->nu = n; M->tu = t; M->rhead = (Olink *) malloc ((M + 1) * sizeof (Olink)); M->rhead) exit (-1); M->chead = (Olink *) malloc ((n + 1) * sizeof (Olink)); M->chead) exit ( -1); for (k = 1; k <= m; k++) m->rhead[k] = null;for (k = 1; k <= N; k++) m->chead[k] = Null;for     (k=1; k<=t; ++k)      {do {flag = 1;       printf ("Enter%d node line number, column number, and value: \ n", K);       scanf ("%d%d%d", &i, &j, &e);      if (i<=0 | | j<=0) flag = 0; }whiLe (!flag);p = (olink) malloc (sizeof (Olnode));      if (NULL = = p) exit (-1);      P->i = i;      P->j = j;      P->e = e; if (Null==m->rhead[i] | | |       M-&GT;RHEAD[I]-&GT;J&GT;J) {p->right = m->rhead[i];      M->rhead[i] = p;         } else {for (q=m->rhead[i]; q->right && q->right->j < J; q=q->right)       ; p->right=q->right;      Complete line Insertion q->right=p; } if (Null==m->chead[j] | | |       M->chead[j]->i>i) {P->down = m->chead[j];      M-&GT;CHEAD[J] = p;       } else {for (q=m->chead[j]; q->down && q->down->i < i; q=q->down);         p->down=q->down;      q->down=p;    }} return 1;}        }int Main () {crosslist M;  Create_smatrix (&AMP;M); return 0;}




C + + version:


#include <iostream> #include <stdlib.h>using namespace std;typedef int elemtype;typedef struct _node_{int i,  j;//row and column subscript elemtype e;//element value struct _node_ *pright; struct _node_ *pdown;} node,*pnode;typedef struct _crosslist_{pnode *rowhead,*colhead;//row and column chain header pointer vector int irow,icol,nodecount;//matrix row number, number of columns, non 0-element number }crosslist,*pcrosslist;//------------------------------------------------void Createcrosslist (pcrosslist PCROSSLISTTEMP);//Create a cross-linked list void printcrosslist (Pcrosslist pcrosslisttemp);//print cross-linked list//------------------------------- -----------------void Createcrosslist (Pcrosslist pcrosslisttemp) {//1. Number of input rows and non-0-element cout<< "input matrix several rows, several columns, several non-0 elements  : \ n ";  cin>>pcrosslisttemp->irow;  cin>>pcrosslisttemp->icol;  cin>>pcrosslisttemp->nodecount;  if (Pcrosslisttemp->nodecount > Pcrosslisttemp->irow*pcrosslisttemp->icol) {return;  }//2. Dynamic requisition row and column pointer array pcrosslisttemp->rowhead = (Pnode *) malloc (sizeof (Pnode) *pcrosslisttemp->irow); if (Pcrosslisttemp->rowHead = = NULL) {return;  } Pcrosslisttemp->colhead = (Pnode *) malloc (sizeof (Pnode) *pcrosslisttemp->icol);    if (Pcrosslisttemp->colhead = = NULL) {free (pcrosslisttemp->rowhead);  Return  }//3. Initialize the two arrays int i,j;  for (i = 0; i < pcrosslisttemp->irow; i++) {pcrosslisttemp->rowhead[i] = NULL;  } for (j = 0; J < pcrosslisttemp->icol; J + +) {Pcrosslisttemp->colhead[j] = NULL; }//4. Create the node and connect to the cross-linked list for (i = 0; i < pcrosslisttemp->nodecount; i++) {//4.1 Create node Pnode pnodetemp = (pnode) mal    LOC (sizeof (Node));    if (pnodetemp = = NULL) {return;    } cout<< "input row and column elements" <<endl;    cin>>pnodetemp->i;    cin>>pnodetemp->j;    cin>>pnodetemp->e;    Pnodetemp->pdown = NULL;    Pnodetemp->pright = NULL; 4.2 Connection//Connection Line///If the row does not have any nodes connected (NULL) or the first node of the row is connected to a node that is greater than the current one to be connected, connect the current node directly to the position of the first node of the row if (pcrosslisttemp->rowhead[p Nodetemp->i] = = NULL | | pcrosslisttemp->rowhead[pnodetemp-&GT;I]-&GT;J&GT;PNODETEMP-&GT;J) {pnodetemp->pright = pcrosslisttemp->rowhead[pnodetemp->i];    Pcrosslisttemp->rowhead[pnodetemp->i] = pnodetemp;      }else//otherwise traverse the line to find the appropriate place to insert {Pnode Pnodetravel = pcrosslisttemp->rowhead[pnodetemp->i];//points to the first node, starting at the first node to traverse        while (pnodetravel->pright! = NULL && pnodetravel->pright->j < pnodetemp->j)//traverse to the previous node {      Pnodetravel = pnodetravel->pright;      }//Connection Pnodetemp->pright = pnodetravel->pright;    Pnodetravel->pright = pnodetemp; }//Connection column, logical with Connection row if (Pcrosslisttemp->colhead[pnodetemp->j]==null | | pcrosslisttemp->colhead[pnodetemp->      J]->i>pnodetemp->i) {Pnodetemp->pdown = pcrosslisttemp->colhead[pnodetemp->j];    PCROSSLISTTEMP-&GT;COLHEAD[PNODETEMP-&GT;J] = pnodetemp;      }else {Pnode Pnodetravel = pcrosslisttemp->colhead[pnodetemp->j]; while (Pnodetravel->pdown! = NULL && PnodEtravel->pdown->i < pnodetemp->i) {Pnodetravel = pnodetravel->pdown;      } Pnodetemp->pdown = pnodetravel->pdown;    Pnodetravel->pdown = pnodetemp;  }}}void printcrosslist (pcrosslist pcrosslisttemp) {int i,j;  Pnode ptemp;    for (i = 0; i < pcrosslisttemp->irow; i++) {ptemp = pcrosslisttemp->rowhead[i]; for (j = 0; J < pcrosslisttemp->icol; J + +) {if (ptemp! = NULL && Ptemp->j = = j) {cout        <<pTemp->e<< "";      Ptemp = ptemp->pright;      }else {cout<< "0";  }} cout<<endl; } Cout<<endl;}  int main () {crosslist C;  Createcrosslist (&AMP;C);  cout<< "================ Split line =========" <<endl;  Printcrosslist (&AMP;C); return 0;}



Data structure---a cross-linked list of sparse matrices implemented in C + +

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.