/*不得不说,静态链表的思想很巧妙啊*/
#include <stdio.h>
#define LEN 1000
typedef struct
{
int data;
int cur;
}StaticLinkList;
/*Initialize the static link list*/
void InitList(StaticLinkList list[])
{
int i;
for(i=0;i<LEN-1;i++)
{
list[i].cur=i+1;
}
list[LEN-1].cur=1;
}
/*Return the spare element index*/
int Malloc_SLL(StaticLinkList list[])
{
int i=list[0].cur;
if(list[0].cur)
{
list[0].cur=list[i].cur;
}
return i;
}
/*Insert an element after the i-th element*/
int InsertElement(StaticLinkList list[],int i,int data)
{
int j;
if(i<1||i>LEN-1)
{
return 0;
}
j=Malloc_SLL(list);
if(j)
{
list[j].data=data;
list[j].cur=list[i].cur;
list[i].cur=j;
return 1;
}
return 0;
}
/*Let the list[0] point to the new spare element*/
void Free_SLL(StaticLinkList list[],int k)
{
list[k].cur=list[0].cur;
list[0].cur=k;
}
/*Delete the i-th element*/
int DeleteElement(StaticLinkList list[],int i)
{
int j,k;
if(i<1||i>LEN)
{
return 0;
}
k=LEN-1;
for(j=0;j<i-1;j++)
{
k=list[k].cur;
}
j=list[k].cur;
list[k].cur=list[j].cur;
Free_SLL(list,j);
return 1;
}