靜態鏈表,實際上就是在一個塊內存或者大數組上,通過索引遊標實現鏈表式的管理。
一個主要的應用場景就是,一張有限長度的有序表 存在高頻次增、刪操作的情況。
///////////////////////////////////////////////////////////////////////////////////////////////////////////
源碼已在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 ************************** */