为什么我在java中的基本堆栈代码不是运行?
Why is my basic stack code in java not running?
所以我正在尝试编写堆栈数据结构的基础知识,当我 运行 sample_stack 中的 class 时,它根本就不是 运行ning而不是打印单词,而只是打印“null”:( 有人知道为什么吗?如果这是显而易见的,我们深表歉意
堆栈 JAVA CLASS:
import java.util.NoSuchElementException;
public class Stack {
// private inner class node
private class Node{
private String item;
private Node link;
public Node() {
item = null;
link = null;
}
public Node(String item, Node link) {
item = this.item;
link = this.link;
}
} // end of inner class
private Node head;
public Stack() {
head = null;
}
// method: PUSH into stack (like addToStart)
public void push(String itemName) {
head = new Node(itemName, head); // so head is the top of the stack ????
}
// method: POP out of stack
public String pop() {
if (head == null) throw new IllegalStateException();
else {
String returnItem = head.item;
head = head.link; // the second top item becomes the new head
return returnItem;
}
}
// method: is it empty?
public boolean isEmpty() {
return ( head == null );
}
}
CLASS 使用堆栈 JAVA CLASS:
public class Stack_Example {
public static void main (String[] args) {
Stack message = new Stack();
message.push("Hi");
System.out.println(message.pop());
message.push("my");
message.push("name");
message.push("is");
message.push("JARVIS");
while (!message.isEmpty()) { // while true
String s = message.pop();
System.out.println(s);
}
}
}
提前致谢!
public void push(String itemName) {
head = new Node(itemName, head); // so head is the top of the stack ????
}
调用构造函数时head为null,所以这里的link,public Node(String item, Node link) {
总是null
你不想,
public void push(String itemName) {
head = new Node(itemName, this);
}
代替?
此外,这是倒退的:
public Node(String item, Node link) {
item = this.item;
link = this.link;
}
应该是:
public Node(String item, Node link) {
this.item = item;
this.link = link;
}
更重要的是,您应该在进行过程中调试所有这些
所以我正在尝试编写堆栈数据结构的基础知识,当我 运行 sample_stack 中的 class 时,它根本就不是 运行ning而不是打印单词,而只是打印“null”:( 有人知道为什么吗?如果这是显而易见的,我们深表歉意
堆栈 JAVA CLASS:
import java.util.NoSuchElementException;
public class Stack {
// private inner class node
private class Node{
private String item;
private Node link;
public Node() {
item = null;
link = null;
}
public Node(String item, Node link) {
item = this.item;
link = this.link;
}
} // end of inner class
private Node head;
public Stack() {
head = null;
}
// method: PUSH into stack (like addToStart)
public void push(String itemName) {
head = new Node(itemName, head); // so head is the top of the stack ????
}
// method: POP out of stack
public String pop() {
if (head == null) throw new IllegalStateException();
else {
String returnItem = head.item;
head = head.link; // the second top item becomes the new head
return returnItem;
}
}
// method: is it empty?
public boolean isEmpty() {
return ( head == null );
}
}
CLASS 使用堆栈 JAVA CLASS:
public class Stack_Example {
public static void main (String[] args) {
Stack message = new Stack();
message.push("Hi");
System.out.println(message.pop());
message.push("my");
message.push("name");
message.push("is");
message.push("JARVIS");
while (!message.isEmpty()) { // while true
String s = message.pop();
System.out.println(s);
}
}
}
提前致谢!
public void push(String itemName) {
head = new Node(itemName, head); // so head is the top of the stack ????
}
调用构造函数时head为null,所以这里的link,public Node(String item, Node link) {
总是null
你不想,
public void push(String itemName) {
head = new Node(itemName, this);
}
代替?
此外,这是倒退的:
public Node(String item, Node link) {
item = this.item;
link = this.link;
}
应该是:
public Node(String item, Node link) {
this.item = item;
this.link = link;
}
更重要的是,您应该在进行过程中调试所有这些