#include"stdio.h"
#include"stdlib.h"
#define MAXSIZE 1000
bool flag=true;
typedef struct MyStruct
{
int data;
int cur;
}stlist[MAXSIZE];
//初始化链表
void InitStlist(stlist &s){
for (int i = 0; i < MAXSIZE-2; i++) s[i].cur=i+1;
s[MAXSIZE-2].cur=0;
s[MAXSIZE-1].cur=0;
flag=false;
}
//静态链表内存分配
int Malloc(stlist s){
int i=s[0].cur;
if(i) s[0].cur=s[i].cur;
return i;
}
//获取指定元素数据在数组中的位置
int LocateElem(stlist s,int e){
int i=s[MAXSIZE-1].cur;
while (i&&s[i].data!=e)
{
i=s[i].cur;
}
return i;//在数组中的位置
}
//回收空间
void Free(stlist &s,int j){
int i=s[0].cur;
s[0].cur=j;
s[j].cur=i;
}
//插入数据
void Insert(stlist &s,int e,int posi){
if (flag) InitStlist(s);
int i=Malloc(s),j=MAXSIZE-1,m=0;
if (i)
{
while (s[j].cur&&m<posi-1)
{
j=s[j].cur;
m++;
}
s[i].cur=s[j].cur;
s[j].cur=i;
s[i].data=e;
}
}
void Dele(stlist &s,int posi){
int i=MAXSIZE-1,j,m=0;
while (s[i].cur&&m<posi-1)
{
i=s[i].cur;
m++;
}
if(!s[i].cur||posi<1) exit(0);
j=s[i].cur;
s[i].cur=s[j].cur;
Free(s,j);
}
int Findelem(stlist s,int pos){
int i=0,j=MAXSIZE-1;
while (j&&i<pos)
{
j=s[j].cur;
i++;
}
if (j&&pos<1) exit(0);
return s[j].data;
}
静态链表的创建、插入、删除···
最新推荐文章于 2024-01-26 11:22:15 发布