刚学数据结构中的栈,写了一个单链栈的java代码实现。
public class SingleStack {
protected Node  topNode;


public void initStack() {
topNode = null;
}

public void push(int dataValue){
Node newNode = new Node(dataValue);
push(newNode);
}
public void push(Node newNode) {
if(topNode == null) {topNode = newNode;newNode.next = null;}
newNode.next = topNode;
topNode = newNode;
}



public int pop() {
int i = topNode.data;
topNode = topNode.next;
return i;
}


public int getTop() {
return topNode.data;
}


public void setEmpty() {
topNode = null;
}

public boolean empty() {
if(topNode == null) return true;
return false;
}}class Node {
public int data;
public Node next;

public Node() {
next = null;
}
public Node(int dataValue) {
data = dataValue;
next = null;
}
}测试时好像pop方法出了问题,就是pop执行五次后里面好像还有元素,调了半天没高兴
public class TestSingleStack {
public static void main(String[] args) {
SingleStack ob = new SingleStack();
ob.initStack();
ob.push(8);
ob.push(8);
ob.push(0);
ob.push(3);
ob.push(4);
System.out.print(ob.pop());
System.out.print(ob.pop());
System.out.print(ob.pop());
System.out.print(ob.pop());
System.out.print(ob.pop());
System.out.print(ob.getTop());
System.out.println(ob.empty());

}}输出是:430888false

解决方案 »

  1.   

    public class SingleStack {
        protected Node  topNode;
        
        
        public void initStack() {
            topNode = null;
        }
        
        public void push(int dataValue){
            Node newNode = new Node(dataValue);
            push(newNode);
        }
        public void push(Node newNode) {
            if(topNode == null) 
            {
             topNode = newNode;
             newNode.next = null;
            }
            else
            {
             newNode.next = topNode;
             topNode = newNode;
            }
        }
        
        
        
        public int pop() {
            int i = topNode.data;
            topNode = topNode.next;
            return i;
        }
        
        
        public int getTop() {
            return topNode.data;
        }
        
        
        public void setEmpty() {
            topNode = null;
        }
        
        public boolean empty() {
            if(topNode == null) return true;
            return false;
        }}class Node {
        public int data;
        public Node next;
        
        public Node() {
            next = null;
        }
        public Node(int dataValue) {
            data = dataValue;
            next = null;
        }
    }public class TestSingleStack {
        public static void main(String[] args) {
            SingleStack ob = new SingleStack();
            ob.initStack();
            ob.push(8);
            ob.push(8);
            ob.push(0);
            ob.push(3);
            ob.push(4);
            System.out.print(ob.pop());
            System.out.print(ob.pop());
            System.out.print(ob.pop());
            System.out.print(ob.pop());
            System.out.print(ob.pop());
            //System.out.print(ob.getTop());
            System.out.println(ob.empty());
            
        }}
      

  2.   

    push(Node newNode)中if 后面少了else
    另外getTop方法会造成NullPointerException,需要捕获
      

  3.   

    难道就是因为省略了一个else分支
      

  4.   

    恩,有点类似C语言中的链表。
    放入第一个元素时topNode指向为null,所以直接将传入的Node对象赋值给topNode即可。当topNode不为null时,要将传入的Node对象newNode的next指向topNode,然后将topNode指向newNode.
    你没加else的话,无论topNode是否为null,后面的语句块都会执行,相等于if{}中的语句不起作用
      

  5.   


    public int pop() {
            if(empty())
              return -1;//抛异常更好些
            int i = topNode.data;
            topNode = topNode.next;
            return i;
        }
        
        
        public int getTop() {
            return topNode.data;
        }
      

  6.   

    public void push(Node newNode) {
            if(topNode == null) {topNode = newNode;newNode.next = null;}
            newNode.next = topNode;
            topNode = newNode;
        }
    看一下这个方法...topNode == null 时topNode = newNode;  这时也就注定了topNode不可能为空了..
    public boolean empty() {
            if(topNode == null) return true;
            return false;
        }
    所以这个方法永远返回false...
    其实你可以设置一个全局变量...做为整个链的容量指针...初始为0,push一个加1,pop一个减1...当指针为0的时候就是空...