如何在Java中对用户定义的双向链接列表进行排序?

问题描述

我已经编写了insertBefore,insertAfter,insertAtHead和insertAtTail函数,我计划将所有这些函数合并到一个insert()函数中,这些函数将在从文本文件中读取新值时执行插入排序。 / p>

文件

/*This program utilizes linked lists to keep track of all words in a text document. It produces a cross-reference,or a concordance.
It keeps track each of the words in a given file,the number of times it occurs,and which line numbers contain the word.*/

import java.util.Scanner;
import java.io.*;
public class Main
{
public static void main(String args[]) throws FileNotFoundException
{
    //declare/initialize linkedlist,scanner,input string and file
    LinkedList list = new LinkedList();
    Scanner scan = new Scanner(system.in);
    String line;
    String[] lineWords;
    File file;
    int c = 0;
    boolean done = false;
    boolean ignore;

    do
    {
        //ask for file name
        System.out.println("Please enter the filename: ");
        line = scan.next();
        file = new File(line);

        //check if file exists
        if (!file.exists())
        {
            System.out.println("That file doesn't exist. Please try again.");
            continue;
        }

        //read from file
        scan = new Scanner(file);

        //loop to display lines from file
        while (scan.hasNextLine())
        {
            line = scan.nextLine();
            c += 1;
            System.out.println(c + " " + line);

            //remove any unnecessary characters
            line = helper.format(line);

            //split line into array of words
            lineWords = line.split(" "); 

            //insert words into linkedlist 
            for (int i = 0; i < lineWords.length; i++)
            {
                //check if word should be ignored
                ignore = helper.shouldIgnore(lineWords[i]);

                //check if the current string is a word that should be ignored
                if (ignore) continue;

                //check if line contains characters
                else if (line.matches(".*\\w.*")) list.insert(lineWords[i],c);

                //otherwise don't add to the list
                else continue;
            }
        }

        //close scanner and exit do-while loop
        scan.close();
        done = true;

    } while (!done);

    //print blank line
    System.out.println("");

    //display list
    list.display();
} 
} 

Helper文件(仅格式化字符串并排除单词)

/*This file contains numerous helper functions for functionality*/

public class helper 
{
    //remove unnecessary characters
    static public String format(String s)
    {
        s = s.replace(",","");
        s = s.replace(".","");
        s = s.replace(";","");
        s = s.replace(":","");
        s = s.replace("\"","");
        s = s.replace("?","");
        s = s.replace("(","");
        s = s.replace(")","");
        s = s.replace("!","");
        s = s.replace("\r\n","").replace("\n","");
        s = s.toLowerCase();
        return s;
    }

    //function to ignore a,an,and the
    static public boolean shouldIgnore(String s)
    {
        if (s.equals("the") || s.equals("The") || s.equals("a") || s.equals("A") || s.equals("an") || s.equals("An")) return true;
        else return false;
    }
}

罪魁祸首,我的LinkedList文件

/*This file contains the functions and defining classes for linkedlists and nodes.*/

public class LinkedList
{
    //declare LinkedList values
    int size;
    Node head;
    Node tail;

    private static class Node
    {
        //node data
        String word;
        int line;
        Node next;
        Node prev;

        //constructor
        public Node(String w,int l)
        {
            this.word = w;
            this.line = l;
            this.next = null;
        }
    }

    //inserts node at head of list
    public void insert(String s,int l)
    {
        //create node to traverse through list
        Node current = head;

        //assign arguments to new node
        Node n = new Node(s,l);

        //check if list is empty
        if (head == null)
        {
            this.insertAtHead(n);
        }

        while (current != null)
        {
            if (current.word.compareto(n.word) > 0)
            {
                System.out.println("YOOOO");
                this.insertBefore(current,n);
            }
            else
            {
                System.out.println("noooo");
                System.out.println(current.word + " --> " + n.word);
                this.display();
                this.insertAfter(current,n);
            } 

            current = current.next;
        }
    }

    //insert a node before a specified node
    public void insertBefore(Node a,Node n)
    {
        //make the prevIoUs of the new node the prevIoUs of the specified node
        n.prev = a.prev;

        //make the prevIoUs of the specified node the new node
        a.prev = n;

        //make the next of the new node the specified node
        n.next = a;

        //if the new node has a prevIoUs node,make the next of the prevIoUs node the new node
        if(n.prev != null) n.prev.next = n;
    }

    //insert a node after a specified node
    public void insertAfter(Node a,Node n)
    {
        //check if the prevIoUs node is null
        if (a == null) 
        { 
            System.out.println("Error. You can't insert after a null node."); 
            return; 
        }  
    
        //make next of new node the next of prevIoUs node
        n.next = a.next; 
    
        //make next of prevIoUs node the new node
        a.next = n; 
    
        //make the prevIoUs of the new node the prevIoUs node
        n.prev = a; 
    
        //make the prevIoUs of the new node's next node the new node
        if (n.next != null) n.next.prev = n; 
    }

    //insert node at head of list
    public void insertAtHead(Node n)
    {
        //Assign the head to be the node after the new node
        n.next = head; 

        //make the prevIoUs value of the new node null
        n.prev = null; 
    
        //make the prevIoUs node of the head the new node
        if (head != null) head.prev = n; 
    
        //make the head point to the new node
        head = n; 
    }

    //insert node at tail of list
    public void insertAtTail(Node n)
    {
        //make a node to use to iterate through the list
        Node last = head;
    
        //the next of the tail must be null
        n.next = null; 
    
        //check if the list is empty
        if (head == null) 
        { 
            n.prev = null; 
            head = n; 
            return; 
        } 
    
        //else iterate through the list
        while (last.next != null) last = last.next; 
    
        //make the next node of the current tail the
        last.next = n; 
    
        /* 7. Make last node as prevIoUs of new node */
        n.prev = last; 
    }

    //displays list
    public void display() {  
        //start at head
        Node n = head;

        //check if list is empty
        if(head == null) 
        {
            System.out.println("List is empty");  
            return;
        }
 
        //traverse through list
        while(n != null) 
        {  
            System.out.println(n.word);  
            n = n.next;  
        }  
    }  
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)