Cross Linked List in data structure (1)

Source: Internet
Author: User

Review the data structure. The cross-linked list is mainly used in two places: sparse matrix storage and directed graph storage.

Sparse Matrix is commonly used for triple storage. However, when the non-zero elements and positions of the matrix change greatly during the operation, they do not adopt sequential structure storage. In this case, cross-linked lists are a good choice.

The following is the data structure of the Cross-linked list, as well as the creation function. There is no post memory release function for a limited space:

Typedef struct qlnode
...{
Int IPOs, JPOs; // row and column subscript of non-zero elements;
Int ielem; // element;
Struct qlnode * right, * down;
} Qlnode;

Typedef struct qlnode * qlink;
Typedef struct
...{
Qlink * rhead, * chead; // header pointer of the row and column;
Int rownum, colnum, totalnum;
} Crosslist;

Bool creatcrosslist (crosslist & crolit)
...{
Int row, coll, total;
Cin> row> Coll> total;
Crolit. rownum = row; crolit. colnum = Coll; crolit. totalnum = total;

Crolit. rhead = new qlink [row]; // create a row header pointer table;
Crorule. chead = new qlink [Coll]; // create a column header index table;

// Initialize the header pointer table;
For (INT I = 0; I <row; I ++)
Crow.rhead [I] = NULL;
For (INT I = 0; I <Coll; I ++)
Crow.chead [I] = NULL;

Int I, j, ELEM; // input in triple format
For (CIN> I> j> ELEM; ELEM! = 0; CIN> I> j> ELEM)
...{
Qlink ptem = new qlnode;
Ptem-> right = ptem-> down = NULL;
Ptem-> IPOs = I; ptem-> JPOs = J; ptem-> ielem = ELEM;

// Insert directly: NULL, or the column coordinate is greater than the maximum column coordinate in the linked list of this row.
// In the same row, the linked list is sorted by column coordinates,
If (crow.rhead [I] = NULL | crow.rhead [I]-> JPOs> J)
...{
Ptem-> right = crow.rhead [I];
Crow.rhead [I] = ptem;
}
Else // query the insert position in the row table:
...{
Qlink BG;
For (BG = crow.rhead [I]; BG-> Right & BG-> right-> JPOs <j; BG = BG-> right );
Ptem-> right = BG-> right; BG-> right = ptem;
} // Insert the row.

// Insert directly: NULL, or the row coordinate is greater than the largest row coordinate in the list.
// In the same column, the linked list is sorted by the row coordinates,
If (crow.chead [J] = NULL | crow.chead [J]-> IPOs> I)
...{
Ptem-> down = crow.chead [J];
Crow.chead [J] = ptem;
}
Else // query the insert position in the list:
...{
Qlink BG;
For (BG = crorule. chead [J]; BG-> down & BG-> down-> IPOs <I; BG = BG-> down );
Ptem-> down = BG-> down; BG-> down = ptem;
} // Insert the column.
}
Return true;
}
// Output elements of each row
Void displayrow (crosslist DEST)
...{
For (INT I = 0; I <DeST. rownum; I ++)
...{
Qlink TEM = DeST. rhead [I];
While (TEM)
...{
Cout <"(" <tem-> IPOs <",";
Cout <tem-> JPOs <",";
Cout <tem-> ielem <")";
TEM = tem-> right;
}
Cout <Endl;
}
}

For example, sparse matrix:
| 3 0 0 5 |
| 0 2 0 0 |
| 2 0 0 0 |
/* Test input:
3 4 4
0 0 3
0 3 5
1 1 2
2 0 2
0 0 0/indicates end */
/* Test output:
(0, 0, 3) (0, 3, 5)
(1, 1, 2)
(2, 0, 2)
Press any key to continue ...*/

The following figure shows the storage of the cross linked list:

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.