版权声明:本文为博主原创文章,未经博主允许不得转载。
注:为了方便编写,本人使用C++实现,与C语言并无太大差别,仅供参考,只实现部分主要功能,其余可以功能可仿照自行添加;
完整代码:
#include<iostream>
using namespace std;
#define MAXSIZE 10//数组的最大长度
#define SUCCESS 1//操作成功
#define ERROR -1//操作失败
typedef int ElemType;//定义数据类型
int STATUS=0;//定义全局变量,用于判断静态链表是否初始化,0表示没有,1表示已经初始化
typedef struct SL{
ElemType elem;
int cur;
}list;
int ListLength(SL* list);
//获取空闲分量
int Malloc_SL(SL* list){
int i=list[0].cur;
if(list[0].cur){
list[0].cur=list[i].cur;
}
return i;
}
//回收空闲分量
void Free_SL(SL* list,int i){
list[i].cur=list[0].cur;
list[0].cur=i;
}
//1-初始化静态链表(此时将所有节点放入被用链表)
void InitList_SL(SL *list){
if(STATUS==1){
cout<<"链表已经初始化!"<<endl;
return;
}
int i;
for(i=0;i<MAXSIZE-1;i++){
list[i].cur=i+1;
}
//设置数组的最后一个节点为数据链表的头结点
list[MAXSIZE-1].cur=0;
STATUS=1;
cout<<"初始化完毕!"<<endl;
}
//打印完整静态链表(测试用)
void Print(SL* list){
int i;
for(i=0;i<MAXSIZE;i++){
cout<<"实际地址:"<<i<<"\tcur:"<<list[i].cur<<"\t数据:"<<list[i].elem<<endl;
}
}
//2-插入操作
int ListInsert(SL *list,int i,ElemType e){
//判断链表是否存在,以下同
if(STATUS==0){
cout<<"链表不存在!无法操作!"<<endl;
return ERROR;
}
int j,x,k=MAXSIZE-1;
//判断插入位置是否正确
if(i<1 || i>ListLength(list)+1){
cout<<"插入位置错误!"<<endl;
return ERROR;
}
//获取空闲下标分量
x=Malloc_SL(list);
//插入
if(x){
//赋值
list[x].elem=e;
//找到第i个元素的前一位
for(j=1;j<=i-1;j++){
k=list[k].cur;
}
//更改cur
list[x].cur=list[k].cur;
list[k].cur=x;
cout<<"插入完成!"<<endl;
return SUCCESS;
}
cout<<"插入失败!"<<endl;
return ERROR;
}
//3-删除操作
int DeleteList(SL *list,int i){
int j,k;
if(STATUS==0){
cout<<"链表不存在!无法操作!"<<endl;
return ERROR;
}
//判断删除位置是否正确
if(i<1 || i>ListLength(list)){
return ERROR;
}
k=MAXSIZE-1;
for(j=1;j<=i-1;j++){
k=list[k].cur;
}
list[j].elem=0;
j=list[k].cur;
list[k].cur=list[j].cur;
Free_SL(list,j);
return SUCCESS;
}
//4-静态链表的长度
int ListLength(SL* list){
int j=0;
if(STATUS==0){
cout<<"链表不存在!无法操作!"<<endl;
return ERROR;
}
int i=list[MAXSIZE-1].cur;
while(i){
i=list[i].cur;
j++;
}
return j;
}
//5-打印数据链表
void Print_SL(SL* list){
int i=list[MAXSIZE-1].cur;
if(STATUS==0){
cout<<"链表不存在!无法操作!"<<endl;
return;
}
cout<<"当前数据链表为:";
while(i){
cout<<list[i].elem<<"\t";
i=list[i].cur;
}
cout<<endl;
}
//6-返回第i个元素的值
ElemType GetElem(SL* list,int i){
int j,k=MAXSIZE-1;
if(STATUS==0){
cout<<"链表不存在!无法操作!"<<endl;
return ERROR;
}
if(i<1 || i>ListLength(list)){
cout<<"位置错误!"<<endl;
return ERROR;
}
for(j=1;j<=i;j++){
k=list[k].cur;
}
return list[k].elem;
}
void menu(SL* list){
int i,n,e;
cout<<"********************************************"<<endl;
cout<<"************1、初始化静态链表***************"<<endl;
cout<<"************2、插入数据*********************"<<endl;
cout<<"************3、删除数据*********************"<<endl;
cout<<"************4、返回静态链表的长度***********"<<endl;
cout<<"************5、打印静态链表*****************"<<endl;
cout<<"************6、返回第i个元素的值************"<<endl;
cout<<"************7、退出*************************"<<endl;
cout<<"********************************************"<<endl;
cout<<"请选择:";
cin>>n;
switch(n){
case 1:
InitList_SL(list);
menu(list);
case 2:
cout<<"请输入数据:";
cin>>e;
cout<<"请输入插入位置:";
cin>>i;
ListInsert(list,i,e);
menu(list);
case 3:
cout<<"请输入删除位置:";
cin>>i;
DeleteList(list,i);
menu(list);
case 4:
cout<<"当前静态链表的长度为:"<<ListLength(list)<<endl;
menu(list);
case 5:
//Print_SL(list);
Print(list);//打印完整静态链表
menu(list);
case 6:
cout<<"请输入位置:";
cin>>i;
cout<<"第"<<i<<"个位置上的值为:"<<GetElem(list,i)<<endl;
menu(list);
case 7:
break;
}
}
void main(){
SL list[MAXSIZE];
menu(list);
/*
InitList_SL(list);
ListInsert(list,1,1);
ListInsert(list,2,2);
ListInsert(list,3,3);
ListInsert(list,4,4);
ListInsert(list,5,5);
ListInsert(list,6,6);
Print_SL(list);
DeleteList(list,3);
Print(list);
Print_SL(list);
*/
system("pause");
}