Static List

Static List
Static List is the smart implementation of list data structure for those languages that have no pointer or similar function, such as Pascal, Basic. It uses an auxilary array to store the locations of data in the main array, which is known as cursor,the supplicant of pointer.

Static List data structure
#define LIST_INIT_SIZE 100

typedef struct
{
ElemType data;
int cur; /*cursor,mean no pointing if 0.*/
}Component,StaticLinkList[LIST_INIT_SIZE];
How static list is organized:
1.Index 0 is reserved and stores no data.
2.Cursor 0 points to first empty index.
3.Last Cursor points to index first data element.
4.Each cursor points to index of its next element.

Operations:
/*space[0].cur is head pointer,
"0" indicates null pointer.*/
Status InitList(StaticLinkList space)
{
int i;
for(i=0;i<LIST_INIT_SIZE-1;i++)
{
space[i].cur=i+1;
}
/*current static linked list is empty,
cur of last element is 0.*/
space[LIST_INIT_SIZE-1].cur=0;
return OK;
}

/*return the cur of allocated node if the linked list is not
empty, otherwise return 0.*/
int AllocSLL(StaticLinkList space)
{
int i=space[0].cur;
if(space[0].cur)
{
space[0].cur=space[i].cur;
}
return i;
}

/*insert a new element e before i element.*/
Status ListInsert(StaticLinkList L,int i,ElemType e)
{
int j,k,l;
/*k set as cur of last element.*/
k=LIST_INIT_SIZE-1;
if(i<1||i>ListLength(L)+1)
{
return ERROR;
}

/*obtain cur of the allocated node.*/
j=AllocSSL(L);
if(j)
{
L[j].data=e;
for(l=1;l<=i-1;l++)
{
L[j].cur=L[k].cur;
}
L[k].cur=j;
return OK;
}
return ERROR;
}

void Free_SSL(StaticLinkList space,int k)
{
space[k].cur=space[0].cur;
space[0].cur=k;
}

Status ListDelete(StaticLinkList L,int i)
{
int j,k;
if(i<1||i>ListLength(L))
{
return ERROR;
}
k=MAXSIZE-1;
for(j=1;j<=i-1;j++)
{
k=L[k].cur;
}
j=L[k].cur;
L[k].cur=L[j].cur;
Free_SSL(L,j);
return OK;
}

int ListLength(StaticLinkList L)
{
int j=0;
int i=L[MAXSIZE-1].cur;
while(i)
{
i=L[i].cur;
j++;
}
return j;
}

原文地址:https://www.cnblogs.com/askDing/p/5978401.html