单链表---删除单链表的节点

package 单链表的删除单链表中的节点;


//Node表示的是一个节点
public class Node {
        public static void main(String[] args) {
            //创建节点
            Node n1=new Node(1);
            Node n2=new Node(2);
            Node n3=new Node(3);
            //追加节点
            //n1->n2->n3
            //n1.oppend(n2);
            //n2.oppend(n3);
            //返回自身的方法就可以一直追加下去-----将原来的public void oppend(Node node)改写为public Node oppend(Node node)+return this
            n1.oppend(n2).oppend(n3).oppend(new Node(4));
//            System.out.println(n1.next().next().next().date);
//            
//            System.out.println(n1.next().isLast());
//            
//            System.out.println(n3.next.isLast());
//            
//            System.out.println(new Node(4).isLast());
            //显示所有节点的信息
            n1.Show();
            //删除n2节点
            n1.removeNext();
            //显示删除后的节点信息
            n1.Show();

            
            
            
        }
        //节点内容
        int date;
        //下一个节点
        Node next;
        
        public Node(int date) 
        {
            this.date=date;
        }
        
        //显示所有节点的信息
        public void Show() 
        {
            Node currentnode = this;
            while(true)
            {
                //取出当前节点
                System.out.print(currentnode.date+" ");
                //取出下一个节点
                currentnode = currentnode.next;
                //如果是最后一个节点
                if(currentnode==null)
                {
                    break;
                }
            }
            System.out.println();
        }
        
        //为节点追加节点
        //最简单的,最麻烦的方法
        //public void oppend(Node node)
        //{
        //    this.next=node;
        //}
        
        //优化方法
        public Node oppend(Node node) 
        {
            //当前节点
            Node currentnode = this;
            //判断当前节点后面是否还有节点(循环后找)
            while(true)
            {
                //取出下一个节点
                Node nextnode = currentnode.next;
                //如果下一个节点为NULL,即当前节点已经是最后一个节点
                if(nextnode==null)
                {
                    break;
                }
                //赋给当前节点
                currentnode = nextnode;
                
            }
            //把需要追回的节点追加为找到的当前节点的下一个节点
            currentnode.next=node;
            return this;
        }
        
        //删除下一个节点
        public void removeNext()
        {
            //取出下下一个节点  
            Node newNext = next.next;
            //把下下一个节点设置为当前节点的下一个节点
            this.next=newNext;
        }
        

        //获取下一个节点
        public Node next() 
        {
            return this.next;
        }
        
        //获取节点中的数据
        public int getDate() 
        {
            return this.date;
        }
        
        //判断当前节点是否为最后一个节点
        public boolean isLast() 
        {
            return next==null;
        }
}

猜你喜欢

转载自blog.csdn.net/weixin_42133768/article/details/86654735