自定异常类
Java代码
public class MyException extends Exception {
public MyException(){};
public MyException(String msg){
super(msg);
}
}
链表结点对像
Java代码
public class Node {
public Node previou=null;//前结点指针
public Node next=null; //后结点指针
public Object value;//结点值
Node(Object value){
thisvalue=value;
}
}
链表对像
Java代码
public class DoubleLinked {
private Node head;//链表头
DoubleLinked(){
}
/**
* 判断是否还有下一个结点没有则为链表的尾结点
* @param node
* @return
*/
public boolean hasNext(Node node){
if(nodenext==null)
return false;
return true;
}
/**
* 判断是否有上一个结点没有则为链表的头结点
* @param node
* @return
*/
public boolean hasPrev(Node node){
if(nodepreviou==null)
return false;
return true;
}
/**
* 获取链表头元素
* @return
* @throws MyException
*/
public Node getHead() throws MyException{
if(head==null){
throw new MyException(链表为空);
}
return head;
}
/**
* 获取上一个接点
* @param node
* @return
*/
public Node getPrev(Node node){
return nodepreviou;
}
/**
* 获取下一个结点
* @param node
* @return
*/
public Node getNext(Node node){
return nodenext;
}
/**
* 根据索引获取结点
* @param index结点索引
* @return
* @throws MyException
*/
public Node getNode(int index) throws MyException{
Node curNode=null;
Node next=null;
Node node=null;
if(head==null){
throw new MyException(链表为空);
}else{
curNode=head;
for(int i=;i<index;i++){
if(curNode==null){
throw new MyException(你要获取的元素索引大于链表长度);
}else{
node=curNode;
if(hasNext(curNode)){
next=curNodenext;
curNode=next;
}else{
curNode=null;
}
}
}
}
return node;
}
/**
* 获取最后一个结点
* @return
* @throws MyException
*/
public Node getLast() throws MyException{
Node curNode=null;
Node next=null;
Node last=null;
boolean flag=true;
if(head==null){
throw new MyException(链表为空);
}else{
curNode=head;
while(flag){
if(hasNext(curNode)){
next=curNodenext;
curNode=next;
}else{
last=curNode;
flag=false;
}
}
}
return last;
}
/**
* 在链表头添加新结点
* @param node
*/
public void addHead(Node node){
if(head==null){
head=node;
}else{
nodenext=head;
headpreviou=node;
head=node;
}
}
/**
* 在链表末尾处添加新结点
* @param node
* @throws MyException
*/
public void addLast(Node node) throws MyException{
if(head==null){
head=node;
}else{
Node last=thisgetLast();
lastnext=node;
nodepreviou=last;
}
}
/**
* 在链表中间插入新结点
* @param node
* @throws MyException
*/
public void insertNode(int indexNode node) throws MyException{
Node indexNode=thisgetNode(index);
Node prev=indexNodepreviou;
prevnext=node;
nodepreviou=prev;
nodenext=indexNode;
indexNodepreviou=node;
}
/**
* 删除链表头结点
* @return
* @throws MyException
*/
public Node deleteHead() throws MyException{
Node head=thisgetHead();
if(hasNext(head)){
Node next=headnext;
thishead=next;
nextpreviou=null;
}
return head;
}
/**
* 删除链表的最后一个结点
* @return
* @throws MyException
*/
public Node deleteLast() throws MyException{
Node last=thisgetLast();
Node prev=lastpreviou;
if(prev==null){
thishead=null;
}else{
prevnext=null;
}
return last;
}
/**
* 根据索引删除链表结点
* @param index
* @return
* @throws MyException
*/
public Node deleteNode(int index) throws MyException{
Node node=thisgetNode(index);
Node prev=nodepreviou;
Node next=nodenext;
if(prev==null && next!=null){
thishead=next;
nextpreviou=null;
}
if(prev!=null && next==null){
prevnext=null;
}
if(prev==null && next==null){
thishead=null;
}
if(prev!=null && next!=null){
prevnext=next;
nextpreviou=prev;
}
return node;
}
} :!: