这几天在学数据结构的时候遇到一个这样的问题,在写链式队列的入队列与出队列方法的时候,先导入了单链表结点类,此结点类有两个成员变量,data和next分别表示结点的数据区与后继结点,如果将这两个变量声明为public型的,例如s.next=s.next.next;没问题;但是如果这两个变量分别声明为private时,用getData()和getNext()方法赋值,例如,s.getNext()=s.getNext().getNext();此时出现错误,赋值语句左边应该是一个变量,不应是值,真是不明白这是怎么回事,一下是我链式队列中的入队和出队的两个方法,这两个方法存在问题,但是我无法找出,请高手帮助我解答
public boolean enQueue(Object obj){
if(obj==null) return false;
else{
OneLinkNode p=new OneLinkNode(obj); 
        OneLinkNode s=rear.getNext();
        s=p;
        rear=p;
}
return true;
}//入队             public Object deQueue(){
if(front==rear) return null;
OneLinkNode s=front.getNext();
Object e=s.getData();
s=s.getNext();
if(front.getNext()==null)
rear=front;
return e;
} //出队

           

解决方案 »

  1.   

    ==左边当然必须是变量,你那get方法获得的是具体值当然不行了!
      

  2.   

    s.getNext()是一个方法,并不是一个属性。
    如果是属性的话,你可以直接:s.next=...
    用java编程的话,别有这种指针的思想。
    java里的做法是:
    s.setNext(s.getNext().getNext())
      

  3.   

    写一个例子,参考一下吧!
    public class Node {    private int value;
        private Node next;    public Node(int value) {
            this.value = value;
        }
        
        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
        public int getValue() {
            return value;
        }    public void setValue(int value) {
            this.value = value;
        }    public Node getNext() {
            return next;
        }    public void setNext(Node next) {
            this.next = next;
        }
        
        public String toString() {
            return String.valueOf(value);
            
        }
    }public class Seqence {    public static void main(String[] args) {
            Node root = new Node(1);
            root.setNext(new Node(2));
            root.getNext().setNext(new Node(3));
            root.getNext().getNext().setNext(new Node(4));
            
            Node item = root;
            
            while(item != null) {
                System.out.println(item);
                item = item.getNext();
            }
        }
    }
      

  4.   

    同意,如果向你写得那样应该写一个SET的方法并且这么用s.setNext(s.getNext().getNext());