TraceTableUnit

本文介绍了一个用于管理和操作二维表格数据的C语言程序库。该库支持表格的初始化、销毁、节点交换、行列插入等功能,并提供了查询节点变化轨迹的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

******************************************************************************
  File Name     :
  Version       :
  Author        :
  Created       : 2009/6/1
  Last Modified :
  Description   :
  Function List :
             
  History       :
  1.Date        : 2009/6/1
    Author      :
    Modification: Created file

******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 数据表规格定义
#define MAX_ROW_LENGTH    9
#define MAX_COLUMN_LENGTH 9

// 数据表节点位置信息定义
typedef struct Table_Node
{
    int  iRowNo;    //行号, 从0开始编号
    int  iColumnNo; //列号, 从0开始编号
}TABLE_NODE;


// 节点轨迹变化链表信息
typedef struct Orbit_List
{
 TABLE_NODE   node;           //节点位置信息
 struct Orbit_List *pNext;
}ORBIT_LIST;

struct table
{
 int total_row_num;
 int total_colum_num;
 ORBIT_LIST *table_mem;
};

struct table g_table = {0, 0, NULL};
int g_max_row_num = 0;
int g_max_col_num = 0;
void DestroyTable (void);
/*****************************************************************************
Description  : 初始化数据表.
Prototype    : int InitTable (int iRowLength, int iColumnLength)
Parameters   : int iRowLenth    - 数据表的行数
               int iColumnLenth - 数据表的列数
Return Value : int - 初始化成功返回0, 失败返回-1
*****************************************************************************/
int InitTable(int iRowLength, int iColumnLength)
{
    // 请在这里实现功能
    int i = 0;
    int j = 0;
    ORBIT_LIST *table_mem = NULL;
  
 if ((iRowLength > MAX_ROW_LENGTH) || (iColumnLength > MAX_COLUMN_LENGTH)) {
  printf("row length or column length is invalid.\n");
  return -1;
 }

 if ((iRowLength <= 0) || (iColumnLength <= 0)) {
  printf("row length or column length is invalid.\n");
  return -1;
 }

 if (NULL != g_table.table_mem) {
  if((g_table.total_row_num == iRowLength)\
   && (g_table.total_colum_num == iColumnLength))
   return -1;
  else/*不是重复初始化*/
   DestroyTable();
 }

 table_mem = (ORBIT_LIST *)malloc((MAX_COLUMN_LENGTH*MAX_ROW_LENGTH) * sizeof(ORBIT_LIST));
 if (NULL == table_mem) {
  printf("memory is not enough for ORBIT_LIST.\n");
  return -1;
 }
 memset(table_mem, 0, (iColumnLength*iRowLength) * sizeof(ORBIT_LIST));

 for (i = 0; i < iRowLength; i++) {
  for(j = 0; j < iColumnLength; j++) {
   (*(table_mem + iColumnLength * i + j)).node.iRowNo = i;
   (*(table_mem + iColumnLength * i + j)).node.iColumnNo = j;
   (*(table_mem + iColumnLength * i + j)).pNext = NULL;
  }
 }

 g_table.total_row_num = iRowLength;
 g_table.total_colum_num = iColumnLength;
 g_table.table_mem = table_mem;

 g_max_row_num = iRowLength;
 g_max_col_num = iColumnLength;

 return 0;
}

/*****************************************************************************
Description   : 销毁数据表.
Prototype     : void DestroyTable (void)
Parameters    : 无
Return Value  : 无
*****************************************************************************/
void DestroyTable (void)
{
    // 请在这里实现功能
    unsigned int i = 0;
    unsigned int j = 0;
    ORBIT_LIST *table_mem = NULL;
    ORBIT_LIST *table_curr = NULL;

 table_mem = g_table.table_mem;
 if (NULL == g_table.table_mem)
  return;

 while(i < g_table.total_row_num * g_table.total_colum_num) {
  while((*(table_mem + i)).pNext != NULL) {
   table_curr = (*(table_mem + i)).pNext;
   (table_mem + i)->pNext = table_curr->pNext;
   free(table_curr);
   table_curr = NULL;
  }
  i++;
 }
 free(table_mem);
 table_mem = NULL;
 g_table.table_mem = NULL;
 g_table.total_row_num = 0;
 g_table.total_colum_num = 0;
 g_max_row_num = 0;
 g_max_col_num = 0;
    return;
}

/*****************************************************************************
Description  : 交换数据表的两个指定单元.
Prototype    : int SwapUnit (TABLE_NODE stNode1, TABLE_NODE stNode2)
Parameters   : TABLE_NODE stNode1 - 指定交换的节点1
               TABLE_NODE stNode2 - 指定交换的节点2
Return Value : int - 交换成功返回0, 失败返回-1
*****************************************************************************/
int SwapUnit (TABLE_NODE stNode1, TABLE_NODE stNode2)
{
    // 请在这里实现功能
    int i = 0;
    int j = 0;
    int update_flag1 = 0;
    int update_flag2 = 0;
    ORBIT_LIST *table_mem = NULL;
    ORBIT_LIST *node1_list = NULL;
    ORBIT_LIST *node2_list = NULL;
    ORBIT_LIST *node_temp = NULL;
    ORBIT_LIST *node1_new_orbit = NULL;
    ORBIT_LIST *node2_new_orbit = NULL;

 table_mem = g_table.table_mem;
 if (NULL == table_mem) {
  printf("no table.\n");
  return -1;
 }

 if ((stNode1.iRowNo > g_max_row_num - 1)\
  || (stNode2.iRowNo > g_max_row_num - 1)\
  || (stNode1.iColumnNo > g_max_col_num - 1)\
  || (stNode2.iColumnNo > g_max_col_num - 1)) {
  printf("node is invalid.\n");
  return -1;
 }

 if ((stNode1.iRowNo == stNode2.iRowNo)\
  && (stNode1.iColumnNo == stNode2.iColumnNo)) {
  return 0;
 }

 for(i = 0; i < g_table.total_row_num; i++) {
  for(j = 0; j < g_table.total_colum_num; j++) {
   node_temp = table_mem + g_table.total_colum_num * i + j;
   while(node_temp->pNext) {
    node_temp = node_temp->pNext;
   }
   if ((node_temp->node.iRowNo == stNode1.iRowNo)\
    && (node_temp->node.iColumnNo == stNode1.iColumnNo)) {
     node1_list = node_temp;
     update_flag1 = 1;
     continue;
   }
   if ((node_temp->node.iRowNo == stNode2.iRowNo)\
    && (node_temp->node.iColumnNo == stNode2.iColumnNo)) {
     node2_list = node_temp;

     update_flag2 = 1;
     continue;
   }
  }
 }
 if (update_flag1 == 1) {
  node1_new_orbit = (ORBIT_LIST *)malloc(sizeof(ORBIT_LIST));
  if (NULL == node1_new_orbit) {
   printf("memory is not enough for node1_new_orbit.\n");
   return -1;
  }
  node1_new_orbit->node.iRowNo = stNode2.iRowNo;
  node1_new_orbit->node.iColumnNo = stNode2.iColumnNo;
  node1_new_orbit->pNext = NULL;
  while(node1_list->pNext) {
    node1_list = node1_list->pNext;
  }
  node1_list->pNext = node1_new_orbit;
 }

 if (update_flag2 == 1) {
  node2_new_orbit = (ORBIT_LIST *)malloc(sizeof(ORBIT_LIST));
  if (NULL == node2_new_orbit) {
   printf("memory is not enough for node2_new_orbit.\n");
   free(node1_new_orbit);
   node1_new_orbit = NULL;
   return -1;
  }
  node2_new_orbit->node.iRowNo = stNode1.iRowNo;
  node2_new_orbit->node.iColumnNo = stNode1.iColumnNo;
  node2_new_orbit->pNext = NULL;
  node2_list->pNext = node2_new_orbit;
 }

 return 0;
}

/*****************************************************************************
Description  : 在数据表中插入一列
Prototype    : int InsertColumnToTable (int iColumnNo)
Parameters   : iColumnNo - 指定插入的列号
Return Value : 插入成功返回0, 失败返回-1
*****************************************************************************/
int InsertColumnToTable(int iColumnNo)
{
    // 请在这里实现功能
    unsigned int i = 0;
    unsigned int j = 0;
    ORBIT_LIST *table_mem = NULL;
    ORBIT_LIST *node1_list = NULL;
    ORBIT_LIST *node1_new_orbit = NULL;

 table_mem = g_table.table_mem;
 if (NULL == table_mem) {
  printf("no table.\n");
  return -1;
 }

 if (iColumnNo > g_max_col_num){
  printf("iColumnNo is invalid.\n");
  return -1;
 }

 if (g_max_col_num == MAX_COLUMN_LENGTH) {
  printf("Column is the max number.\n");
  return -1;
 }

 for(i = 0; i < g_table.total_row_num; i++) {
  for(j = 0; j < g_table.total_colum_num; j++) {
   node1_list = table_mem + g_table.total_colum_num * i + j;
   while(node1_list->pNext) {
    node1_list = node1_list->pNext;
   }
   if (node1_list->node.iColumnNo < iColumnNo)
    continue;
   node1_new_orbit = (ORBIT_LIST *)malloc(sizeof(ORBIT_LIST));
   if (NULL == node1_new_orbit) {
    printf("memory is not enough for insetting iColumnNo.\n");
    return -1;
   }
   node1_new_orbit->node.iColumnNo = node1_list->node.iColumnNo + 1;
   node1_new_orbit->node.iRowNo = node1_list->node.iRowNo;
   node1_new_orbit->pNext = NULL;

   node1_list->pNext = node1_new_orbit;
  }
 }

 g_max_col_num += 1;
    return 0;
}


/*****************************************************************************
Description  : 在数据表中插入一行
Prototype    : int InsertRowToTable(int iRowNo)
Parameters   : iColumnNo - 指定插入的行号
Return Value : 插入成功返回0, 失败返回-1
*****************************************************************************/
int InsertRowToTable(int iRowNo)
{
    // 请在这里实现功能
    unsigned int i = 0;
    unsigned int j = 0;
    ORBIT_LIST *table_mem = NULL;
    ORBIT_LIST *node1_list = NULL;
    ORBIT_LIST *node1_new_orbit = NULL;

 table_mem = g_table.table_mem;
 if (NULL == table_mem) {
  printf("no table.\n");
  return -1;
 }

 if (iRowNo > g_max_row_num){
  printf("iRowNo is invalid.\n");
  return -1;
 }

 if (g_max_row_num >= MAX_ROW_LENGTH) {
  printf("Column is the max number.\n");
  return -1;
 }

 for(j = 0; j < g_table.total_colum_num; j++) {
  for(i = 0; i < g_table.total_row_num; i++) {
   node1_list = table_mem + g_table.total_colum_num * i + j;
   while(node1_list->pNext) {
    node1_list = node1_list->pNext;
   }
   if (node1_list->node.iRowNo < iRowNo)
    continue;
   node1_new_orbit = (ORBIT_LIST *)malloc(sizeof(ORBIT_LIST));
   if (NULL == node1_new_orbit) {
    printf("memory is not enough for insetting iColumnNo.\n");
    return -1;
   }
   node1_new_orbit->node.iColumnNo = node1_list->node.iColumnNo;
   node1_new_orbit->node.iRowNo = node1_list->node.iRowNo + 1;
   node1_new_orbit->pNext = NULL;

   node1_list->pNext = node1_new_orbit;
  }
 }
 g_max_row_num += 1;

    return 0;
}

/*******************************************************************************************************
Description  : 查询数据表中某个节点的位置变化轨迹.
Prototype    : int QueryOrbitOfUnitData(TABLE_NODE stNode, ORBIT_LIST** ppOrbit)
Parameters   : TABLE_NODE stNode     - 指定查询的原表格中的节点
               ORBIT_LIST** ppOrbit  - 节点变化轨迹链表
                              此链表内存由本函数负责申请,调用者调用FreeOrbitOfUnitData进行释放
Return Value : 查询成功返回0, 失败返回-1
*********************************************************************************************************/
int QueryOrbitOfUnitData(TABLE_NODE stNode, ORBIT_LIST** ppOrbit)
{
    // 请在这里实现功能
    int i = 0;
    int j = 0;
    ORBIT_LIST *table_mem = NULL;

 if (NULL == g_table.table_mem) {
  printf("no table.\n");
  return -1;
 }

 if ((stNode.iRowNo > g_table.total_row_num - 1)\
  || (stNode.iColumnNo > g_table.total_colum_num - 1)) {
  printf("stNode row num or column num is invalid.\n");
  return -1;
 }

 for(j = 0; j < g_table.total_colum_num; j++) {
  for(i = 0; i < g_table.total_row_num; i++) {
   table_mem = g_table.table_mem + g_table.total_colum_num * i + j;
   if ((table_mem->node.iColumnNo == stNode.iColumnNo) && (table_mem->node.iRowNo == stNode.iRowNo)) {
    *ppOrbit = table_mem;
    return 0;
   }
  }
 }

 *ppOrbit = table_mem; 

    return 0;
}

/*****************************************************************************
Description  : 释放查询时返回的节点位置变化轨迹链表的内存.
Prototype    : int FreeOrbitOfUnitData(void* pOrbit)
Parameters   : void* pOrbit   - 释放的链表头指针
Return Value : 无
*****************************************************************************/
void FreeOrbitOfUnitData(void* pOrbit)
{
 // 请在这里实现功能
    ORBIT_LIST *node1_list = NULL;
    ORBIT_LIST *table_curr = NULL;

 node1_list = (ORBIT_LIST *)pOrbit;
 while(node1_list->pNext) {
  table_curr = node1_list->pNext;
  node1_list->pNext = node1_list->pNext->pNext;
  free(table_curr);
  table_curr = NULL;
 }
  
 return;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值