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