数据结构之单链表

/*
 *@date:08-07-06
 *@descript:单链表的实现与应用
 **/
public class LinList
{
	public  Node head;//头指针
	public  Node current;//当前节点位置
	private int size;//数据元素个数
	
	
	public LinList()//构造函数
	{
		head=current=new Node(null);
		size=0;
	}
	public Node getHead()
	{
		return head;
	}
	//定位函数
	public void index(int i) throws Exception
	{
		if(i<-1||i>size-1)
		{
			throw new Exception("参数错误!");
		}
		if(i==-1)
		{
			return;
		}

		current=head.next;
		int j=0;
		while((current!=null)&&j
 
 
  
  size)
		{
			throw new Exception("参数错误!");
		}

		index(i-1);
		//新节点的next域为current.next,而current的next域为新节点
		current.setNext(new Node(obj,current.next));
		size++;

	}
	//删除函数
	public Object delete(int i)throws Exception
	{
		if(size==0)
		{
			throw new Exception("链表元素已空无法删除");
		}
		if(i<0||i>size-1)
		{
			throw new Exception("参数错误!");
		}
		index(i-1);
		Object obj=current.next.getElement();
		//i-1个节点的next域指向i个节点的next域,让i节点脱链
		current.setNext(current.next.next);
		size--;
		return obj;
		
	}
	//获取数据元素
	public Object getData(int i)throws Exception
	{
		if(i<0||i>size)
		{
			throw new Exception("参数错误!");
		}
		index(i);
		return current.getElement();
	}
	//获取元素个数
	public int size()
	{
		return size;
	}
	//判断是否为空
	public boolean isEmpty()
	{
		return size==0;
	}

	//主函数
	public static void main(String[] args)
        {
    	LinList aLinList=new LinList();
    	LinList bLinList=new LinList();
    	LinList cLinList=new LinList();
    	int n=10;
    	try {
	    		for (int i=0; i
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值