1.十字链表
十字链表是为了便于求得图中顶点的度(出度和入度)而提出来的。它是综合邻接表和逆邻接表形式的一种链式存储结构。图的话就不上了,应该都知道这玩意是个怎么回事。
一般来说十字链表的存储结构是由三个域构成,data域,In的指针域,out的指针域构成。在下面的代码中,老师给的存储结构是包含了节点的行,列两个数据。直接从数组读取过来的行列吧。
package graph;
/**
*
* @author leeyz_1@163.com
*/
public class OrthogonalList {
/**
* An inner class for adjacent node.
*/
class OrthogonalNode {
/**
* The row index.
*/
int row;
/**
* The column index.
*/
int column;
/**
* The next out node.
*/
OrthogonalNode nextOut;
/**
* The next in node.
*/
OrthogonalNode nextIn;
/**
*********************
* The first constructor.
*
* @param paraRow The row.
* @param paraColumn The column.
*********************
*/
public OrthogonalNode(int paraRow, int paraColumn) {
row = paraRow;
column = paraColumn;
nextOut = null;
nextIn = null;
}// Of OrthogonalNode
}// Of class OrthogonalNode
/**
* The number of nodes. This member variable may be redundant since it is always
* equal to headers.length.
*/
int numNodes;
/**
* The headers for each row.
*/
OrthogonalNode[] headers;
/**
*********************
* The first constructor.
*
* @param paraMatrix The matrix indicating the graph.
*********************
*/
public OrthogonalList(int[][] paraMatrix) {
numNodes = paraMatrix.length;
// Step 1. Initialize. The data in the headers are not meaningful.
OrthogonalNode tempPreviousNode, tempNode;
headers = new OrthogonalNode[numNodes];
// Step 2. Link to its out nodes.
for (int i = 0; i < numNodes; i++) {
// headers的i位置,创建一个新的节点,行为i,列的值全部初始化为-1
headers[i] = new OrthogonalNode(i, -1);
// 前一个节点置为headers的该位置
tempPreviousNode = headers[i];
// for j 按列挨个循环,直到找到不为0的点(存在相连的边)
for (int j = 0; j < numNodes; j++) {
if (paraMatrix[i][j] == 0) {
continue;
} // Of if
// Create a new node.
tempNode = new OrthogonalNode(i, j);
// Link.
// tempPreviousNode为headers[i],下一个指向新节点
tempPreviousNode.nextOut = tempNode;
// 更新tempPreviousNode到最后的位置
tempPreviousNode = tempNode;
} // Of for j
} // Of for i
// Step 3. Link to its in nodes. This step is harder.
OrthogonalNode[] tempColumnNodes = new OrthogonalNode[numNodes];
for (int i = 0; i < numNodes; i++) {
// 复制
tempColumnNodes[i] = headers[i];
} // Of for i
for (int i = 0; i < numNodes; i++) {
// tempNode此时为该节点的下一个
tempNode = headers[i].nextOut;
// while来寻找这一行相连的点
while (tempNode != null) {
/**
* temoNode.column是相连的那个节点的列数,将这个列数作为index,
* 当成tempColumnNodes的行,将这一个节点的nextIn,也就是入改成tempNode
*/
tempColumnNodes[tempNode.column].nextIn = tempNode;
// 再将这一个改成tempNode
tempColumnNodes[tempNode.column] = tempNode;
// 向后更新
tempNode = tempNode.nextOut;
} // Of while
} // Of for i
}// Of the constructor
/**
*********************
* Overrides the method claimed in Object, the superclass of any class.
*********************
*/
public String toString() {
String resultString = "Out arcs: ";
OrthogonalNode tempNode;
for (int i = 0; i < numNodes; i++) {
tempNode = headers[i].nextOut;
while (tempNode != null) {
resultString += " (" + tempNode.row + ", " + tempNode.column + ")";
tempNode = tempNode.nextOut;
} // Of while
resultString += "\r\n";
} // Of for i
resultString += "\r\nIn arcs: ";
for (int i = 0; i < numNodes; i++) {
tempNode = headers[i].nextIn;
while (tempNode != null) {
resultString += " (" + tempNode.row + ", " + tempNode.column + ")";
tempNode = tempNode.nextIn;
} // Of while
resultString += "\r\n";
} // Of for i
return resultString;
}// Of toString
/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
int[][] tempMatrix = { { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 1, 0, 0, 0 }, { 0, 1, 1, 0 } };
OrthogonalList tempList = new OrthogonalList(tempMatrix);
System.out.println("The data are:\r\n" + tempList);
}// Of main
}// Of class OrthogonalList
2.总结
在连接十字链表的in的时候,那个while循环,看了好一阵,最后加了三个输出,挨个输出才搞懂具体是怎么连的,像这种挨个去查看当前值,并且一定要去用手去算一下过程才能快速看懂。