package com;
/** 静态链表-游标实现
* 数组操作代替指针
* 让每个元素知道下个元素位置(内存地址)
*/
public class LinearTable {
static int MAXSIZE=1000;
table[] tableList=new table[MAXSIZE];
class table{
Object date;
int cur;
}
void initList(LinearTable space){
for(int i=0;i<MAXSIZE;i++){
tableList[i]=new table();
tableList[i].cur=i;
// space[i].cur=i+1;
}
tableList[MAXSIZE-1].cur=0;
}
int macllo_sll(){
int i=tableList[0].cur; //获取数组第一个元素cur
if(tableList[0].cur>0){
tableList[0].cur=tableList[i].cur;
}
return i;
}
void listInsert(int i,Object e){
int j,k,l;
k=1;//最后一个元素下标
//判读越界
j=macllo_sll();
if(j>0){
tableList[j+1].date=e;
tableList[j+1].cur=0;
tableList[j].cur=j+1;
tableList[0].cur=j+1;
// L[j].cur=
}else{
tableList[k].date=e;
tableList[k].cur=0;
tableList[0].cur=k;
}
}
public void free_ssl(int index){
tableList[index].date=null;
}
public void read(){
for(int i=0;i<MAXSIZE;i++){
System.out.print("cur="+tableList[i].cur);
System.out.println(" date="+tableList[i].date);
if(tableList[i].cur==0)
break;
}
}
public static void main(String[] args) {
LinearTable linearTable=new LinearTable();
linearTable.initList(linearTable);
linearTable.listInsert(1, "1");
linearTable.listInsert(2, "2");
linearTable.listInsert(3, "3");
linearTable.listInsert(4, "4");
linearTable.read();
linearTable.free_ssl(2);
linearTable.listInsert(5,"5");
System.out.println("------");
linearTable.read();
}
}
java数据结构 三(静态链表-游标实现)
最新推荐文章于 2023-03-14 12:45:45 发布