本帖最后由 dollyn 于 2010-03-31 15:01:32 编辑

解决方案 »

  1.   

    Link里全是问题;改好的代码如下package csdn;public class Link
    {
        private Node first = null;//链表表头
        
        public Link()
        {
            this.first = null;
        }
        
        public boolean isEmpty()
        {
            if(this.first == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void insertHeadNode(int data)
        {
            Node node = new Node(data);
            if(this.first == null)
            {
                this.first = node;
            }
            else
            {
                Node tempNode = this.first;
                this.first = node;
                this.first.next = tempNode;            
            }
        }
        public Node deleteHeadNode()
        {
            if(this.first == null)
            {
                return null;
            }
            else
            {
                Node head = this.first;
                Node next = this.first.next;
                this.first = next;
                head = null;
                return head;
            }
        }
        public Node findNode(Node node, int k)
        {
            if(this.first == null)
            {
                System.out.println("this link is empty!");
                return null;
            }
            else
            {
                if(node.data == k)
                {
                    return node;
                }
                else
                {
                    node = node.next;
                    return this.findNode(node, k);
                }
            }
        }
        public void displayLink (Node node)
        {
         if(this.first == null)
            {
                System.out.println("this link is empty!");
                return;
            }        if(node != null)
            {
                node.NodeDisplay();
                node = node.next;
                this.displayLink(node);
            }
        }
        
        public static void main(String[] args)
        {
            // TODO Auto-generated method stub
            Link aaa = new Link();
            aaa.insertHeadNode(4);
            aaa.insertHeadNode(6);
            aaa.insertHeadNode(8);
            aaa.insertHeadNode(45);
            aaa.insertHeadNode(876);
    //        aaa.deleteHeadNode();
            aaa.displayLink(aaa.first);
            Node node = aaa.findNode(aaa.first, 8);
            System.out.println(node.data + "  next = " + node.next.data);
            
        }}
      

  2.   

    Link里全是问题,改好的代码如下package csdn;public class Link
    {
        private Node first = null;//链表表头
        
        public Link()
        {
            this.first = null;
        }
        
        public boolean isEmpty()
        {
            if(this.first == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void insertHeadNode(int data)
        {
            Node node = new Node(data);
            if(this.first == null)
            {
                this.first = node;
            }
            else
            {
                Node tempNode = this.first;
                this.first = node;
                this.first.next = tempNode;            
            }
        }
        public Node deleteHeadNode()
        {
            if(this.first == null)
            {
                return null;
            }
            else
            {
                Node head = this.first;
                Node next = this.first.next;
                this.first = next;
                head = null;
                return head;
            }
        }
        public Node findNode(Node node, int k)
        {
            if(this.first == null)
            {
                System.out.println("this link is empty!");
                return null;
            }
            else
            {
                if(node.data == k)
                {
                    return node;
                }
                else
                {
                    node = node.next;
                    return this.findNode(node, k);
                }
            }
        }
        public void displayLink (Node node)
        {
         if(this.first == null)
            {
                System.out.println("this link is empty!");
                return;
            }        if(node != null)
            {
                node.NodeDisplay();
                node = node.next;
                this.displayLink(node);
            }
        }
        
        public static void main(String[] args)
        {
            // TODO Auto-generated method stub
            Link aaa = new Link();
            aaa.insertHeadNode(4);
            aaa.insertHeadNode(6);
            aaa.insertHeadNode(8);
            aaa.insertHeadNode(45);
            aaa.insertHeadNode(876);
    //        aaa.deleteHeadNode();
            aaa.displayLink(aaa.first);
            Node node = aaa.findNode(aaa.first, 8);
            System.out.println(node.data + "  next = " + node.next.data);
            
        }}