简体   繁体   English

我尝试使用Google搜索。 DoublyLinkedList问题。 编辑(2)

[英]I tried googling this. DoublyLinkedList question. Edited(2)

I am new to LinkedLists. 我是LinkedLists的新手。 We are working with DoublyLinkedLists and we have to do a method called addAfter. 我们正在使用DoublyLinkedLists,我们必须执行一个名为addAfter的方法。 So I googled it and came up with nothing. 所以我用谷歌搜索,却一无所获。 The node class and variable initialation is given to us in a skeleton by him as well, so right away I am thinking, am I missing an initialation for newInfo and afterThis?. 节点类和变量初始化也由他在骨架中提供给我们,所以我马上想到,我是否缺少newInfo和afterThis的初始化? This is how he describes addAfter--- 这就是他描述addAfter--

This method adds a new node containing the integer newInfo to the next of the node containing the integer afterThis. 此方法将包含整数newInfo的新节点添加到包含整数afterThis的节点的下一个节点。 If there is no node containing the integer afterThis, insert the new node at the last. 如果afterThis之后没有包含整数的节点,请在最后插入新节点。 If there are multiple nodes containing the integer afterThis, insert the new node right next to the first occurrence of afterThis in the list. 如果存在多个包含afterThis整数的节点,则在列表中首次出现afterThis的旁边插入新节点。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;



//import DoublyLinkedList.Node;



public class DoublyLinkedList {
    public static class Node {
        private int element;
        private Node prev, next;


        public Node(int e, Node p, Node n) {
            element = e;
            prev = p;
            next = n;
        }

        public  int getElement() {
            return element;
        }

        public Node getPrev() {
            return prev;
        }

        public Node getNext(){
            return next;
        }

        public void setPrev(Node p) {
            prev = p;
        }

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


    }
/***************************************************************************************************************************/   
    public DoublyLinkedList(String fileName) throws IOException {

        FileReader input = new FileReader(fileName); // this is where it reads the filenames HW4-A and myAnotherInput
        BufferedReader myReader = new BufferedReader(input);
        String line = "";

        while ((line = myReader.readLine()) != null) {
            int nextNumber = Integer.parseInt(line.trim());//needed trim or it kept throwing an error. Suggested by Liam.
            // System.out.println(nextNumber);
            addLast(nextNumber); // takes the number given by parseint and puts it last
            System.out.println(nextNumber);
        }
        input.close();


    }
    private Node header;
    private Node trailer;
    private int size = 0;

    public DoublyLinkedList() {
        header = new Node(0, null, null);
        trailer = new Node(0, header, null);
        header.setNext(trailer);

    }



    public String toString() {
        String toReturn = "";

        Node current = header;


        while (current != null) {
            toReturn += current.getElement();//Credit to Liam (do not know last name, a CS student friend)
            current = current.getNext();

            if (current == null) {
                break;
            } else {
                toReturn += " -> "; // Allows for -> to be put in between the numbers 

            }

        }

        return toReturn;
    }



    public void reverseList() {
        // complete this method
    }

    public void addAfter(int newInfo, int afterThis) {
        Node tmp = new Node(newInfo,header.next, trailer.prev);
    tmp.prev = header.next;
    header.next = header;
    tmp.next = header.next;

    if(afterThis == null)
        newInfo.addLast();
    }

    public void negateList() {
        // complete this method
    }


    public void addFirst(int e) {
        Node tmp = new Node(e, header, null);
        if(header != null ) {header.prev = tmp;}
        header = tmp;
        if(trailer == null) { trailer = tmp;}
        size++;
    }

    public void addLast(int e) {

        Node tmp = new Node(e, null, trailer);
        if(trailer != null) {
            trailer.next = tmp;
            }
        trailer = tmp;
        if(header == null) {
            header = tmp;


        size++;
        }
}

    public int size() {
        return size;//line 39 size
    }

    public boolean isEmpty() {
        return size == 0;
    }

}

this is what I have so far. 这是我到目前为止所拥有的。 Again,I just need a little guidance on the addAfter. 同样,我只需要对addAfter进行一些指导。 I appreciate any education with LinkedLists because they are proving a hard thing for me to grasp. 我很欣赏LinkedLists,因为它们证明了我很难掌握的知识。 Thanks! 谢谢!

Think about this logically, a double linked list is a series of nodes that looks like: 从逻辑上考虑一下,双链表是一系列看起来像以下的节点:

在此处输入图片说明

How would we go about adding something after B, but before C? 我们将如何在B之后但C之前添加内容?

  1. Take our new item, and set it to b's next and C's prev. 接受我们的新项目,并将其设置为b的下一个和c的上一个。
  2. Take B and set it to our item's prev. 取B并将其设置为我们商品的上一个。
  3. Take C and set it to our items's next. 取C并将其设置为下一个项目。

There is also the special case, where you are adding after D or the last element in the list. 还有一种特殊情况,您要在D或列表中的最后一个元素之后添加。 In this situation you simply set D's next, and the new item's prev. 在这种情况下,您只需设置D的下一个以及新项目的上一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM