我正在尝试为java中的链表堆栈编写搜索功能。我遇到了问题,因为当我声明一个指向我的head变量的临时变量时,我在搜索时不会丢失实际的值栈,我无法调用我的topX和topY方法,我不确定为什么。造成这种情况的原因是什么?
我得到的错误是myNode类型未定义topX(MyNode)。
我的代码:
public class MyStack
{
private MyNode head;
public MyStack()
{
head = null;
}
public boolean isEmpty()
{
if (head == null)
return true;
else
return false;
}
public void push (int v1, int v2)
{
MyNode tmp = new MyNode (v1, v2);
tmp.next = head;
head = tmp;
}
public void pop()
{
head = head.next;
}
public int topX(MyNode node)
{
return node.x;
}
public int topY(MyNode node)
{
return node.y;
}
//THIS IS WHERE i get my error
public boolean search(int v1, int v2, MyNode node){
MyNode temp = node;
//My problem is I can't even call this function
while (temp.topX(node)!= v1 && temp.topY(node) != v2
&& temp.next!=null){
temp = temp.next;
}
}
}
//I declared MyNode seperately
class MyNode
{
int x, y;
MyNode next;
MyNode (int v1, int v2)
{
x = v1;
y = v2;
next = null;
}
}
}