0

我一直在研究基于其索引将节点插入到链表中的项目,如果节点的索引为 == -1,则将其插入最后。

这是我在 LinkedLists 的第一次拍摄,所以我什至不确定我是否正确连接了这些东西,所以任何提示都会真正有帮助。

我已经能够按顺序将节点放入列表中,但是在第一个测试用例之后,我得到了一个空指针异常。

这是我的代码

--------------------LinkedListClass---------

   package edu.wmich.cs1120.GudahWaleed.lab7.impl;

import edu.wmich.cs1120.lab7.interfaces.ICharNode;
import edu.wmich.cs1120.lab7.interfaces.IListCharNodes;

public class ListCharNodes implements IListCharNodes {

    private int length;
    private ICharNode lastNode;
    private ICharNode firstNode;

    public ListCharNodes() {
        firstNode = new CharNode();
        lastNode = null;
    }

    public boolean isEmpty() {

        return firstNode == null;
    }

    public int size() {
        int count = 0;
        ICharNode p = firstNode;
        while (p != null) {
            count++;
            p = p.getNext();
        }
        return count;
    }

    public ICharNode get_Symmetric(ICharNode node) {

        ICharNode temp = firstNode.getNext();

        for (int i = length / 2; i < length; i++) {
            temp = temp.getNext();
        }

        return temp;

    }

    @Override
    public ICharNode getFirstNode(ICharNode node) {

        return firstNode;

    }

    @Override
    public int getLength() {

        return length;
    }

    public void insertCharNode(ICharNode node) {

        int index = node.getIndex();

        if (index == -1) {

            insertCharNodeAtEnd(node);

        }

        else if (firstNode.getIndex() == node.getIndex()) {
            node.setNext(firstNode.getNext());
            firstNode = node;
            return;
        }

        else if (index == 0) {
            node.setNext(firstNode);
            firstNode = node;

            if (lastNode == null)

                return;
        }

        if (index > 0) {

            ICharNode pred = firstNode;

            for (int k = 1; k <= index - 1; k++) {

                pred = pred.getNext();
            }

            pred.setNext(new CharNode(node.getContent(), node.getIndex(), pred
                    .getNext()));

            if (pred.getNext().getNext() == null)

                lastNode = pred.getNext();
        }
    }

    @Override
    public void insertCharNodeAtEnd(ICharNode node) {
        if (isEmpty()) {
            firstNode = node;
            lastNode = firstNode;

        }

        else {
            lastNode.setNext(node);
            lastNode = lastNode.getNext();
        }
    }

    @Override
    public boolean isPalindrome() {

        ICharNode temp = firstNode;

        int count = 0;

        while (count < (length / 2)) {

            ICharNode pal = get_Symmetric(temp);

            if (pal.getContent() != temp.getContent()) {
                return false;
            } else
                count++;
        }
        return true;

    }

    @Override
    public void printMessage() {
        ICharNode toPrint = firstNode;
        while (toPrint != null) {

            System.out.print(toPrint.getContent());
            // System.out.print(toPrint.getIndex());
            // System.out.print(length);
            toPrint = toPrint.getNext();

        }
    }

    @Override
    public void setLength(int l) {
        length = length + l;

    }

}

---------------节点类--------

  package edu.wmich.cs1120.GudahWaleed.lab7.impl;

import edu.wmich.cs1120.lab7.interfaces.ICharNode;

public class CharNode implements ICharNode {

    private int index;
    private char content;
    private ICharNode next;
    private ICharNode last;

        public CharNode(char c, int i) {
            setContent(c);
            setIndex(i);
        }
        public CharNode(char c, int i, ICharNode next){
        setContent(c);
        setIndex(i);
        setNext(next);
        }

        public CharNode(){
            next = null;

        }

        @Override
        public void setContent(char c) {

        content = c;

        }

        @Override
        public char getContent() {

            return content;
        }

        @Override
        public void setIndex(int i) {

            index = i;

        }

        @Override
        public int getIndex() {

            return index;
        }

        @Override
        public void setNext(ICharNode next) {
             this.next=next;

        }

        @Override
        public ICharNode getNext() {

            return next;
        }



    }

----------------------测试类------------- ----

    package edu.wmich.cs1120.GudahWaleed.lab7.impl;

import java.io.FileNotFoundException;
import java.io.IOException;

import edu.wmich.cs1120.lab7.interfaces.ICharNode;
//import edu.wmich.cs1120.lab7.interfaces.IGlyphMessage;
import edu.wmich.cs1120.lab7.interfaces.IListCharNodes;
//import edu.wmich.cs1120.lab7.interfaces.IStoneAnalysis;

/**
 * @author sww
 *
 */
@SuppressWarnings("unused")
public class Test {
    /**
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        /*IStoneAnalysis stone = new StoneAnalysis();
        IGlyphMessage glpymsg = new GlyphMessage();
        IListCharNodes list = new ListCharNodes();

        stone.setGmsg(glpymsg);
        glpymsg.setListCharNodes(list);
        */

        IListCharNodes list = new ListCharNodes();
        ICharNode n1 = new CharNode('a',1);
        ICharNode n2 = new CharNode('b',0);
        ICharNode n3 = new CharNode('a',2);
        ICharNode n4 = new CharNode('b',-1); 

        list.insertCharNode(n1);
        list.insertCharNode(n4);
        list.insertCharNode(n3);
        list.insertCharNode(n2);
        System.out.print("Message 1 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        list.insertCharNode(n4);
        list.insertCharNode(n1);
        list.insertCharNode(n3);
        list.insertCharNode(n2);
        System.out.print("Message 2 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        list.insertCharNode(n4);
        list.insertCharNode(n3);
        list.insertCharNode(n1);
        list.insertCharNode(n2);
        System.out.print("Message 2.1 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        list.insertCharNode(n4);
        list.insertCharNode(n2);
        list.insertCharNode(n1);
        list.insertCharNode(n3);
        System.out.print("Message 2.2 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        list.insertCharNode(n4);
        list.insertCharNode(n3);
        list.insertCharNode(n2);
        list.insertCharNode(n1);
        System.out.print("Message 2.3 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        list.insertCharNode(n1);
        list.insertCharNode(n2);
        list.insertCharNode(n4);
        list.insertCharNode(n3);
        System.out.print("Message 3 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        list.insertCharNode(n3);
        list.insertCharNode(n1);
        list.insertCharNode(n4);
        list.insertCharNode(n2);
        System.out.print("Message 4 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        list.insertCharNode(n3);
        list.insertCharNode(n1);
        list.insertCharNode(n2);
        list.insertCharNode(n4);
        System.out.print("Message 5 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        CharNode n5 = new CharNode('c',2);
        CharNode n6 = new CharNode('a',3);

        list.insertCharNode(n1);
        list.insertCharNode(n4);
        list.insertCharNode(n6);
        list.insertCharNode(n2);
        list.insertCharNode(n5);
        System.out.print("Message 6 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        n5 = new CharNode('c',2);
        n6 = new CharNode('b',3);

        list.insertCharNode(n1);
        list.insertCharNode(n4);
        list.insertCharNode(n6);
        list.insertCharNode(n2);
        list.insertCharNode(n5);
        System.out.print("Message 7 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

    }

}
4

1 回答 1

0
        IListCharNodes list = new ListCharNodes();
        ICharNode n1 = new CharNode('a',1);
        ICharNode n2 = new CharNode('b',0);
        ICharNode n3 = new CharNode('a',2);
        ICharNode n4 = new CharNode('b',-1); 

        list.insertCharNode(n1);
        list.insertCharNode(n4);
        list.insertCharNode(n3);
        list.insertCharNode(n2);
        System.out.print("Message 1 ");
        list.printMessage(); 
        System.out.println(" is interesting or not "+ list.isPalindrome());

        list = new ListCharNodes();
        list.insertCharNode(n4);   // Exception is thrown at this line

问题是当你这样做

list = new ListCharNodes();

第二次将 lastNode 字段设置为空。查看 ListCharNodes 类的构造函数。

因此,当为 n4 调用 insertCharNodeAtEnd 时,它会转到 else 部分(因为 isEmpty 产生错误),其中使用 null 对象调用 setNext 方法,即 lastNode

因此给出一个空指针异常。

于 2013-11-09T08:04:36.760 回答