如何删除尾部列表并打印列表?

时间:2016-10-13 16:22:31

标签: java

我一直在试验节点和单链表,我想删除尾部列表并将其打印出来。在我的主要部分,我试图这样做,但它给了我一个错误。

编辑: 输出并注释错误行:

=> 1484150620067

如果我取消注释该行,我会在eclipse中收到此错误。

Circle{radius=2} has a perimeter: 12.56
Circle{radius=1} has a perimeter: 6.28
Square{length=3} has perimeter: 12.0
Square{length=5} has perimeter: 20.0
Triangle{length=1 width=4 height=5} has perimeter: 10.0
Triangle{length=3 width=5 height=7} has perimeter: 15.0
Rectangle{length=1 width=2} has perimeter: 6.0
Rectangle{length=8 width=9} has perimeter: 34.0

主:

  Exception in thread "main" java.lang.NullPointerException
    at ShapeLinkedList.deleteAtIndex(ShapeLinkedList.java:108)
    at M.main(M.java:35)

ShapeLinkedList:

public class M {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int[] values = {2,1,3,5,1,4,5,3,5,7,1,2,8,9};
        ShapeLinkedList sll = new ShapeLinkedList();
        Circle c1 = new Circle(values[0]);
        Circle c2 = new Circle(values[1]);
        Square sq1 = new Square(values[2]);
        Square sq2 = new Square(values[3]);
        Triangle t1 = new Triangle(values[4],values[5],values[6]);
        Triangle t2 = new Triangle(values[7],values[8],values[9]);
        Rectangle r1 = new Rectangle(values[10],values[11]);
        Rectangle r2 = new Rectangle(values[12],values[13]);
        sll.insertAtBeginning(r1);
        sll.insertAtBeginning(r2);
        sll.insertAtBeginning(c1);
        sll.insertAtBeginning(c2);
        sll.insertAtEnd(sq1);
        sll.insertAtEnd(sq2);
        sll.insertAtEnd(t1);
        sll.insertAtEnd(t2);

        System.out.println(c1);
        System.out.println(c2);
        System.out.println(sq1);
        System.out.println(sq2);
        System.out.println(t1);
        System.out.println(t2);
        System.out.println(r1);
        System.out.println(r2);

       //ERROR STARTS HERE

        /*sll.deleteAtIndex(sll.length());



        System.out.println(c1);
        System.out.println(c2);
        System.out.println(sq1);
        System.out.println(sq2);
        System.out.println(t1);
        System.out.println(t2);
        System.out.println(r1);
        System.out.println(r2);

    */
    }

}

圆圈类:

public class ShapeLinkedList {

    public Node head;

    public ShapeLinkedList() {
        head = null;
    }



    public ShapeLinkedList(Node head) {
        this.head = head;
    }
    public boolean isEmpty() {
        return length() == 0;
    }
    public void insertAtEnd(Shape data) {
// TODO to be implemented
        Node n = new Node(data,null);
        if (head == null)
            head = n;
        else
        {

            tail().setNext(n);
        }

    }
    public void insertAtBeginning(Shape data) {
// TODO to be implemented
        Node n = new Node(data, head);
        head = n;
    }
    public Node tail() {
// TODO to be implemented
// returns the last node
        Node temp = head;
        if(temp != null)
        {
           while(temp.getNext() != null)
           {
               temp = temp.getNext();
           }
        }
        return temp;
    }
    public int length() {
// TODO to be implemented
        Node temp = head;

        if(temp == null)
            return 0;
        else
        {
            int i = 1;
            while(temp.getNext() != null)
            {
                temp = temp.getNext();
                i++;

            }
            return i;
        }
    }
    void insertAtIndex(int idx, Shape data) {
// TODO to be implemented
        if(length() >= idx)
        {
            Node n = new Node(data, null);

            Node temp = findAtIndex(idx);

            n.setNext(temp.getNext());
            temp.setNext(n);
        }

    }
    Node findAtIndex(int idx) {
// TODO to be implemented

        if(length() >= idx)
        {
            Node temp = head;
            for(int i = 0; i < idx; i++)
            {
                temp = temp.getNext();
            }
            return temp;
        }
        else
            return null;

    }
    void deleteAtIndex(int idx) {
// TODO to be implemented

        if(length() >= idx )
        {

            if(idx == 0)
                head = head.getNext();
            else
            {
                Node temp = findAtIndex(idx-1);
                temp.setNext(temp.getNext().getNext());
            }
        }


    }
    @Override
    public String toString() {
        return "";

    }
    void deleteData(Shape s) {
// TODO to be implemented
        Node temp = head;
        for(int i = 0; i < length(); i++)
        {
            if(temp.getData() == s)
                deleteAtIndex(i);
        }

    }
    @Override
    public int hashCode() {
// TODO to be implemented
        return 0;
    }
    @Override
    public boolean equals(Object obj) {
// TODO to be implemented

        return false;
    }
    // Node is nested class because it only exists along with linked list
    public static class Node {
        private Shape data;
        private Node next;
// TODO develop all the methods that are needed
// such as constructors, setters, getters
// toString, equals, hashCode
        public Node(Shape S, Node N)
        {
            data = S;
            next = N;
        }


        public Node getNext()
        {
            return next;
        }

        public Shape getData()
        {
            return data;
        }

        public void setNext(Node N)
        {
            next = N;
        }
    }

}

我做错了什么原因?任何可能的解决方案来解决这个问题?

1 个答案:

答案 0 :(得分:0)

问题是,当你要求deleteAtIndex方法的length()时,你忘了考虑从0开始的索引。当你有一个10个值的列表时,你有索引0-9,但是你传递了deleteAtIndex(10),即总大小,而不是最后一个值的索引。

所以在你的deleteAtIndex方法中:

  else
        {
            Node temp = findAtIndex(idx-1);
            temp.setNext(temp.getNext().getNext());
        }

它查找最后一个索引或索引9.然后执行getNext()。getNext()。

这通常没问题,但是:

你的findAtIndex中的

        for(int i = 0; i < idx; i++)
        {
            temp = temp.getNext();
        }

如果你传入9,那么你将从头部移动9个节点。离开你的尾巴,但你想成为尾巴前的一个节点。