在java中打印double linkedList

时间:2016-02-13 15:21:58

标签: java linked-list doubly-linked-list

我坚持如何打印双链表。我必须打印'testListStrings()'和'testListIntegers()'

    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();
}

我看到可能使用toString方法,但我不确定这是否有用。我的代码已经有一个名为printList()的方法,但它没有任何参数(教授已经为我们编写了代码,我们必须填写代码)。 print方法在链表类中。

更新 -

所以现在在这里,我被困住了,

public void printList() {
    MyLinkedList testList = new MyLinkedList();

    Node temp = testList.beginMarker;

    while(temp != null){

        System.out.println(temp.myData);
        temp = temp.myHead;
    }


}

---我的代码返回null

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;



    }

    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;

        }




    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;
             for(int i = size(); i > index; i--)
                 x = x.myTail;
        }
    return x;
    }

0 个答案:

没有答案