JAVA通过继承线性表来实现有序表

1,对于线性表而言,里面的元素是无序的,可以随意地将新元素增加到线性表中而不需要考虑该元素在线性表中的位置。但是,对于有序表而言,其中的元素是按照某种方式进行排序的,因此在有序表中插入元素时,需要按照顺序将该新元素放置到有序表的合适的位置。

但由于有序表与线性表有很多相似的地方,因此,下面通过继承线性表来实现有序表。线性表的实现参考:http://www.cnblogs.com/hapjin/p/4549492.html

 

2,在Node内部类的实现中,定义了获取Node类的属性的get方法和set方法,这些方法为protected类型的,意味着Node类所在的外部类的子类可以通过这些方法来访问Node类里面的数据域。同样地,在Node类所在的外部类中(LinkedChainBase)也定义了protected类型的方法addFirstNode、getFirstNode、removeFirstNode……这样使得LinkedChainBase的子类可以直接访问其数据域,提高操作的效率。

 

3,有序表的实现类SortedLinkList.java通过继承基类LinkedChainBase来达到直接访问线性中的数据域的目的。由于有序表终究不是线性表,因此从线性表中继承而来的某些方法(如:replace方法)会破坏有序的有序性质。因此,对于这些方法可能通过抛出异常的方式来处理。

ListInterface代码参考:http://www.cnblogs.com/hapjin/p/4549492.html

具体的实现代码如下:

基类LinkedChainBase.java

public class LinkedChainBase<T> implements java.io.Serializable {
    private Node firstNode;
    private int length;
    
    public LinkedChainBase(){
        clear();
    }
    public final void clear() {//clear()在构造器中被调用了,所以此外用final修饰符
        firstNode = null;
        length = 0;
    }
    
    /*
     * 提供操作数据域firstNode的protected方法,使得子类可以直接操作表的数据域
     */
    //在表的表头插入元素
    protected void addFirstNode(Node newNode){
        assert newNode != null:"null argument in addFirstNode";
        newNode.setNextNode(firstNode);
        firstNode = newNode;
        length++;
    }
    
    protected Node getFirstNode(){//获取表的第一个结点
        return firstNode;
    }
    //获取表中某个位置处的结点
    protected Node getNodeAt(int givenPosition){
        assert (!isEmpty() && ((1 <= givenPosition) && (givenPosition <= length)));
        Node currentNode = firstNode;
        for(int counter = 1; counter < givenPosition; counter++){
            currentNode = currentNode.next;
        }
        assert currentNode != null;
        return currentNode;
    }
    //在表中某个结点后添加新结点
    protected void addAfterNode(Node nodeBefore, Node newNode){
        assert newNode != null:"illegal to add a null node";
        newNode.setNextNode(nodeBefore.getNextNode());
        nodeBefore.setNextNode(newNode);
    }
    //删除表中的第一个结点
    protected T removeFirstNode(){
        T result = null;
        if(firstNode != null){
            result = firstNode.data;
            firstNode = firstNode.getNextNode();
        }
        return result;
    }
    //删除表中的指定结点后的其他结点
//    protected T removeAfterNode(Node nodeBefore){
//        
//    }
    public boolean isEmpty() {//判断链表是否为空
        boolean result;
        if(length == 0){
            assert firstNode == null;
            result = true;
        }
        else{
            assert firstNode != null;
            result = false;
        }
        return result;
    }
    
    public int getLength() {//获取表的长度
        return length;
    }

    public void display() {//遍历表,显示表中的每个结点的值
        Node currentNode = firstNode;
        while(currentNode != null){
            System.out.println(currentNode.data);
            currentNode = currentNode.next;
        }
    }
    
    public T getEntry(int givenPosition) {//获取指定位置的结点的值
        T result = null;
        if((!isEmpty()) && ((givenPosition >= 1) && (givenPosition <= getLength()))){
            result = getNodeAt(givenPosition).getData();
        }
        return result;
    }
    
    public boolean contains(T anEntry) {//判断链表中的结点是否包含某个值
        boolean found = false;
        Node currentNode = getFirstNode();
        while(!found && currentNode != null){
            if(currentNode.getData().equals(anEntry)){
                found = true;
                break;
            }
            currentNode = currentNode.getNextNode();
        }
        return found;
    }
    
    /*
     * Node 是一个内部类,通用类型T与类中声明的通用类型相同,因此Node后面不需要<T>
     * Node 声明为protected,这样子类就可以访问Node类中的方法,从而直接地操作线性表的数据域,提高操作效率
     */
        protected class Node{
            private T data;
            private Node next;
            
            protected Node(T dataPortion){
                data = dataPortion;
                next = null;
            }
            private Node(T dataPortion, Node nextNode){
                data = dataPortion;
                next = nextNode;
            }
            protected T getData(){
                return data;
            }
            protected void setData(T dataPortion){
                data = dataPortion;
            }
            protected Node getNextNode(){
                return next;
            }
            protected void setNextNode(Node nextNode){
                next = nextNode;
            }
        }
}

 

有序表的实现类SortedLinkList.java

import linklist.ListInterface;
public class SortedLinkList<T extends Comparable<? super T>> extends LinkedChainBase<T> 
implements ListInterface<T>,java.io.Serializable {

    //有序表中的元素之所以是有序的,主要是通过该add方法来保证的
    @Override
    public boolean add(T newEntry) {
        Node newNode = new Node(newEntry);
        Node nodeBefore = getNodeBefore(newEntry);
        if(nodeBefore == null)
            addFirstNode(newNode);
        else
            addAfterNode(nodeBefore, newNode);
        return true;
    }

    
    private Node getNodeBefore(T anEntry){
        Node currentNode = getFirstNode();
        Node nodeBefore = null;
        //对待插入的元素与有序表中的元素进行比较,保证元素有序
        while((currentNode != null) && (anEntry.compareTo(currentNode.getData()) > 0)){
            nodeBefore = currentNode;
            currentNode = currentNode.getNextNode();
        }
        return nodeBefore;
    }
    
    @Override
    public boolean add(int givenPosition, T newEntry) {
        throw new UnsupportedOperationException("illegal to add element at a specified position.");
    }

    @Override
    public T remove(int givenPosition) {
        throw new UnsupportedOperationException("illegal to add element at a specified position.");

    }

    @Override
    public boolean replace(int givenPosition, T newEntry) {
        throw new UnsupportedOperationException("illegal to add element at a specified position.");
    }
}

 

参考《数据结构与算法分析 第二版 JAVA语言描述》Frank M. Carrano 著

posted @ 2015-05-20 20:49  大熊猫同学  阅读(685)  评论(0编辑  收藏  举报