java数据结构 三(静态链表-游标实现)

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();
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值