单链表(java语言实现)

Written by Bruth_Lee in Southwest University of Science And Technology.

I figure out what is the most difficult to understand, and I hope you can understand it.

Insert:


Delete:


LinkList class:

package Second_chapter2;

import Second_chapter1.List;

public class LinkList implements List{
	Node head;
	int size;
	public LinkList() {head = new Node();size=0;}
	public LinkList(Object[] a) {
		Node p;
		int i,n=a.length;
		head = new Node();
		for(i=n-1; i>=0; i--) {
			p = new Node(a[i], head.next);
			head.setnext(p);
		};
		size = n;
	};
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	public Node index(int i) { 
		Node p;
		int j;
		if(i<0 || i>size) {
			p=null;
		} 
		else if(i==0) p = head;
		else {
			p = head.next;
			j=1;
			while(i!=j){//???
				p = p.next;
				j++;
		}	
	  }
	return p;
}
	
	@Override
	public void clear() {
		// TODO Auto-generated method stub
	head = new Node();
	size=0;
	}

	@Override
	public Object gete(int i) {
		// TODO Auto-generated method stub
		if(i<1 || i>size)  return null;
		Node p=index(i);
		return p.getdata();
	}

	@Override
	public int leng() {
		// TODO Auto-generated method stub
		return size;
	}

	@Override
	public int loct(Object el) {
		// TODO Auto-generated method stub
		Node p;
		int j;
		p = head.next;
		j=1;
//		while(p!=null) {
//			if(p.data.equals(el)) break;
//			p=p.next;
//			j++;
//		}
		while(p!=null && !p.data.equals(el) ) {
			p=p.next;j++;
		}
		if(p!=null) return j;
		else return -1;
	};
	

	@Override
	public boolean inst(int loc, Object el) {
		// TODO Auto-generated method stub
		if(loc<1 || loc>size+1) return false;
		Node p = index(loc-1);
		p.setnext(new Node(el,p.next));
		size++;
		return true;
	}

	@Override
	public Object dele(int loct) {
		// TODO Auto-generated method stub
		if(size==0 || loct<1 || loct>size) return null;
		Node p = index(loct-1);
		Object el = p.next.data;
		p.setnext(p.next.next);
		size--;
		return el;
	}

	@Override
	public boolean full() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean empt() {
		// TODO Auto-generated method stub
		return head.next == null;
	}

}

Node class:

package Second_chapter2;

public class Node {
	Object data;
	Node next;
	public Node() {
		this(null,null);
	}
	public Node(Object d, Node n) {
		data = d;
		next = n;
	}
	
	public Object getdata() {return data;}
	public Node getnext() {return next;}
	public void setdata(Object el) {data = el;}
	public void setnext(Node p) {next = p;}
};

List interface:

package Second_chapter1;


public interface List {
public void clear();//Initialization
public Object gete(int i);//Return the i element
public int leng();//Seek length
public int loct(Object el);//Find, If el has been found, return the sequence of element,else return null;
public boolean inst(int loc, Object el);//Insert el into loc;
public Object dele(int loct);//Delete the element in the loc position;
public boolean full();//Judge whether the sequence_list is full;
public boolean empt();//Judge whether the sequence_list is empt;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖无为

感谢你们的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值