Java——在堆栈中实现队列



我已经创建了下面的代码,我不打算实现Stack和Queue。现在我已经实现了Stack,我认为我实现了一个Queue,所以我想知道在哪里以及如何实现Queue。记住,我必须使用队列来馈送堆栈,但我似乎绕过了这一点:)

非常感谢您的帮助!

package stackandqueue;

import java.util.*;
import java.util.Stack;
import java.util.Queue;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.Arrays;
public class StackAndQueue 
{
public static void main(String[] args) throws IOException 
{
// Create three empty stacks of Bays.
// Bay 1 linked list
Queue<String> bayoneStack = new LinkedList<String>();
// Bay 2 linkd list.
Queue<String> baytwoStack = new LinkedList<String>();
// Bay 3 linked list
Queue<String> baythreeStack = new LinkedList<String>();

Queue<String> bayloadStack = new LinkedList<String>();

System.out.println("***********************************************");
// Open and read text file
String inputFileName = "PodData4.txt";
FileReader fileReader = new FileReader("PodData4.txt");
// Create the FileReader object
try (BufferedReader br = new BufferedReader(fileReader);)
{    
// Sort the data into the relevant linked list by type F, T or P.
String[] strings = br.readLine().split(",");
for (String str : strings)
{
    switch (str.charAt(0))
    {
        case 'F':
            bayoneStack.add(str);
            break;
        case 'T':
            baytwoStack.add(str);
            break;
        case 'P':
            baythreeStack.add(str);
            break;
        default:
            // In-case of invalid input
    }
    System.out.println(str);

}
} 
catch(IOException ex)
{
//handle exception;
}
finally
{
fileReader.close();
}
// Prints out the linked list stacks showing all Bays.
System.out.println("***********************************************");
System.out.println("Bay 1:Food: " + bayoneStack.toString());
System.out.println("Bay 2:Technical: " + baytwoStack.toString());
System.out.println("Bay 3:Personal: " + baythreeStack.toString());
}
}

您可以利用LinkedList实现队列接口的事实:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
public class Test {
  public static void main( String[] args ) throws IOException {
    // Create three empty stacks of Bays.
    // Bay 1 linked list
    Queue<String> bayoneStack = new LinkedList<String>();
    // Bay 2 linkd list.
    Queue<String> baytwoStack = new LinkedList<String>();
    // Bay 3 linked list
    Queue<String> baythreeStack = new LinkedList<String>();
    // Open and read text file
    String inputFileName = "PodData4.txt";
    FileReader fileReader = new FileReader( inputFileName );
    // Create the FileReader object
    try ( BufferedReader br = new BufferedReader( fileReader ); ) {
      // Sort the data into the relevant linked list by type F, T or P.
      String[] strings = br.readLine().split( "," );
      for ( String str : strings ) { 
        switch ( str.charAt( 0 ) ) {
          case 'F':
            bayoneStack.add( str );
            break;
          case 'T':
            baytwoStack.add( str );
            break;
          case 'P':
            baythreeStack.add( str );
            break;
          default:
            // In-case of invalid input
            System.out.println( "" );
        }
      }
    } catch ( IOException ex ) {
      // handle exception;
    } finally {
      fileReader.close();
    }
    // Prints out the linked list stacks showing all Bays.
    System.out.println( "Bay 1:Food: " + bayoneStack.toString() );
    System.out.println( "Bay 2:Technical: " + baytwoStack.toString() );
    System.out.println( "Bay 3:Personal: " + baythreeStack.toString() );
  }
}

相关内容

  • 没有找到相关文章

最新更新