C: Common Operations on two-dimensional arrays

Source: Internet
Author: User
Tags array definition

C: Common Operations on two-dimensional arrays

/* Description: The program inserts columns, inserts rows, and exchanges two elements at the specified position in a two-dimensional array, and outputs the change track of the element at the specified position. Author: socrates Date: 2014-08-17 */# include "stdafx. h "# include
 
  
# Include/* Maximum number of rows and columns of a two-dimensional array */# define MAX_ROW_NUM (9) # define MAX_COL_NUM (9) /* location information of each element in a two-dimensional array */typedef struct _ PathNode {int x;/* row */int y;/* column */} Node; /* content of each element (indicating the linked list of the position track) */typedef struct _ PathNodeList {Node node; _ PathNodeList * pNext;} PathNodeList; /* indicates the number of valid columns in a two-dimensional array */typedef struct _ CurRowColNum {int iArrayRow; int iArrayCol;} CurRowColNum; /* number of two-dimensional array columns */static CurRowColNum g_stRowColNum = {0};/* Two-dimensional array definition */PathNodeList * G_pGrid [MAX_ROW_NUM] [MAX_COL_NUM];/* error code */enum _ RetCode {ERR =-1,/* failure */OK = 0/* success */} RETCODE; /* set the number of columns in the current array */void SetRowAndColNum (int iRow, int iCol) {rows = iRow; g_stRowColNum.iArrayCol = iCol;}/* Get the number of columns in the array */CurRowColNum getrow () {return g_stRowColNum;}/* Create a track linked list with no leading Node */int CreatePathNodelist (PathNodeList * & pList, Node * pPathNode) {if (NULL = pPathNode) {return ERR;} p List = (PathNodeList *) malloc (sizeof (PathNodeList); if (NULL = pList) {return ERR;} pList-> node. x = pPathNode-> x; pList-> node. y = pPathNode-> y; pList-> pNext = NULL; return OK;}/* destroy track linked list */int DestoryPathNodelist (PathNodeList * pList) {if (NULL = pList) {return ERR;} PathNodeList * p = pList; while (NULL! = P-> pNext) {p = p-> pNext; free (pList); pList = p;} free (pList); pList = NULL; return OK ;} /* Insert the current Node location to the track linked list */int InsertPathNodeList (PathNodeList * pPathNodeList, Node * pPathNode) {if (NULL = pPathNodeList) | (NULL = pPathNode) {return ERR;} PathNodeList * pNewPathNode = (PathNodeList *) malloc (sizeof (PathNodeList); if (NULL = pPathNode) {return ERR;} pNewPathNode-> node. x = pPathNode-> x; PNewPathNode-> node. y = pPathNode-> y; pNewPathNode-> pNext = NULL; PathNodeList * p = pPathNodeList; while (NULL! = P-> pNext) {p = p-> pNext;} p-> pNext = pNewPathNode; return OK;}/* initialize a two-dimensional array */int InitGrid (int iXNum, int iYNum) {if (iXNum <0) | (iXNum> MAX_ROW_NUM) {return ERR;} if (iYNum <0) | (iYNum> MAX_COL_NUM )) {return ERR;}/* store the number of rows and columns in the array */SetRowAndColNum (iXNum, iYNum);/* each element in the array creates a chain table to store its running track, no initialization element is NULL by default */for (int I = 0; I <iXNum; I ++) {for (int j = 0; j <iYNum; j ++) {Node node = {0 }; Node. x = I; node. y = j; if (OK! = CreatePathNodelist (g_pGrid [I] [j], & node) {return ERR ;}} return OK;}/* destroy a two-dimensional array */int DestoryGrid () {CurRowColNum stcurRowColNum = getRowAndColNum (); for (int I = 0; I <stcurRowColNum. iArrayRow; I ++) {for (int j = 0; j <stcurRowColNum. iArrayCol; j ++) {if (OK! = DestoryPathNodelist (g_pGrid [I] [j]) {continue ;}}return OK;}/* Get the current position of the element */Node * GetCurPostion (PathNodeList * pPathList) {if (NULL = pPathList) {return NULL;} PathNodeList * p = pPathList;/* The Last node of the linked list indicates the current position */while (NULL! = P-> pNext) {p = p-> pNext;} return & (p-> node);}/* switch the location of two nodes */int SwapNode (Node aNode, node bNode) {Node * paNode = GetCurPostion (g_pGrid [aNode. x] [aNode. y]); Node * pbNode = GetCurPostion (g_pGrid [bNode. x] [bNode. y]);/* Insert the current position of Node B to the linked list of node a */if (OK! = InsertPathNodeList (g_pGrid [aNode. x] [aNode. y], pbNode) {return ERR;}/* Insert the current position of node a to the linked list of Node B */if (OK! = InsertPathNodeList (g_pGrid [bNode. x] [bNode. y], paNode) {return ERR;}/* exchange a and B nodes */PathNodeList * pTmp = NULL; pTmp = g_pGrid [aNode. x] [aNode. y]; g_pGrid [aNode. x] [aNode. y] = g_pGrid [bNode. x] [bNode. y]; g_pGrid [bNode. x] [bNode. y] = pTmp; return OK;}/* Insert elements into unused rows in the array */int insertToEmptyRow (int iRow) {CurRowColNum stcurRowColNum = getRowAndColNum (); for (int j = 0; j <stcurRowColNum. iArrayCol; j ++) {N Ode node = {0}; node. x = iRow; node. y = j; if (OK! = CreatePathNodelist (g_pGrid [iRow] [j], & node) {return ERR ;}return OK;}/* Insert a row to a two-dimensional array */int insertRowtoGrid (int iInsertRow) {if (MAX_ROW_NUM <= iInsertRow) {return ERR;} CurRowColNum stcurRowColNum = getRowAndColNum ();/* Insert invalid rows in the array directly and insert them in the middle of existing rows, the position where the row elements need to be exchanged from the first to the second */if (iInsertRow> = stcurRowColNum. iArrayRow) {if (OK! = InsertToEmptyRow (iInsertRow) {return ERR ;}} else {/* Insert a new row into the first invalid row */if (OK! = InsertToEmptyRow (stcurRowColNum. iArrayRow) {return ERR;}/* switch by two rows starting from the newly inserted new row, until the inserted row is replaced by the specified position */for (int I = stcurRowColNum. iArrayRow; I> iInsertRow; I --) {for (int j = 0; j <stcurRowColNum. iArrayCol; j ++) {Node curNode; Node nextNode; curNode. x = I-1; curNode. y = j; nextNode. x = I; nextNode. y = j; if (OK! = SwapNode (curNode, nextNode) {return ERR ;}}}/* Add 1 */SetRowAndColNum (stcurRowColNum. iArrayRow + 1, stcurRowColNum. iArrayCol); return OK;}/* Get the trajectory of the specified Node in the two-dimensional array */int GetNodePath (Node pathNode, PathNodeList ** pPathList) {CurRowColNum stcurRowColNum = getRowAndColNum (); if (pathNode. x <0) | (pathNode. x> = stcurRowColNum. iArrayRow) {* pPathList = NULL; return ERR;} if (pathNode. y <0) | (PathNode. y> = stcurRowColNum. iArrayCol) {* pPathList = NULL; return ERR;} * pPathList = g_pGrid [pathNode. x] [pathNode. y]; return OK;}/* Insert an element into a column not used in the array */int insertToEmptyCol (int iCol) {CurRowColNum stcurRowColNum = getRowAndColNum (); 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 ;} /* Insert a column into the two-dimensional array */int insertColtoGrid (int iInsertCol) {if (MAX_COL_NUM <= iInsertCol) {return ERR;} CurRowColNum stcurRowColNum = getRowAndColNum (); /* Insert an invalid column in the array and insert it directly into the middle of the existing column. You need to change the position of the column elements from the first to the second. */if (iInsertCol> = stcurRowColNum. iArrayCol) {if (OK! = InsertToEmptyCol (iInsertCol) {return ERR ;}} else {/* Insert a new row into the first invalid column */if (OK! = InsertToEmptyCol (stcurRowColNum. iArrayCol) {return ERR;}/* Exchange by two columns starting from the newly inserted column, until the inserted column is replaced with the specified position */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 ;}}}/* Add 1 */SetRowAndColNum (stcurRowColNum. iArrayRow, stcurRowColNum. iArrayCol + 1); return OK;}/* test code */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); ass Ert (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 ;}
 

Related Article

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.