美文网首页
数据结构与算法分析(一)表的Java实现

数据结构与算法分析(一)表的Java实现

作者: lhsjohn | 来源:发表于2018-12-12 19:54 被阅读0次

两种表的java实现

1 MyLinkedList

public class MyLinkedList<AnyType> implements Iterable<AnyType> {

    /**
     * 节点类
     * 
     * @author lihuashuo
     *
     * @param <AnyType>
     */
    private static class Node<AnyType> {

        public AnyType data;
        public Node<AnyType> prev;
        public Node<AnyType> next;

        public Node(AnyType data, Node<AnyType> prev, Node<AnyType> next) {
            this.data = data;
            this.prev = prev;
            this.next = next;
        }
    }

    private int theSize;
    private int modCount = 0;
    private Node<AnyType> beginMarker;
    private Node<AnyType> endMarker;

    public MyLinkedList() {
        doClear();
    }

    public void clear() {
        doClear();
    }

    public int size() {
        return theSize;
    }

    public boolean isEmpty() {
        return theSize == 0;
    }

    public boolean add(AnyType x) {
        add(size(), x);
        return true;
    }

    public void add(int idx, AnyType x) {

    }

    public AnyType get(int idx) {
         return getNode(idx).data;
    }

    public AnyType set(int idx, AnyType newVal) {
        Node<AnyType> p = getNode(idx);
        AnyType oldVal = p.data;
        p.data = newVal;
        return oldVal;
    }

    public AnyType remove(int idx) {
        return remove(getNode(idx));
    }
    private void addBefore(Node<AnyType> p, AnyType x) {
        Node<AnyType> newNode = new Node<>(x, p.prev, p);
        newNode.prev.next = newNode;
        p.prev = newNode;
        theSize++;
        modCount++;
    }

    private Node<AnyType> getNode(int idx) {

        return getNode(idx, 0, size() - 1);
    }

    private Node<AnyType> getNode(int idx, int lower, int upper) {
        Node<AnyType> p;
        if (idx < lower || idx > upper) {
            throw new IndexOutOfBoundsException();
        }
        if (idx < size() / 2) {
            p = beginMarker.next;
            for (int i = 0; i < idx; i++) {
                p = p.next;
            }

        } else {
            p = endMarker;
            for (int i = size(); i > idx; i--) {
                p = p.prev;
            }
        }

        return p;

    }

    private void doClear() {
        beginMarker = new Node<AnyType>(null, null, null);
        endMarker = new Node<AnyType>(null, beginMarker, null);
        beginMarker.next = endMarker;
        theSize = 0;
        modCount++;
    }

    private AnyType remove(Node<AnyType> p) {
       p.next.prev = p.prev;
       p.prev.next = p.next;
       theSize--;
       modCount++;
       return p.data;
    }
    
    @Override
    public Iterator<AnyType> iterator() {
        // TODO Auto-generated method stub
        return new LinkedListIterator();
    }
    
    
    private class LinkedListIterator implements Iterator<AnyType>{
     
     private Node<AnyType> current = beginMarker.next;  
     private int expectedModCount = modCount;
     private boolean okToRemove = false;
     
        @Override
        public boolean hasNext() {
            // TODO Auto-generated method stub
            return current!=endMarker;
        }

        @Override
        public AnyType next() {
          if(modCount !=expectedModCount)
              throw new ConcurrentModificationException();
          if(!hasNext())
              throw new NoSuchElementException();
            
          AnyType nextItem = current.data;
          current = current.next;
          okToRemove = true;
          return nextItem;
          
        }
        
        
        public void remove() {
            if(modCount!=expectedModCount)
                throw new ConcurrentModificationException();
            if(!okToRemove)
                throw new IllegalStateException();
            
            MyLinkedList.this.remove(current.prev);
            expectedModCount++;
            okToRemove = false;
            
            
        }
        
        
    }
    
    

}

2. MyArrayList


public class MyArrayList<AnyType> implements Iterable<AnyType> {

    private static final int DEFAULT_CAPACITY = 10;

    private int theSize;
    private AnyType[] theItems;

    public MyArrayList() {
        doClear();
    }

    private int size() {
        // TODO Auto-generated method stub
        return theSize;
    }

    private boolean isEmpty() {
        return size() == 0;
    }

    private void trimToSize() {
        ensureCapacity(size());
    }

    private void doClear() {
        theSize = 0;
        ensureCapacity(DEFAULT_CAPACITY);
    }

    private void ensureCapacity(int newCapacity) {
        if (newCapacity < theSize)
            return;

        AnyType[] old = (AnyType[]) new Object[newCapacity];
        for (int i = 0; i < size(); i++) {
            theItems[i] = old[i];
        }

    }

    public AnyType get(int idx) {
        if (idx < 0 || idx >= size())
            throw new ArrayIndexOutOfBoundsException();
        return theItems[idx];
    }

    public AnyType set(int idx, AnyType newVal) {
        if (idx < 0 || idx >= size())
            throw new ArrayIndexOutOfBoundsException();
        AnyType old = theItems[idx];
        theItems[idx] = newVal;
        return old;

    }

    public boolean add(AnyType x) {
        add(size(), x);
        return true;
    }

    public void add(int idx, AnyType x) {
        if (theItems.length == size())
            ensureCapacity(size() * 2 + 1);

        for (int i = theSize; i > idx; i--) {
            theItems[i] = theItems[i - 1];
        }
        theItems[idx] = x;
        theSize++;
    }

    public AnyType remove(int idx) {
        AnyType removedItem = theItems[idx];
        for (int i = idx; i < size() - 1; i++) {
            theItems[i] = theItems[i + 1];
        }
        theSize--;
        return removedItem;
    }

    
    public Iterator<AnyType> iterator(){
        return new ArrayListIterator();
    }
    

    private  class ArrayListIterator<AnyType> implements Iterator<AnyType> {
        private int current = 0;
        
        @Override
        public boolean hasNext() {
            // TODO Auto-generated method stub
            return current < size();
        }

        @Override
        public AnyType next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            // TODO Auto-generated method stub
            return (AnyType) theItems[current++];
        }
        
        public void remove() {
            MyArrayList.this.remove(--current);
        }
        


    }
    

}

作者: lhsjohn 转载请注明出处

相关文章

网友评论

      本文标题:数据结构与算法分析(一)表的Java实现

      本文链接:https://www.haomeiwen.com/subject/ainphqtx.html