linlist.java

来自「关于数据结构的JAVA源代码」· Java 代码 · 共 62 行

JAVA
62
字号
public class LinList implements List {
	Node head;
	Node current;
	int size;
	
	LinList(){
		head = current = new Node(null);
	}
	
	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 < i){
			current=current.next;
			j ++;
		}
	}

	public void insert(int i,Object obj) throws Exception{
		if(i < 0 || i > size){
			throw new Exception("参数错误!");
		}
		index(i - 1);
		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();
		current.setNext(current.next.next);
		size --;
		return obj;
	}
	
	public int size(){
		return size;
	}
	
	public boolean isEmpty(){
		return size == 0;
	}
	
	public Object getData(int i) throws Exception{
		if(i < -1 || i > size - 1){
			throw new Exception("参数错误!");
		}
		index(i);
     	return current.getElement();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?