使用双链表进行堆栈
嘿。我被分配使用双链表做堆栈,我遇到了一个问题。我无法链接到前一个元素(虽然使用一个链接没有任何问题)。
class Node
{
Data data;
Node previous;
Node next;
}
class Data
{
int size;
double price;
boolean isOnOffer;
char sex;
String brand;
Data(int size, double price, boolean isOnOffer, char sex, String brand){
this.size = size;
this.price = price;
this.isOnOffer = isOnOffer;
this.sex = sex;
this.brand = brand;
}
}
class Stack
{
private static int sizeOfStack;
private static Node topElement;
public static boolean isEmpty() { return topElement == null; }
public static void Initialize() {
sizeOfStack = 0;
topElement = null;
}
public static void Push(Data x) {
Node oldElement = topElement;
topElement = new Node();
topElement.data = x;
topElement.next = oldElement;
topElement.previous = null;
//oldElement.previous = topElement; // <----- problem here
sizeOfStack++;
}
public static void Pop() {
if (!isEmpty()){
topElement = topElement.next; // delete first node
sizeOfStack--;
}
}
public static void Top() {
int size = topElement.data.size;
double price = topElement.data.price;
boolean isOnOffer = topElement.data.isOnOffer;
char sex = topElement.data.sex;
String brand = topElement.data.brand;
System.out.println(size + " " + price + " " + isOnOffer + " " + sex + " " + brand);
}
public static void Kill() { }
public static void Print() { }
public static void main(String[] args){
Push(new Data(37, 155, false, 'F', "Nike"));
Push(new Data(38, 140, true, 'F', "Reebok"));
Push(new Data(35, 160.99, false, 'F', "Converse"));
Push(new Data(35, 20.99, true, 'F', "Inkaras"));
Pop();
Pop();
Top();
}
}
Hey. I am assigned to do Stack using double-linked list and I faced a problem. I can't link to previous element (while I had no problems using one link).
class Node
{
Data data;
Node previous;
Node next;
}
class Data
{
int size;
double price;
boolean isOnOffer;
char sex;
String brand;
Data(int size, double price, boolean isOnOffer, char sex, String brand){
this.size = size;
this.price = price;
this.isOnOffer = isOnOffer;
this.sex = sex;
this.brand = brand;
}
}
class Stack
{
private static int sizeOfStack;
private static Node topElement;
public static boolean isEmpty() { return topElement == null; }
public static void Initialize() {
sizeOfStack = 0;
topElement = null;
}
public static void Push(Data x) {
Node oldElement = topElement;
topElement = new Node();
topElement.data = x;
topElement.next = oldElement;
topElement.previous = null;
//oldElement.previous = topElement; // <----- problem here
sizeOfStack++;
}
public static void Pop() {
if (!isEmpty()){
topElement = topElement.next; // delete first node
sizeOfStack--;
}
}
public static void Top() {
int size = topElement.data.size;
double price = topElement.data.price;
boolean isOnOffer = topElement.data.isOnOffer;
char sex = topElement.data.sex;
String brand = topElement.data.brand;
System.out.println(size + " " + price + " " + isOnOffer + " " + sex + " " + brand);
}
public static void Kill() { }
public static void Print() { }
public static void main(String[] args){
Push(new Data(37, 155, false, 'F', "Nike"));
Push(new Data(38, 140, true, 'F', "Reebok"));
Push(new Data(35, 160.99, false, 'F', "Converse"));
Push(new Data(35, 20.99, true, 'F', "Inkaras"));
Pop();
Pop();
Top();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如已经指出的:如果
oldElement
为 null,您将得到一个 NullPointerException。之前检查是否为 null,例如if(oldElement != null) { oldElement.previous = topElement; }
。另请注意,
Top()
方法不适用于空堆栈,它会在第一行topElement.data...
中抛出 NPE。As already pointed out: if
oldElement
is null, you'd get a NullPointerException. Check for null before, likeif(oldElement != null) { oldElement.previous = topElement; }
.Also note that the
Top()
method won't work for an empty stack, it would throw an NPE in the first linetopElement.data...
.看看不同的情况:
Case: #1 “空栈情况”
Case: #2 “正常情况”
看 Case: #3 我们发现它与 case #2 类似,不需要实现
Look at the different cases:
Case: #1 "empty stack case"
Case: #2 "normal case"
Looking at Case: #3 we see that it is similar to case #2, no need to implement