静态链表

#include <iostream.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef struct
{
	int data;
	int cur;
}StaticLinkList[MAXSIZE];

/////////////////////////////////
int InitList(StaticLinkList space)   //初始化
{
	int i;
	for(i=0;i<MAXSIZE-1;i++)
	{
		space[i].cur=i+1;
        space[i].data=i;
	}
	space[MAXSIZE-1].cur=0;
	return OK;
}

//////////////////////////////////
int Malloc_SSL(StaticLinkList space) //仿malloc
{
	int i=space[0].cur;
	if(space[0].cur)
	{
		space[0].cur=space[i].cur;
	}
	
	return i;
}

////////////////////////////////////
int ListInsert(StaticLinkList L,int i,int e)  //插入数据
{
	int ListLength(StaticLinkList L);
	int j,k,l;
	k=MAXSIZE-1;
	if(i<1||i>ListLength(L))       
	{
		return ERROR;
	}
	j=Malloc_SSL(L);
	if(j)
	{
		L[j].data=e;
		for(l=1;l<i-1;l++)
		{
			k=L[k].cur;
		}
		L[j].cur=L[k].cur;
		L[k].cur=j;
		return OK;
	}
	return ERROR;
}

//////////////////////////////////////////
int ListDelete(StaticLinkList L,int i)   //删除数据
{
    void Free_SSL(StaticLinkList space,int k);       
	int ListLength(StaticLinkList L);                
	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;
}

///////////////////////////////////////
void Free_SSL(StaticLinkList space,int k) //回收下标为k的内存
{
	space[k].cur=space[0].cur;
	space[0].cur=k;
}

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

/////////////////////////////////////////
void ListPrint(StaticLinkList L)  //输出数据
{
	int i=L[MAXSIZE-1].cur;
	while(i)
	{
		i=L[i].cur;
		cout<<L[i].data<<"   ";
	}
	cout<<endl;
}

/////////////////////////////////////////
int main ()
{
	return 0;
}

你可能感兴趣的:(静态链表)