Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中优化和解决问题的线性节点_Java_Algorithm_Queue_Nodes_Shortest Path - Fatal编程技术网

java中优化和解决问题的线性节点

java中优化和解决问题的线性节点,java,algorithm,queue,nodes,shortest-path,Java,Algorithm,Queue,Nodes,Shortest Path,最终,我真的想把它作为一个解谜工具,但我想我也可以为了其他目的修改它……我试图找到起始词和结束词之间最短的单词链,一次改变一个字母。我使用线性节点和链接队列来实现这一点 import java.util.*; public class Driver { static LinkedQueue<LinearNode<String>> open = new LinkedQueue<LinearNode<String>>(); publi

最终,我真的想把它作为一个解谜工具,但我想我也可以为了其他目的修改它……我试图找到起始词和结束词之间最短的单词链,一次改变一个字母。我使用线性节点和链接队列来实现这一点

import java.util.*;

public class Driver
{
    static LinkedQueue<LinearNode<String>> open = new LinkedQueue<LinearNode<String>>();
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a four letter start word: ");
    String start = input.next();
    System.out.print("Enter a four letter end word: ");
    String end = input.next();
    System.out.println();
    input.close();

    if (start.equals(end))
    {
        System.out.println("The words are the same.\nThe chain length is 0.");
    }
    else
    {
        System.out.println(findChain(start, end));
    }
}

public static String findChain(String start, String end)
{
    boolean finalFound = false;
    Dictionary dict = new Dictionary("four.txt");
    char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    LinearNode<String> begin = new LinearNode<String>(start);
    ArrayList<String> used = new ArrayList<String>();
    used.add(start);
    open.enqueue(begin);

    while((!open.isEmpty()) && (!finalFound))
    {
        LinearNode<String> currentNode = open.dequeue();
        String currentWord = currentNode.getElement();
        char[] charStr = currentWord.toCharArray();

        for (int i = 0; i < charStr.length; i++)
        {
            for (char ch : alphabet)
            {
                char[] charStr2 = charStr.clone();
                charStr2[i] = ch;
                String newWord = new String(charStr2);

                if (dict.contains(newWord) && (!used.contains(newWord)))
                {
                    used.add(newWord);
                    LinearNode<String> newNode = new LinearNode<String>(newWord, currentNode);
                    open.enqueue(newNode);
                    if (newWord.equals(end))
                    {
                        finalFound = true;
                        i = 4;
                        break;
                    }
                }
            }
        }
    }

    if (finalFound == true)
    {
        String str = "";
        LinkedStack<String> stack = new LinkedStack<String>();

        while (!open.isEmpty())
        {
            stack.push(open.dequeue().toString());
        }

        for (int j = 0; j < stack.size() - 1; j++)
        {
            str += stack.pop() + "\n";
        }
        return str;
    }
    else
        return "No chain found";
    }
}


    public class LinkedQueue<T> implements QueueADT<T>
    {
        private int count;
        private LinearNode<T> front, rear;

        public LinkedQueue()
        {
            count = 0;
            front = rear = null;
        }

        public void enqueue(T element)
        {
            LinearNode<T> node = new LinearNode<T>(element);

            if (isEmpty())
                front = node;
            else 
                rear.setNext(node);

            rear = node;
            count++;
        }

        @Override
        public T dequeue() throws EmptyCollectionException
        {
            if (isEmpty())
                throw new EmptyCollectionException("queue");

            T result = front.getElement();
            front = front.getNext();
            count--;

            if (isEmpty())
                rear = null;

            return result;
        }

        @Override
        public T first()
        {
            return front.getElement();
        }

        @Override
        public boolean isEmpty()
        {
            return (count == 0);
        }

        @Override
        public int size()
        {
            return count;
        }

        @Override
        public String toString()
           {
              String result = "";
              LinearNode<T> current = front;

              while (current != null)
              {
                 result = result + (current.getElement()).toString() + "\n";
                 current = current.getNext();
              }

              return result;
           }

    }


public class LinkedStack<T> implements StackADT<T>
{
    private int count;
    private LinearNode<T> top;

    public LinkedStack()
    {
        setCount(0);
        top = null;
    }

    public void push(T element)
    {
        LinearNode<T> temp = new LinearNode<T>(element);

        temp.setNext(top);
        top = temp;
        setCount(getCount() + 1);

    }

    public T pop()
    {
        if (isEmpty())
            throw new EmptyCollectionException("stack");

        T result = top.getElement();
        top = top.getNext();
        setCount(getCount() - 1);

        return result; 
    }

    public T peek()
    {
        if (isEmpty())
            throw new EmptyCollectionException("stack");

        T result = top.getElement();

        return result;
    }

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

    public int size()
    {
        return getCount();
    }

    public int getCount()
    {
        return count;
    }

    public void setCount(int count)
    {
        this.count = count;
    }

}
import java.util.*;
公务舱司机
{
静态LinkedQueue open=新建LinkedQueue();
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入四个字母的起始单词:”);
字符串start=input.next();
System.out.print(“输入四个字母的结束词:”);
String end=input.next();
System.out.println();
input.close();
如果(开始等于(结束))
{
System.out.println(“单词相同。\n链长度为0”);
}
其他的
{
System.out.println(findChain(开始,结束));
}
}
公共静态字符串findChain(字符串开始、字符串结束)
{
布尔finalFound=false;
Dictionary dict=新字典(“four.txt”);
字符[]字母表={'a','b','c','d','e','f','g','h','i','j','k','l',',
‘m’、‘n’、‘o’、‘p’、‘q’、‘r’、‘s’、‘t’、‘u’、‘v’、‘w’、‘x’、‘y’、‘z’};
LinearNode begin=新的LinearNode(开始);
ArrayList used=新的ArrayList();
已使用。添加(开始);
打开。排队(开始);
而((!open.isEmpty())&&(!finalFound))
{
LinearNode currentNode=open.dequeue();
字符串currentWord=currentNode.getElement();
char[]charStr=currentWord.toCharArray();
for(int i=0;i
请指定您遇到的问题。@afzalex它输出了大量看似随机的单词,我怀疑这是一个列表,其中列出了它确定的每个单词,而不是最短链问题的解决方案。您是否创建了自己的
LinkedQueue
LinkedStack
字典
?如果是这样的话,那么为什么不使用标准的
收集框架
。为什么你认为不看你的代码就有人能告诉你问题出在哪里,我知道这些都能正常工作。Dictionary只是一个包含arrayList的对象,但我想要一个自定义对象,因为我