um(); for (int i = 0; i < stcurRowColNum.iArrayRow; i++) { Node node = {0}; node.x = i; node.y = iCol; if (OK != CreatePathNodelist(g_pGrid[i][iCol], &node)) { return ERR; } } return OK; } /*向二维数组插入一列*/ int insertColtoGrid(int iInsertCol) { if (MAX_COL_NUM <= iInsertCol) { return ERR; } CurRowColNum stcurRowColNum = getRowAndColNum(); /*插入数组中的无效列,直接插入,而向已有列中间插入,需要从前 向后两两交换列元素的位置*/ if (iInsertCol >= stcurRowColNum.iArrayCol) { if (OK != insertToEmptyCol(iInsertCol)) { return ERR; } } else { /*先将新行插入第一个无效列中*/ if (OK != insertToEmptyCol(stcurRowColNum.iArrayCol)) { return ERR; } /*从刚插入的新列开始,按列两两交换,直到把插入的列 置换到参数指定的位置*/ for (int j = stcurRowColNum.iArrayCol; j > iInsertCol; j--) { for (int i = 0; i < stcurRowColNum.iArrayRow; i++) { Node curNode; Node nextNode; curNode.x = i; curNode.y = j - 1; nextNode.x = i; nextNode.y = j; if (OK != SwapNode(curNode, nextNode)) { return ERR; } } } } /*数组有效列数加1*/ SetRowAndColNum(stcurRowColNum.iArrayRow, stcurRowColNum.iArrayCol + 1); return OK; } /*测试代码*/ int main(int argc, char* argv[]) { assert(OK == InitGrid(5, 5)); PathNodeList *pList = NULL; Node node; node.x = 3; node.y = 4; Node node1; node1.x = 1; node1.y = 1; Node node2; node2.x = 4; node2.y = 4; assert(OK == SwapNode(node, node1)); assert(OK == insertRowtoGrid(1)); assert(OK == insertColtoGrid(3)); assert(OK == GetNodePath(node2, &pList)); PathNodeList *p = pList; while (NULL != p) { printf("(%d\t%d)\n", p->node.x, p->node.y); p = p->pNext; } assert(OK == DestoryGrid()); return 0; }
|