Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的双链表_Java_Eclipse - Fatal编程技术网

java中的双链表

java中的双链表,java,eclipse,Java,Eclipse,我的方法有问题。没有一个输出是正确的,我不确定我是否正确使用了双链接列表。有人能纠正或解释我做错了什么吗 public class SlideList { private SlideNode head; private SlideNode tail; private SlideNode cursor; public SlideList() { head = null; tail = null; curs

我的方法有问题。没有一个输出是正确的,我不确定我是否正确使用了双链接列表。有人能纠正或解释我做错了什么吗

public class SlideList 
{

    private SlideNode head;
    private SlideNode tail;
    private SlideNode cursor;

    public SlideList()
    {
        head = null;
        tail = null;
        cursor = null;
    }
    public void editCurrentSlide(String text, int lineNum) throws IllegalArgumentException 
    {
        if(cursor==null||lineNum>5||lineNum<0)
            throw new IllegalArgumentException("Slide does not exist");
        cursor.getSlide().setData(text, lineNum);
    }
    public boolean jumpToPosition(int position)
    {
        if(position<0 || position>listLength())
            return false;
        resetCursor();
        for(int x=1;x<=position;x++)
        {
            cursor = cursor.getNext();
        }
        return true;
    }
    public void displayCurrentSlide() throws EmptyListException
    {
        if(cursor==null)
            throw new EmptyListException("There is no list");
        displaySlide(currentSlide());
    }
    public void displaySlides(int start, int end) throws IllegalArgumentException
    {
        int length = listLength();
        resetCursor();
        if(start<1)
            start=1;
        if(end>length)
            end = length;
        if(start>length||length==0)
            throw new IllegalArgumentException("There is no list");
        resetCursor();

        for(int i = 1; i < start; i++)//we can choose where to start the list with i
        {
            System.out.println("Inside loop1");
            cursor=cursor.getNext();

        }
        System.out.println("start: " + start + " end: " + end);
        for(int x = start; x<=end; x++)
        {

            System.out.println("Inside loop2");
            displaySlide(x);
            if(start<end)
                cursor = cursor.getNext();

        }
        //cursor = cursor.getPrev();


    }
    public void displaySlide(int x)
    {
        System.out.print("******************************");
        System.out.print(" " + x + " ");
        System.out.print("******************************\n\n");
        System.out.print(cursor.getSlide().toString());
        System.out.print("\n******************************");
        System.out.print(" " + x + " ");
        System.out.print("******************************\n");
    }
    public int currentSlide()
    {
        SlideNode temp = new SlideNode();
        temp = head;
        int position=1;
        while(temp!=cursor)
        {
            temp = temp.getNext();
            position++;
        }
        return position;
    }
    public int listLength()
    {
        System.out.println("In Length of List metho");
        SlideNode nodePtr = head;
        int answer = 1;
        while(nodePtr != tail)
        {
            System.out.println("In Length of List loop");
            answer++;
            nodePtr = nodePtr.getNext();
        }
        return answer;
    }
    public boolean removeCurrentSlide() 
    {
        if(cursor!=null)
        {
            cursor = cursor.getPrev();
            cursor.getNext().getNext().setPrev(cursor);
            cursor.setNext(cursor.getNext().getNext());
            cursor = cursor.getNext();
            return true;
        }
        return false;
    }
    public void addAfterCurrent(Slide newSlide)
    {
        SlideNode node = new SlideNode(newSlide);
        if(cursor==null)
        {
            addToEnd(newSlide);
        }
        else
        {
            node.setNext(cursor.getNext());
            node.setPrev(cursor);
            node.getNext().setPrev(node);
            cursor.setNext(node);

            cursor = cursor.getNext();

            if(cursor.getNext() == null)
            {
                tail = cursor;
            }
        }
    }
    public void addToEnd(Slide newSlide)
    {
        SlideNode node = new SlideNode(newSlide);
        if(isEmpty())
        {
            System.out.println("Created list");
            head = node;
            tail = node;
            cursor = node;
        }
        else
        {
            while(cursor!=null)
            {
                cursor = cursor.getNext();
            }
            tail.setNext(node);
            node.setPrev(tail);
            node.setNext(head);
            tail = node;
            cursor = node;

        }
        System.out.println("Out of addToEnd");
    }
    public boolean isEmpty()
    {
        return (cursor==null);
    }
    public boolean moveForward()
    {
        if(cursor!=tail)
        {
            cursor = cursor.getNext();

            return true;
        }
        else
            return false;       
    }
    public boolean moveBack()
    {
        if(cursor!=head)
        {
            cursor = cursor.getPrev();

            return true;
        }
        else
            return false;       
    }
    public void resetCursor()
    {
        cursor = head;
    }

    public static void main(String[] args)
    {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        try 
        {


            SlideList slides = new SlideList();

            System.out.println("AddToEnd");

            Slide newSlide = new Slide();
            newSlide.setData("1", "2", "3", "4", "5");
            slides.addToEnd(newSlide);
            slides.displayCurrentSlide();

            System.out.println("AddToEnd");

            Slide newSlide1 = new Slide();
            newSlide1.setData("2", "3", "3", "4", "5");
            slides.addToEnd(newSlide1);
            slides.displayCurrentSlide();

            System.out.println("AddToEnd");

            Slide newSlide2 = new Slide();
            newSlide2.setData("3", "4", "5", "6", "7");
            slides.addToEnd(newSlide2);
            slides.displayCurrentSlide();

            /*System.out.println("AddToEnd/ResetCursor");

            Slide newSlide3 = new Slide();
            newSlide3.setData("4", "5", "6", "7", "8");
            slides.resetCursor();
            slides.addToEnd(newSlide3);
            slides.displayCurrentSlide();
            System.out.println("Length of List");
            System.out.println(slides.listLength());
            System.out.println("Displaying All Slides");
            slides.displaySlides(1, 10);*/

        }
        /*catch (IOException e) 
        {
            System.out.println(e.getMessage());
        } */
        catch (EmptyListException e)
        {
            System.out.println(e.getMessage());
        }


    }


}
公共类幻灯片列表
{
专用滑动电极头;
私有滑极尾;
专用滑动节点游标;
公共幻灯片列表()
{
head=null;
tail=null;
游标=空;
}
public void editCurrentSlide(字符串文本,int-lineNum)引发IllegalArgumentException
{

如果(cursor==null | | lineNum>5 | | lineNum首先,大多数链接列表将有多个可能访问它的项,因此光标不是列表的固有部分,而是“列表访问器”的固有部分


第二,如果你做错了什么,你需要知道你打算做什么。我们可能会找到答案(正如我上面所做的);但是,没有人知道这是否是你特定问题的正确答案。描述问题(提供示例)我们会尽最大努力来描述它为什么会出乎意料地出现。

请考虑写一个。你的输出出了什么问题?请解释你得到的结果和你期望得到的结果。也考虑Mark Elliot的建议,否则没有人会费尽心思阅读所有的代码。所以不是你学习的地方。