根据其在LinkedList中的索引插入节点

时间:2013-11-09 06:00:17

标签: java

我一直致力于项目将基于其索引的节点插入到链表中,如果节点的索引是== -1,则将其插入到末尾。

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

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

这是我的代码

------------------- 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;

    }

}

--------------- Node Class --------------------

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

    }

}

1 个答案:

答案 0 :(得分: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字段设置为null。查看ListCharNodes类的构造函数。

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

因此给出空指针异常。