简体   繁体   English

Java Double LinkedList打印

[英]Java Double LinkedList print

I have posted this code several times I apologize if you keep looking at this question. 如果您继续关注这个问题,我已经多次张贴此代码,我深表歉意。 I've been working on this for a bit so any help would be helpful i've done as much as I can so far. 我已经做了一些工作,所以到目前为止,我所做的一切都将对您有所帮助。 But when my program prints the data it switches the 8 and the 7 around and I can't figure out why! 但是,当我的程序打印数据时,它会切换8和7,我不知道为什么! Here is all the code. 这是所有代码。 And I have not started making my remove method yet so disregard that functionality. 而且我还没有开始制作我的remove方法,所以请忽略该功能。

public class MyLinkedList<AnyType> implements Iterable<AnyType> {
    private int theSize;
    private Node<AnyType> beginMarker;
    private Node<AnyType> endMarker;

    public class Node<AnyType> {
        public Node(AnyType data, Node<AnyType> head, Node<AnyType> tail) {
            myData = data;
            myHead = head;
            myTail = tail;
        }

        public AnyType myData;
        public Node<AnyType> myHead;
        public Node<AnyType> myTail;

    }

    public MyLinkedList() {
        beginMarker = new Node(null, endMarker, null);
        endMarker = new Node(null, null, beginMarker);
        theSize = 0;
    }

    public void clear() {
        beginMarker.myHead = endMarker;
        endMarker.myTail = beginMarker;

    }

    public int size() {
        return theSize;
    }

    public boolean exist(AnyType newVal) {
        beginMarker.myHead.myData = newVal;

        if (newVal != null) {
            return true;
        }

        return false;
    }

    private void addBefore(Node<AnyType> previousNode, AnyType newNode) {
        Node<AnyType> new_node = new Node<>(newNode, previousNode.myTail, previousNode);

        new_node.myTail.myHead = new_node;
        previousNode.myTail = new_node;
        theSize++;
    }

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

    public boolean add(int index, AnyType newVal) {
        addBefore(getNode(index, 0, size()), newVal);

        return true;
    }

    private Node<AnyType> getNode(int index) {
        return getNode(index, 0, size() - 1);
    }

    public Node<AnyType> get(AnyType nodeData) {
        Node<AnyType> node = beginMarker;

        while (node != endMarker) {

            // Means node.data = nodeData
            if (node.myData.equals(nodeData)) {
                return node;
            }
        }
        return null;
    }

    // Added method
    private Node<AnyType> getNode(int index, int lower, int upper) {
        Node<AnyType> x;

        if (index < lower || index > upper)
            throw new IndexOutOfBoundsException();

        if (index < size() / 2) {
            x = beginMarker.myHead;
            for (int i = 0; i < index; i++)
                x = x.myHead;
        } else {
            x = endMarker.myTail;
            for (int i = size(); i > index; i--) {
                x = x.myTail;

            }
        }
        return x;
    }

    public void printList() {
        Node temp = beginMarker.myHead;

        while (temp != null) {
            System.out.println(temp.myData);
            temp = temp.myHead;
        }
    }

    public java.util.Iterator<AnyType> iterator() {
        return new LinkedListIterator();
    }

    public void remove(AnyType removeVal) {
        /*
         * if(node.myData.equals(nodeData))
         * 
         * MyLinkedList testList = new MyLinkedList();
         * 
         * Node temp = testList.beginMarker.myData; while(temp != null){
         * 
         * if(temp == removeVal){ temp.myTail = temp.myHead; temp.myHead =
         * temp.myTail; } else{ temp.myHead = temp; }
         * 
         * 
         * }
         */
    }

    private class LinkedListIterator implements java.util.Iterator<AnyType> {

        private Node<AnyType> node_ = beginMarker;

        public void remove() {

        }

        public boolean hasNext() {
            if (node_.myHead != null) {
                return true;
            }
            return false;
        }

        public AnyType next() {
            if (!hasNext()) {
                return null;
            }
            node_ = node_.myHead;
            return node_.myData;
        }
    }

    private static void testListIntegers() {
        MyLinkedList<Integer> testList = new MyLinkedList<Integer>();
        testList.add(new Integer(5));
        testList.add(new Integer(4));
        testList.add(new Integer(3));
        System.out.println(" We have so far inserted " + testList.size() + " elements in the list");
        testList.remove(4);
        System.out.println(" Now, there is only " + testList.size() + " elements left in the list");
        testList.add(1, new Integer(7));
        testList.add(2, new Integer(8));
        System.out.println(" About to print content of the list");
        testList.printList();
    }

    private static void testListStrings() {
        MyLinkedList<String> testList = new MyLinkedList<String>();
        testList.add(new String("hello"));
        testList.add(new String("this is"));
        testList.add(new String("cs3345 project 2"));
        System.out.println(" We have so far inserted " + testList.size() + " elements in the list");
        testList.remove("this is");
        System.out.println(" Now, there is only " + testList.size() + " elements left in the list");
        testList.add(1, "a modified version of");
        testList.add(2, "cs3345 project 2, call it version 2");
        System.out.println(" About to print content of the list");
        testList.printList();
    }

    public static void main(String args[]) throws Exception {
        // Add whatever code you need here
        // However, you will need to call both testListIntegers()
        // and testListStrings()
        testListIntegers();
        testListStrings();

    }
}

The output is as follows: 输出如下:

 We have so far inserted 3 elements in the list
 Now, there is only 3 elements left in the list
 About to print content of the list
8
7
3
4
5
 We have so far inserted 3 elements in the list
 Now, there is only 3 elements left in the list
 About to print content of the list
cs3345 project 2, call it version 2
a modified version of
cs3345 project 2
this is
hello

Problem is with addBefore method: 问题在于addBefore方法:

private void addBefore(Node<AnyType> previousNode, AnyType newNode) {
    Node<AnyType> new_node = new Node<>(newNode, previousNode.myTail, previousNode);

    new_node.myTail.myHead = new_node;
    previousNode.myTail = new_node;
    theSize++;
}

It is the same like: 就像这样:

private void addBefore(Node<AnyType> previousNode, AnyType newNode) {
    Node<AnyType> new_node = new Node<>(newNode, previousNode.myTail, previousNode);

    previousNode.myHead = new_node;
    previousNode.myTail = new_node;
    theSize++;
}

So basically you are breaking doubly linked list. 因此,基本上,您正在打破双向链表。

Edit: 编辑:

According to algorithm described here this method should look like: 根据此处描述的算法此方法应类似于:

private void addBefore(Node<AnyType> previousNode, AnyType newNode) {
    Node<AnyType> new_node = new Node<>(newNode, previousNode, previousNode.myTail);
    if(previousNode.myTail==null){
        beginMarker.myHead = new_node;
    } else {
        previousNode.myTail.myHead = new_node;
    }
    previousNode.myTail = new_node;
    theSize++;
}

See that your list is indexed from 0 so the output should be: 看到您的列表从0开始索引,因此输出应为:

5
7
8
4
3

And next problem is in printList method (infinite loop) because somewhere is wrong usage of your begin/end marker. 下一个问题是printList方法(无限循环),因为某处错误地使用了开始/结束标记。

adding theSize--; 增加Size--; at the end of the remove method should decrease the elements from 3 to 1 在remove方法的最后,应将元素从3减少到1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM