JAVA的链表实现 (二)

2014-11-24 07:45:48 · 作者: · 浏览: 2
el){
if(!isEmpty()){
tail.next = new IntSLLNode(el);
tail = tail.next;
}
else{
head = tail = new IntSLLNode(el);
}
}

public int deleteFormHead(){
int el = head.info;
if(head == tail){
head = tail = null;
}else{
head = head.next;
}
return el;
}

public int deleteFormTail(){
int el = tail.info;
if(head == tail){
head = tail = null;
}else{
IntSLLNode tmp;
for(tmp = head; tmp.next != tail; tmp = tmp.next);
tail = tmp;
tail.next = null;
}
return el;
}

public void printALL(){
for(IntSLLNode tmp = head; tmp != null; tmp = tmp.next)
System.out.println(tmp.info + "");
}


public boolean isIntList(int el){
IntSLLNode tmp;
for(tmp = head; tmp != null && tmp.info != el; tmp = tmp.next);
return tmp != null;
}


public void delete(int el){
if(!isEmpty()){
if(head == tail && el == head.info){
head = tail = null;
}
else if(el == head.info)
head = head.next;
else{
IntSLLNode pred, tmp;
for(pred = head, tmp = head.next; tmp != null && tmp.info != el; pred = pred.next, tmp = tmp.next);
if(tmp != null){
pred.next = tmp.next;
if(tmp == tail){
tail = pred;
}
}
}
}
}


public static void main(String [] args){
IntSLLList list = new IntSLLList();
System.out.println(list.isEmpty());
list.addToHead(6);
list.addToTail(14);
list.printALL();
list.deleteFormHead();
list.isIntList(6);
list.addToHead(15);
list.printALL();
list.delete(15);
list.printALL();
}
}

实现了最基本的功能,没有写注释,主要是希望自己好好看看,呵呵



摘自 技术是我的毕生追求