静态链表的创建、插入、删除···

#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;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值