在java中跳过列表删除和打印方法

时间:2016-11-30 05:25:25

标签: java linked-list skip-lists

我在java中使用跳过列表实现。我有插入和搜索方法有效。但我无法编写删除和打印方法。下面是代码:SkipList.java

    public class SkipList<T extends Comparable<T>> {

    private SkipNode<T> start;
    private SkipNode<T> end;
    private SkipNode<T> supportStart;
    private SkipNode<T> supportEnd;
    private int size;
    private int sizeSupport;

    public T getStart() {
        return start.getData();
    }

    public T getEnd() {
        return end.getData();
    }

    public int getSize() {
        return size;
    }

    public void add(T data) {
        if (start == null) {
            insertAsFirstElement(data);
        } else {
            insert(data);
        }
    }

    private void insertAsFirstElement(T data) {
        SkipNode<T> node = new SkipNode<>(data);
        start = node;
        end = node;
        size++;

        SkipNode<T> supportNode = new SkipNode<>(data);
        supportStart = supportNode;
        supportEnd = supportNode;
        supportNode.setDown(node);
        sizeSupport++;
    }

//Adding element in the end assuming user enters data in ascending order
    private void insert(T data) {
        SkipNode<T> node = new SkipNode<>(data);
        end.setNext(node);
        node.setPrevious(end);
        end = node;
        size++;

        int expectedSupportSize = (int) Math.sqrt(size);
        if (sizeSupport < expectedSupportSize) {
            SkipNode<T> supportNode = new SkipNode<>(data);
            supportEnd.setNext(supportNode);
            supportNode.setPrevious(supportEnd);
            supportEnd = supportNode;
            supportNode.setDown(node);
            sizeSupport++;

            if (sizeSupport > 2) {
                reAjustSupportList();
            }

        }
    }





    private void reAjustSupportList() {
        SkipNode<T> navigationNode = supportStart.getNext();
        int i = 1;

        while (navigationNode != supportEnd) {
            SkipNode<T> tempNode = navigationNode.getDown();
            for (int j = 1; j <= i; j++) {
                tempNode = tempNode.getNext();
            }
            navigationNode.setDown(tempNode);
            navigationNode.setData(tempNode.getData());
            navigationNode = navigationNode.getNext();

            i++;
        }
    }

    public boolean search(T data) {
    SkipNode<T> navigationNode = supportStart;
    int compare;

    while ((compare = data.compareTo(navigationNode.getData())) > 0
            && navigationNode.getNext() != null) {
        navigationNode = navigationNode.getNext();
    }

    if (compare == 0) {
        return true;
    }

    if (compare < 0) {
        navigationNode = navigationNode.getPrevious();
    }
    navigationNode = navigationNode.getDown().getNext();

    while ((compare = data.compareTo(navigationNode.getData())) > 0
            && navigationNode.getNext() != null) {
        navigationNode = navigationNode.getNext();
    }
    return compare == 0;
}



    private static class SkipNode<T> {

        public SkipNode(T data) {
            this.data = data;
        }

        private SkipNode<T> next = null;
        private SkipNode<T> previous = null;
        private SkipNode<T> down = null;
        private T data;

        public SkipNode<T> getNext() {
            return next;
        }

        public void setNext(SkipNode<T> next) {
            this.next = next;
        }

        public SkipNode<T> getPrevious() {
            return previous;
        }

        public void setPrevious(SkipNode<T> previous) {
            this.previous = previous;
        }

        public SkipNode<T> getDown() {
            return down;
        }

        public void setDown(SkipNode<T> down) {
            this.down = down;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }

    public static void main(String[] args) {

        SkipList<Integer> mylist = new SkipList<Integer>();

         mylist.add(12);
         mylist.add(23);
         mylist.add(30);
         mylist.add(20);
         mylist.add(45);
         mylist.add(34);

         System.out.println("Size of liked list equals:"+ mylist.size);
         System.out.println(mylist.search(12));  // true
         System.out.println(mylist.search(45));  //true
         System.out.println(mylist.search(40));  // false



    } 
}

我试图通过

打印获得的列表
for(int i=1;i<mylist.size;i++)
         {
             System.out.println(mylist[i]);
         }

但我知道,我犯了一些逻辑错误。如果可能的话,你能展示删除和打印方法吗?非常感谢您的帮助!

0 个答案:

没有答案