尝试从 .txt 文件获取输入,并将其放入双向链表
Trying to get input from a .txt file, and put it in a doubly linked list
我正在尝试从 .txt 文件中获取输入,并将其放入按字母顺序排列的双向链表中。然而,.txt 文件的某些名称最终会进入并以某种方式从双向链表中消失? main 中 insert 函数中的 .sort() 函数应该按字母顺序对名称进行排序,但有时它不会把名字按字母顺序排列。我该如何解决这个问题?
谢谢:)
主要方法
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;
}
}
双向链表Class#
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;
}
}
节点Class#
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
注意 DLL 中的这一行:
else if(tail.getNext()==null)
该条件始终是 true
,因为尾部后面永远没有节点。
也许你想要这个?
else if (next == null)
我正在尝试从 .txt 文件中获取输入,并将其放入按字母顺序排列的双向链表中。然而,.txt 文件的某些名称最终会进入并以某种方式从双向链表中消失? main 中 insert 函数中的 .sort() 函数应该按字母顺序对名称进行排序,但有时它不会把名字按字母顺序排列。我该如何解决这个问题?
谢谢:)
主要方法
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;
}
}
双向链表Class#
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;
}
}
节点Class#
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
注意 DLL 中的这一行:
else if(tail.getNext()==null)
该条件始终是 true
,因为尾部后面永远没有节点。
也许你想要这个?
else if (next == null)