发布网友 发布时间:2022-04-25 16:04
共2个回答
热心网友 时间:2023-10-15 20:35
stack类修改一下就行了,修改处见注释
static class stack热心网友 时间:2023-10-15 20:35
public class solution0301 {
static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
static class stack {
Node top;
Object pop() {
if (top != null) {
Object topdata = top.data;
top = top.next;
return topdata;
}
return null;
}
void push(Node topdata) {
Node t = new Node(topdata.data);
t.next = top;
top = t;
}
Object peek() {
return top;
}
}
public static void main(String[] args) {
Node n1 = new Node(12);
Node n2 = new Node(23);
Node n3 = new Node(34);
stack stack = new stack();
stack.push(n1);
stack.push(n2);
stack.push(n3);
while (stack.peek() != null) {
System.out.println(stack.pop());
}
}
}
追问非常感谢,你们俩方法都对,他回复早了点就把分给他了,还是谢谢你啦!!