⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linklist.c

📁 <Win2k系统编程>源码.次数为国人自编,内容丰富,还是不错的.
💻 C
📖 第 1 页 / 共 2 页
字号:
// ************************************************************************
BOOL
SetCurrentNodeEx( PLIST pList, PNODE pKeyNode,
  int (*MatchFunction)( PNODE pNodeOne, PNODE pNodeTwo ),
  int Occurence )
{
  int Position;

  //-- if list is empty, exit
  if( pList->pCurrentNode == NULL ) {
    pList->ListError = LIST_ERROR_NO_NODE;
    return( FALSE );
  }

  //-- if match and order are same function
  if( MatchFunction == (pList->OrderFunction) ) {
    int  LastPosition;

    LastPosition = Position = (*MatchFunction)(pKeyNode, pList->pCurrentNode);

    while( Occurence ) {
      if( ( Position == LIST_LEFT_OF ) && ( LastPosition == LIST_LEFT_OF )
          && ( pList->pCurrentNode != pList->pFirstNode ) )
        pList->pCurrentNode = pList->pCurrentNode->pLeftLink;
      else if( ( Position == LIST_RIGHT_OF ) && ( LastPosition == LIST_RIGHT_OF )
            && ( pList->pCurrentNode != pList->pLastNode ) )
        pList->pCurrentNode = pList->pCurrentNode->pRightLink;
      else {
        Occurence--;
        continue;
      }
      LastPosition = Position;
      Position = (*MatchFunction)(pKeyNode, pList->pCurrentNode);
    }

  }
  //-- match and order are not the same function, thus start
  //   the search at the front of the list
  else {
    pList->pCurrentNode = pList->pFirstNode;
    while( (Occurence > 0) && ( (pList->pCurrentNode) != NULL ) ) {
      Position = (*MatchFunction)(pKeyNode, pList->pCurrentNode);
      if( Position == LIST_MATCH )
        Occurence--;
      if( Occurence > 0 )
        pList->pCurrentNode = pList->pCurrentNode->pRightLink;
    }
  }

  if( ( Position == LIST_MATCH ) && ( Occurence == 0 ) ) {
    pList->ListError = LIST_NO_ERROR;
    return( TRUE );
  }
  pList->ListError = LIST_ERROR_NO_MATCH;

  return( FALSE );
}


// ************************************************************************
// FUNCTION : GetCurrentNode( PLIST pList, PNODE* )
// PURPOSE  : gets the current node from the list
// COMMENTS :
//   Does not change pList->pCurrentNode.
//   Changes ppNode, thus do not pass in a ppNode which has additional
//   memory associated with it.
// ************************************************************************
BOOL
GetCurrentNode( PLIST pList, PNODE* ppNode )
{
  CHECK_POINTER( ppNode );
  if( pList->pCurrentNode == NULL ) {
    pList->ListError = LIST_ERROR_NO_NODE;
    return( FALSE );
  }
  *ppNode = pList->pCurrentNode;
  pList->ListError = LIST_NO_ERROR;

  return( TRUE );
}


// ************************************************************************
// FUNCTION : GetFirstNode( PLIST pList, PNODE* )
// PURPOSE  : gets the first (left-most) node from the list
// COMMENTS :
//   Does not change pList->pCurrentNode.
//   Changes ppNode, thus do not pass in a ppNode which has additional
//   memory associated with it.
// ************************************************************************
BOOL
GetFirstNode( PLIST pList, PNODE* ppNode )
{
  CHECK_POINTER( ppNode );
  if( pList->pFirstNode == NULL ) {
    pList->ListError = LIST_ERROR_NO_NODE;
    return( FALSE );
  }
  *ppNode = pList->pFirstNode;
  pList->ListError = LIST_NO_ERROR;

  return( TRUE );
}


// ************************************************************************
// FUNCTION : GetLastNode( PLIST pList, PNODE* )
// PURPOSE  : get the last (right-most) node from the List
// COMMENTS :
//   Does not change pList->pCurrentNode.
//   Changes ppNode, thus do not pass in a ppNode which has additional
//   memory associated with it.
// ************************************************************************
BOOL
GetLastNode( PLIST pList, PNODE* ppNode )
{
  CHECK_POINTER( ppNode );
  if( pList->pLastNode == NULL ) {
    pList->ListError = LIST_ERROR_NO_NODE;
    return( FALSE );
  }
  *ppNode = pList->pLastNode;
  pList->ListError = LIST_NO_ERROR;

  return( TRUE );
}


// ************************************************************************
// FUNCTION : GetNextNode( PLIST pList, PNODE* )
// PURPOSE  : get the next (right) node from the pList->pCurrentNode
// COMMENTS :
//   Does not change pList->pCurrentNode.
//   Changes ppNode, thus do not pass in a ppNode which has additional
//   memory associated with it.
// ************************************************************************
BOOL
GetNextNode( PLIST pList, PNODE* ppNode )
{
  CHECK_POINTER( ppNode );
  if( (*ppNode)->pRightLink == NULL ) {
    pList->ListError = LIST_ERROR_NO_NODE;
    return( FALSE );
  }
  *ppNode = (*ppNode)->pRightLink;
  pList->ListError = LIST_NO_ERROR;

  return( TRUE );
}


// ************************************************************************
// FUNCTION : GetPrevNode( PLIST pList, PNODE* )
// PURPOSE  : get the previous (left) node from the pList->pCurrentNode
// COMMENTS :
//   Does not change pList->pCurrentNode.
//   Changes ppNode, thus do not pass in a ppNode which has additional
//   memory associated with it.
// ************************************************************************
BOOL
GetPrevNode( PLIST pList, PNODE* ppNode )
{
  CHECK_POINTER( ppNode );
  if( (*ppNode)->pLeftLink == NULL ) {
    pList->ListError = LIST_ERROR_NO_NODE;
    return( FALSE );
  }
  *ppNode = (*ppNode)->pLeftLink;
  pList->ListError = LIST_NO_ERROR;

  return( TRUE );
}


// ************************************************************************
// FUNCTION : DeleteCurrentNode( PLIST pList )
// PURPOSE  : deletes the current node from the list and frees the memory
//            associated with it
// COMMENTS :
//   Changes pList->pCurrentNode.
//
//   Typically, SetCurrentNode (or SetCurrentNodeEx) is called first to set
//   pList->pCurrentNode.  Also, any addtional memory associated with this
//   node should be freed first before calling this function.
// ************************************************************************
BOOL
DeleteCurrentNode( PLIST pList )
{
  PNODE pOldCurrentNode;

  if( pList->pCurrentNode != NULL ) {
    pOldCurrentNode = pList->pCurrentNode;

    if( pOldCurrentNode == pList->pFirstNode ) {
      pList->pFirstNode   = pOldCurrentNode->pRightLink;
      pList->pCurrentNode = pOldCurrentNode->pRightLink;
    }
    else {
      pOldCurrentNode->pLeftLink->pRightLink = pOldCurrentNode->pRightLink;
      pList->pCurrentNode = pOldCurrentNode->pLeftLink;
    }

    if( pOldCurrentNode == pList->pLastNode )
      pList->pLastNode = pOldCurrentNode->pLeftLink;
    else
      pOldCurrentNode->pRightLink->pLeftLink = pOldCurrentNode->pLeftLink;

    DestroyNode( pOldCurrentNode );
    pList->ListError = LIST_NO_ERROR;
    return( TRUE );
  }
  pList->ListError = LIST_NO_ERROR;

  return( TRUE );
}


// ************************************************************************
// FUNCTION : DestroyNode( PNODE pNode )
// PURPOSE  : deallocates a node
// COMMENTS :
// ************************************************************************
BOOL
DestroyNode( PNODE pNode )
{
  free( pNode );

  return( TRUE );
}


// ************************************************************************
// FUNCTION : DestroyList( PLIST pList )
// PURPOSE  : deallocates a list, does not free any nodes, if present
// COMMENTS :
// ************************************************************************
BOOL
DestroyList( PLIST pList )
{
  free( pList );
  pList->ListError = LIST_NO_ERROR;

  return( TRUE );
}


// ************************************************************************
// FUNCTION : GetListError( PLIST pList )
// PURPOSE  : get the last linked list error
// COMMENTS :
// ************************************************************************
int
GetListError( PLIST pList )
{
  return( pList->ListError );
}

⌨️ 快捷键说明

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