hello all ,I have writen a program of LinList with First Node,but i don't kown to write the LinList without the First Node.Who can help me amend from the following program.Thanks ahead!!!

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

Recommended Answers

All 2 Replies

i'am very sorry.
“参数错误”means"the parameter is wrong"
"链表已空无元素可删"means"the LinList is empty.there is no element to be deleted"

It's not pretty clear what you mean by the `First Node' since there is nothing of that sort in your program. What are the specifications of the LinkedList you are trying to create here? Without having an inking of how things ought to be, helping you out would be difficult.

By the way, take a look at the source code of LinkedList which comes along with your JDK; it might just help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.