【问题标题】:Double Linked List Implementation Alphabetically双链表实现按字母顺序
【发布时间】:2020-06-13 17:38:05
【问题描述】:

我正在尝试在 java 中构建一个按字母顺序组织的链表,但我在插入和重新排列列表的逻辑方面遇到了问题,到目前为止我的实现看起来像,数据包含一个名称 getter,它曾经是整理其在列表中的位置:

    class Node
{

   private Data data;
   private Node nextNode;

   public Node (Data data, Node nextElement)
   {
      this.nextNode = nextElement;
      this.data = data;
   }

   public Node getNextNode ()
   {
      return nextNode;
   }

   public void setNextNode (Node nextNode)
   {
      this.nextNode = nextNode;
   }

   public Data getData ()
   {
      return data;
   }

   public void setData (Data data)
   {
      this.data = data;
   }
}

那么我的链表类如下:

public class LinkedList {
    private Node head = null;
    private int size = 0; 

    public void addData(Data c){
        this.size++;

        //empty case
        if(this.head == null){
            this.head = new Node(c, null);
            return;
        }



        Node current = this.head;
        Node previous = null;

        while(current.getNextNode()!=null&&previous.getData().getFirstName().compareTo(c.getFirstName())<=1){
            previous = current; // hold last element 
            current = current.getNextNode(); //move to next element
           }

        //insert into list
        if(current.getNextNode()==null){
            current.setNextNode(new Node(c,null)); //append end of List 
            previous = new Node(current.getData(), current);
        }else{
            Node insert = new Node(c, current); //prepend infront
            current.setNextNode(insert);
        }

我尝试的一切似乎都给了我的空指针异常,但也许我在这里没有得到这些概念。我首先检查我的列表是否为空,并在其前面添加一个指向 null 的节点。当给定新节点时,我循环遍历当前列表,直到找到一个点并将前一个指针设置为当前,当前成为指向列表中下一项的新节点(假设我们不在最后的列表,它只是指向空)。

在这一点上我很迷茫,我的 this.head 去哪里了,还是我必须在所有边缘情况下都这样做?任何理解方面的帮助将不胜感激

【问题讨论】:

  • 首先Node类的实现不是双链表。双链表的每个节点都包含对前一个节点和下一个节点的引用。
  • 是的,我猜它是一个单链表,但我必须跟踪前一个和下一个节点以按字母顺序执行。

标签: java linked-list nullpointerexception nodes


【解决方案1】:

您的实现是 Double-LinkedList,它不包含对前一个节点的引用。

这里如何按顺序插入

        public void addData(Data c) {
            Node newNode = new Node(c, null);

            if (head == null) {
                head = newNode;
                size++;
                return;
            }

            Node current = head;
            Node prev = null;
            int comparison;

            while (current != null) {
                comparison = c.getFirstName().compareTo(current.getData().getFirstName());
                if (comparison == 0) {
                    return;
                } else if (comparison > 0) { /// greater than
                    if (current.nextNode == null) { // check if reach tail of the linked list add and break
                        current.nextNode = newNode;
                        break;
                    }
                } else { // less then
                    if (prev == null) { // check if it should be first then put and break
                        Node oldHead = head;
                        head = newNode;
                        head.nextNode = oldHead;
                        break;
                    }
                    prev.nextNode = newNode;
                    newNode.nextNode = current;
                    break;
                }
                prev = current;
                current = current.nextNode;
            }
            size++;
        }

【讨论】:

  • 谢谢,这更有意义。但是对于 compare=0 的情况,具有相同“firstName”的数据元素不会被添加到列表中。我认为只保留它作为比较 >=0 将允许匹配的元素被附加到之后的位置
【解决方案2】:
public void addData(Data c) {
    this.size++;

    //empty case
    if (this.head == null) {
        this.head = new Node(c, null);
        return;
    }


    Node current = this.head;
    Node previous = null;

    //change in Code
    while (current.getNextNode() != null && current.getData().getFirstName().compareTo(c.getFirstName()) <= 1) {
        previous = current; // hold last element
        current = current.getNextNode(); //move to next element
    }

    //insert into list
    if (current.getNextNode() == null) {
        current.setNextNode(new Node(c, null)); //append end of List
        previous = new Node(current.getData(), current);
    } else {
        Node insert = new Node(c, current); //prepend infront
        current.setNextNode(insert);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-10
    • 2019-01-25
    • 2019-06-12
    • 2021-01-20
    • 2020-10-12
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多