我正在尝试实现一个 Queue 来反转堆栈并打印堆栈 FIFO?
I'm trying to implement a Queue that would reverse the stack made and print stack FIFO?
我正在尝试制作一个程序,以先进先出的顺序打印用户输入。我目前制作了一个打印堆栈 LIFO(后进先出)的程序。但是,我将如何实现一个以 FIFO(先进先出)顺序显示堆栈输出的队列。
我的代码如下:
import java.util.*;
public class stackReversal {
private class Node {
private String item;
private Node next;
}
private Node first = null;
public boolean isEmpty() {
return (first == null);
}
public void push(String s) {
Node node = new Node();
node.item = s;
node.next = first;
first = node;
}
public String pop() {
if (first == null)
throw new RuntimeException("Stack Empty!");
String result = first.item;
first = first.next;
return result;
}
public String popString() {
String result = "";
Node current = first;
while (current != null) {
result += current.item;
current = current.next;
}
return result;
}
public String toString() {
StringBuilder nodes = new StringBuilder();
Node node = first;
while (node != null) {
nodes.append(node.item).append("\n");
node = node.next;
}
if(isEmpty()) {
return "";
} else {
return nodes.toString().substring(0, nodes.toString().length());
}
}
class Queue{
Node first, last;
public void enqueue(Node n){
if(first == null){
first = n;
last = first;
}else{
last.next = n;
last = n;
}
}
public Node dequeue(){
if(first == null){
return null;
}else{
Node temp = new Node(first);
first = first.next;
return temp;
}
}
}
public static void main(String[] args)
{
stackReversal s = new stackReversal();
Scanner input = new Scanner(System.in);
System.out.print("Enter strings:");
String in = "";
while (!in.equals("end-of-input"))
{
in = input.nextLine();
if (in.equals("end-of-input"))
break;
else
s.push(in);
}
System.out.println("Strings:"+"\n" + s);
}
}
我按照上面的修改了。反转 logic.The 原来的 Scanner 方法格式不正确。
import java.util.*;
public class stackReversal {
private class Node {
private String item;
private Node next;
// private Node prev;
}
private Node first = null;
public boolean isEmpty() {
return (first == null);
}
public void push(String s) {
Node node = new Node();
node.item = s;
node.next = first;
first = node;
}
public String pop() {
if (first == null)
throw new RuntimeException("Stack Empty!");
String result = first.item;
first = first.next;
return result;
}
public String popString() {
String result = "";
Node current = first;
while (current != null) {
result += current.item;
current = current.next;
}
return result;
}
public String toString() {
StringBuilder nodes = new StringBuilder();
Node node = first;
while (node != null) {
nodes.append(node.item).append("\n");
node = node.next;
}
if(isEmpty()) {
return "";
} else {
return nodes.toString().substring(0, nodes.toString().length());
}
}
class Queue{
Node first, last;
public void enqueue(String s){
Node node = new Node();
node.item = s;
node.next = first;
first = node;
}
public Node dequeue(){
if(first == null){
return null;
}else{
Node temp = new Node();//Node temp = new Node();
first = first.next;
return temp;
}
}
public String toString() {
StringBuilder nodes = new StringBuilder();
Node node = first;
while (node != null) {
// Insert the current item at the beginning of the String
nodes.insert(0, String.format("%s%n", node.item));
node = node.next;
}
if(isEmpty()) {
return "";
} else {
return nodes.toString().substring(0, nodes.toString().length());
}
}
}
public static void main(String[] args)
{
stackReversal s = new stackReversal();
Queue q = s.new Queue();
Scanner input = new Scanner(System.in);
System.out.print("Enter strings:");
String in = input.nextLine();
while (!in.equals("end-of-input"))
{
if (in.equals("end-of-input"))
break;
else
q.enqueue(in);
s.push(in);
in = input.nextLine();
}
System.out.println("Strings:"+"\n" + s);
System.out.println("Strings:"+"\n" + q);
}
}
我正在尝试制作一个程序,以先进先出的顺序打印用户输入。我目前制作了一个打印堆栈 LIFO(后进先出)的程序。但是,我将如何实现一个以 FIFO(先进先出)顺序显示堆栈输出的队列。
我的代码如下:
import java.util.*;
public class stackReversal {
private class Node {
private String item;
private Node next;
}
private Node first = null;
public boolean isEmpty() {
return (first == null);
}
public void push(String s) {
Node node = new Node();
node.item = s;
node.next = first;
first = node;
}
public String pop() {
if (first == null)
throw new RuntimeException("Stack Empty!");
String result = first.item;
first = first.next;
return result;
}
public String popString() {
String result = "";
Node current = first;
while (current != null) {
result += current.item;
current = current.next;
}
return result;
}
public String toString() {
StringBuilder nodes = new StringBuilder();
Node node = first;
while (node != null) {
nodes.append(node.item).append("\n");
node = node.next;
}
if(isEmpty()) {
return "";
} else {
return nodes.toString().substring(0, nodes.toString().length());
}
}
class Queue{
Node first, last;
public void enqueue(Node n){
if(first == null){
first = n;
last = first;
}else{
last.next = n;
last = n;
}
}
public Node dequeue(){
if(first == null){
return null;
}else{
Node temp = new Node(first);
first = first.next;
return temp;
}
}
}
public static void main(String[] args)
{
stackReversal s = new stackReversal();
Scanner input = new Scanner(System.in);
System.out.print("Enter strings:");
String in = "";
while (!in.equals("end-of-input"))
{
in = input.nextLine();
if (in.equals("end-of-input"))
break;
else
s.push(in);
}
System.out.println("Strings:"+"\n" + s);
}
}
我按照上面的修改了。反转 logic.The 原来的 Scanner 方法格式不正确。
import java.util.*;
public class stackReversal {
private class Node {
private String item;
private Node next;
// private Node prev;
}
private Node first = null;
public boolean isEmpty() {
return (first == null);
}
public void push(String s) {
Node node = new Node();
node.item = s;
node.next = first;
first = node;
}
public String pop() {
if (first == null)
throw new RuntimeException("Stack Empty!");
String result = first.item;
first = first.next;
return result;
}
public String popString() {
String result = "";
Node current = first;
while (current != null) {
result += current.item;
current = current.next;
}
return result;
}
public String toString() {
StringBuilder nodes = new StringBuilder();
Node node = first;
while (node != null) {
nodes.append(node.item).append("\n");
node = node.next;
}
if(isEmpty()) {
return "";
} else {
return nodes.toString().substring(0, nodes.toString().length());
}
}
class Queue{
Node first, last;
public void enqueue(String s){
Node node = new Node();
node.item = s;
node.next = first;
first = node;
}
public Node dequeue(){
if(first == null){
return null;
}else{
Node temp = new Node();//Node temp = new Node();
first = first.next;
return temp;
}
}
public String toString() {
StringBuilder nodes = new StringBuilder();
Node node = first;
while (node != null) {
// Insert the current item at the beginning of the String
nodes.insert(0, String.format("%s%n", node.item));
node = node.next;
}
if(isEmpty()) {
return "";
} else {
return nodes.toString().substring(0, nodes.toString().length());
}
}
}
public static void main(String[] args)
{
stackReversal s = new stackReversal();
Queue q = s.new Queue();
Scanner input = new Scanner(System.in);
System.out.print("Enter strings:");
String in = input.nextLine();
while (!in.equals("end-of-input"))
{
if (in.equals("end-of-input"))
break;
else
q.enqueue(in);
s.push(in);
in = input.nextLine();
}
System.out.println("Strings:"+"\n" + s);
System.out.println("Strings:"+"\n" + q);
}
}