试图从.txt文件中获取输入,并将其放入双链表中

问题描述 投票:0回答:1

我正在尝试从.txt文件中获取输入,并以字母顺序将其放在双链表中。但是,.txt文件的某些名称最终还是加入了,并且以某种方式从双重链表中消失了?在排序()在主的插入函数功能应该按字母顺序的名称进行排序,然而有时不把按字母顺序排列的姓名。我怎样才能解决这个问题呢?

谢谢:)

主要方法

import java.util.*;
import java.io.*;
class Main 
{
  public static void main(String[] args) throws FileNotFoundException 
  {
    DLL dub = new DLL();
    dub=insert("input.txt");
    dub.traverse();
  }
  public static DLL insert(String fileName) throws FileNotFoundException // gets input from .txt file
  {
    DLL list = new DLL();
    try(Scanner file = new Scanner(new File(fileName)))
    {
      while(file.hasNextLine())
      {
        String line = file.nextLine();
        if(line.startsWith("delete ")) // if .txt contains delete then delete then find the word from the DLL, and remove it
        {
          String element = line.substring("delete ".length());
          list.remove(element);
          continue;
        }
        list.add(line);
      }
      list.sort();
    }
    return list;
  }
}

双向链表类编号

import java.io.*;
import java.util.*;
public class DLL
{
  private Node head;
  private Node tail;
  /******************************************************************/
  public void traverse() // this method displays the DLL traversing forward and backward
  {
    System.out.println();
    for(Node n=head; n!=null; n=n.getNext()) // traverses forwards
    {
      System.out.println(n.getValue());
    }
    System.out.println("==========");
    for(Node n=tail; n!=null; n=n.getPrev()) // traverses backwards
    {
      System.out.println(n.getValue());
    }
  }
  /******************************************************************/
  public void add(String line) // this method adds a name into the doubly linked list 
  {
    Node newNode = new Node(line); // creates a new node 
    if(head==null) //if the head is null set the head and tail equal to the new node 
    {
      head=newNode;
      tail=newNode;
    }
    else // otherwise, add the tail at the end 
    {
      newNode.setPrev(tail);
      tail.setNext(newNode);
      tail=newNode;
    }
  }
  /******************************************************************/
  public void remove(String line) // removes a node 
  {
    Node nodeToDelete = find(line); // checks to see if node is available
    removeNode(nodeToDelete); // remove the node 
  }
  /******************************************************************/
  private Node find(String element) // this method checks to see if the name is available 
  {
    Node curr = head;
    if(curr==null) // if the node is empty, then make the program crash
    {
      throw new IllegalStateException("LinkedList is empty.");
    }
    while(curr!=null) // otherwise, traverse through the program
    {
      if(curr.getValue().equals(element))
      {
        return curr;
      }
      curr=curr.getNext();
    }
    if(curr==null) // if element cant be found then throw an exception
    {
      throw new IllegalStateException("Element was not found.");
    }
    return null; // returns nothing 
  }
  /******************************************************************/
  public Node findSmallest() // this method finds 
  {
    Node curr = head;
    if(curr==null)
    {
      throw new IllegalStateException("LinkedList is empty.");
    }
   String smallestElement = curr.getValue();
   Node smallestNode=curr;
   while(curr!=null)
   {
      if(smallestElement.compareTo(curr.getValue())<0)
      {
        smallestNode=curr;
        smallestElement = smallestNode.getValue();      
      }
      curr=curr.getNext();    
   }
    return smallestNode;
  }
  /******************************************************************/
  public void removeNode(Node nodeToDelete)
  {
    Node prev = nodeToDelete.getPrev();
    Node next = nodeToDelete.getNext();

    if(prev == null && next == null) // case to remove an item with no previous or next 
    {
      head=null;
      tail=null;
    }
    else if(prev==null) // case to remove a name that happens to be at the head 
    {
      head=next;
      head.setPrev(null);
    }
    else if(tail.getNext()==null) // case to remove a name that happens to be at the tail
    {
      tail=prev;
      tail.setNext(null);
    }
    else // otherwise remove the node in-between
    {
      prev.setNext(next);
      next.setPrev(prev);
    }
  }
  /******************************************************************/
  public boolean isEmpty()
  {
    return head==null;
  }
  /******************************************************************/
  public void sort()
  {
    DLL sorted = new DLL();
    if(isEmpty())
    {
      return;
    }    
    while (!isEmpty()) 
    {
      Node smallest = findSmallest();
      sorted.add(smallest.getValue());
      remove(smallest.getValue());
    }
    head = sorted.head;
    tail = sorted.tail;
  }
}

节点类编号

public class Node
{
  private Node prev;
  private Node next;
  private String name;
  Node(String name) // constructor
  {
    this.name=name;
  }
  public String getValue() // this function returns the name 
  {
    return this.name;
  }
  public void setValue(String name) // this function changes the value
  {
    this.name=name;
  }
  public Node getNext() // this function returns the previous node 
  {
    return this.next;
  }
  public Node getPrev() // this function returns the previous node 
  {
    return this.prev;
  }

  public void setNext(Node next) // this function sets the next node
  {
    this.next=next;
  }

  public void setPrev(Node prev) // this function sets the previous node
  {
    this.prev=prev;
  }
}

input.txt文件#

Jim
Jill
John
Louis
delete Jill
Bob
Jack
delete Jim

输出我得到#

Jack
Bob
=========
Bob
Jack

我期望的输出

Bob
Jack
John
Louis 
=========
Louis 
John
Jack 
Bob
java algorithm input linked-list doubly-linked-list
1个回答
0
投票

注意DLL中的这一行:

else if(tail.getNext()==null)

该条件始终为true,因为尾部之后没有节点。

也许您想要这个?

else if (next == null)
© www.soinside.com 2019 - 2024. All rights reserved.