静态链表的实现

来源:互联网 发布:乔约翰逊生涯数据 编辑:程序博客网 时间:2024/06/02 00:24


静态链表,实际上就是在一个块内存或者大数组上,通过索引游标实现链表式的管理。

一个主要的应用场景就是,一张有限长度的有序表 存在高频次增、删操作的情况。


///////////////////////////////////////////////////////////////////////////////////////////////////////////

源码已在VC6.0上验证通过。


/*****************************************************************
 * 文件名:  静态链表
 * 实现功能:实现静态链表的创建、插入、删除、查找等基本功能
 * 作者:    韩立忠  2011.12.23
 ****************************************************************/

/************************* 引入的头文件 *************************/

#include <stdio.h>
#include <memory.h>
#include <assert.h>

/**************************** 宏定义 ****************************/

#define TRUE      1
#define FALSE     0
#define ERROR    -1
#define MAXSIZE 128    // 定义静态链表的最大长度

/************************* 定义数据结构 *************************/

typedef int data_t;

/* 定义静态链表结点 */
typedef struct sNode_s
{
 data_t data;
 unsigned int cur;
}sNode_t;

/************************* 全局函数声明 *************************/

int staticListInit(sNode_t *staticList);                    // 静态链表初始化函数
int staticListInsert(sNode_t *staticList, unsigned int dataPos, data_t data); // 静态链表插入函数
int staticListDelete(sNode_t *staticList, unsigned int dataPos);              // 静态链表删除函数
int staticListMalloc(sNode_t *staticList);                  // 静态链表分配游标函数
int staticListFree(sNode_t *staticList, unsigned int iCur); // 静态链表释放游标函数
unsigned int staticListCount(sNode_t *staticList);          // 静态链表元素计数函数
void staticListTrace(sNode_t *staticList);                  // 静态链表遍历打印函数

/**************************** 主函数 ****************************/

void main(void)
{
 unsigned int i = 0;
 unsigned int j = 0;
 sNode_t staticList[MAXSIZE] = {0};

 staticListInit(staticList);

 for (i = 1; i <= 16; i++)
 {
  staticListInsert(staticList, i, i);
 }
 staticListTrace(staticList);
 
 for (i = 2, j = 0; i <= 16; i += 2, j++)
 {
  staticListDelete(staticList, i - j);
 }
 staticListTrace(staticList);

 for (i = 9; i <= 16; i++)
 {
  staticListInsert(staticList, i, 4 * (i - 8));
 }
 staticListTrace(staticList);

 for (i = 1, j = 0; i <= 16; i += 2, j++)
 {
  staticListDelete(staticList, i - j);
 }
 staticListTrace(staticList);

 return;
}

/* 静态链表初始化游标cur函数 */
int staticListInit(sNode_t *staticList)
{
 unsigned int idx = 0;
 if (NULL == staticList)
 {
  assert(0);
  return ERROR;
 }

 memset(staticList, 0, MAXSIZE * sizeof(sNode_t));

 for (idx = 0; idx < MAXSIZE-1; idx++)
 {
  //(staticList + idx)->cur = idx + 1;
  staticList[idx].cur = idx + 1;
 }
 staticList[MAXSIZE-1].cur = 0;

 return TRUE;
}

/* 静态链表插入数据函数 */
int staticListInsert(sNode_t *staticList, unsigned int dataPos, data_t data)
{
 unsigned int idx = 0;
 unsigned int tmpCuri = 0;
 unsigned int tmpCurj = MAXSIZE - 1;  /* 最后尾元素的游标 */
 if ((NULL == staticList) || ((0 == dataPos) || (dataPos > staticListCount(staticList) + 1)))
 {
  assert(0);
  return ERROR;
 }

 /* 获得空闲分量的游标cur */
 tmpCuri = staticListMalloc(staticList);
 if (0 == tmpCuri)
 {
  return FALSE;
 }

 staticList[tmpCuri].data = data;
 /* 找到第dataPos个元素之前的位置 */
 for (idx = 1; idx <= dataPos - 1; idx++)
 {
  tmpCurj = staticList[tmpCurj].cur;
 }

 /* 将第dataPos-1个元素cur赋值给新元素的cur */
 staticList[tmpCuri].cur = staticList[tmpCurj].cur;
 /* 把新元素的cur赋值给第dataPos-1个元素的cur */
 staticList[tmpCurj].cur = tmpCuri;

 return TRUE;
}

/* 静态链表删除数据函数 */
int staticListDelete(sNode_t *staticList, unsigned int dataPos)
{
 unsigned int tmpCuri = 0;
 unsigned int tmpCurj = MAXSIZE - 1;
 if ((NULL == staticList) || ((0 == dataPos) || (dataPos > staticListCount(staticList))))
 {
  assert(0);
  return ERROR;
 }

 for (tmpCuri = 1; tmpCuri <= dataPos - 1; tmpCuri++)
 {
  tmpCurj = staticList[tmpCurj].cur;
 }
 tmpCuri = staticList[tmpCurj].cur;
 staticList[tmpCurj].cur = staticList[tmpCuri].cur;

 staticListFree(staticList, tmpCuri);

 return TRUE;
}

/* 静态链表游标cur分配函数 */
int staticListMalloc(sNode_t *staticList)
{
 unsigned int idx = 0;
 if (NULL == staticList)
 {
  assert(0);
  return ERROR;
 }

 /* 当前数组第一个元素的cur存的值,即要返回的第一个备用空闲的下标 */
 idx = staticList[0].cur;
 if (staticList[0].cur != 0)
 {
  /* 由于要拿出一个分量来使用,故把它的下一分量用作备用 */
  staticList[0].cur = staticList[idx].cur;
 }

 return idx;
}

/* 静态链表游标cur释放函数 */
int staticListFree(sNode_t *staticList, unsigned int iCur)
{
 if ((NULL == staticList) || ((0 == iCur) || (iCur > MAXSIZE-1)))
 {
  assert(0);
  return ERROR;
 }

 /* 把首元素cur值赋给删除分量cur,把删除分量cur赋值给首元素cur */
 staticList[iCur].cur = staticList[0].cur;
 staticList[0].cur = iCur;

 return TRUE;
}

/* 静态链表元素个数统计函数 */
unsigned int staticListCount(sNode_t *staticList)
{
 unsigned int count = 0;
 unsigned int tmpCur = 0;
 if (NULL == staticList)
 {
  assert(0);
  return ERROR;
 }

 tmpCur = staticList[MAXSIZE - 1].cur;
 while (tmpCur != 0)
 {
  if (tmpCur > MAXSIZE - 1)
  {
   assert(0);
   return ERROR;
  }
  tmpCur = staticList[tmpCur].cur;
  count++;
 }

 return count;
}

void staticListTrace(sNode_t *staticList)
{
 unsigned int tmpCur = 0;
 if (NULL == staticList)
 {
  assert(0);
  return;
 }

 printf("staticList:\n");
 tmpCur = staticList[MAXSIZE - 1].cur;
 while (staticList[tmpCur].cur != 0)
 {
  if (tmpCur > MAXSIZE - 1)
  {
   assert(0);
   return;
  }
  printf("[%d,%d]-->", staticList[tmpCur].data, staticList[tmpCur].cur);
  tmpCur = staticList[tmpCur].cur;
 }
 printf("[%d,%d]\n\n", staticList[tmpCur].data, staticList[tmpCur].cur);

 return;
}

/* ************************* The End ************************** */


1 0
原创粉丝点击