next_node.c

来自「稀疏矩阵、链表、图、队列、二叉树、多叉树、排序、遗传算法等的实现」· C语言 代码 · 共 52 行

C
52
字号
/**************************************************************************
**  SP_NEXT_NODE                                                         **
**                                                                       **
**    Returns the next node in a given dimension.                        **
**                                                                       **
**  INPUT:                                                               **
**    sp -- The sparse matrix which contains the nodes                   **
**    dim -- The dimension of the node being requested                   **
**    node -- The current node                                           **
**                                                                       **
**  OUTPUT:                                                              **
**    SP_NODE -- A pointer to the node                                   **
**                                                                       **
**  SIDE EFFECTS:                                                        **
**    The error_no field of the sparse matrix can be set to an error if  **
**  an error is encountered.  Whenever an error is encountered, this     **
**  value is set and a NULL pointer is returned.  Thus, if a NULL        **
**  pointer is returned from this function it is important to examine    **
**  the error_no of the associated sparse matrix before assuming that    **
**  the node list is empty.                                              **
**                                                                       **
**  NOTES:                                                               **
**                                                                       **
**************************************************************************/

#include <stdio.h>
#include "sparse.h"

SP_NODE *sp_next_node(SPARSE_MATRIX *sp, int dim, SP_NODE *node)
/* SPARSE_MATRIX *sp  The sparse matrix in which to find the node */
/* int dim            The dimension number of the next node to be found */
/* SP_NODE *node      The current node */
{
  /* If the sparse matrix passed is empty, then there are no nodes to be found */
  if (sp == (SPARSE_MATRIX *)NULL)
    return ((SP_NODE *)NULL);

  sp->error_no = SP_NOERR;

  /* Make sure the dimension is within range */
  if (dim > sp->dimensions)
  {
    sp->error_no = SP_DIM;
    return((SP_NODE *)NULL);
  }

  /* The stack has next, then previous node pointers for each dimension in that 
     order, so there are 2 pointers for each dimension.  The next is the first
     of the two, so the offset is 2 * dim */
  return(*((SP_NODE **)(node->dimension_stack + (2 * (dim - 1)))));
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?