数据结构-双端链表


/*
 * 双端链表
 * 
 * 双端链表与传统链表非常相似,
 * 但它有一个新特性:即对最后一个链接点的引用
 */

public class LinkedList
{
    private Node first ;//表首
    private Node last ;//表尾
    private Node newNode ;//要插入的新节点
    
    //将first,last赋为null,其实并不是必须的,引用类型在创建之初会自动赋为null
    //这里为了清晰起见,将其赋为null
    public LinkedList ()
    {
        first = null ;
        last  = null ;
    }
    
    //判断当前链表是否为空
    public boolean isEmpty ()
    {
        return first == null ;
    }
    
    //从链表表首插入数据
    public void insertFirst ( int data )
    {
        //要插入的新节点
        newNode = new Node (data) ;
        
        //如果条件成立说明链表里什么都没有
        if ( isEmpty () == true )
        {
            last = newNode ;//链表里啥都没有,所以首尾都是当前这个节点啦
        }
        //不管上面的if是否成立,下面的代码都会执行 即:向表首插节点
        
        newNode.next = first ; //新节点的next指向原first;如果原first为null,那么next也为null
        first = newNode ;//first指向新节点
    }
    
    //从链表表尾插入数据
    public void insertLast ( int data )
    {
        newNode = new Node ( data ) ; 
        
        if ( isEmpty () == true )
        {
            //链表里都是空的,首节点当然也是新节点啦!
            first = newNode ;
        }
        else
        {
            /*
             * 这里为什么要加else?
             * 
             * 答:预防链表为空时,使用了insertLast(),
             * 这时last为null,所以last.next = newNode ; ERROR!!!
             */
            last.next = newNode ;//将原last的next指向新节点
        }
        last = newNode ;//新节点成为吊车尾了=_=
    }
    
    //从表首删除一个数据
    public int deleteFirst ()
    {
        Node temp = first ;//存放要被删除的first ,以便最后返回其数据
        
        //如果条件成立,说明链表里就这一个“独苗”
        if ( first.next == null )
        {
            last = null ;
        }
        
        first = first.next ;
        
        return temp.data ;
    }
    
    //以链接点中的内容为索引,修改链接点的数据
    public void update ( int data , int udata )
    {
        Node current = first ; //current用来记录遍历到哪儿了
        
        //遍历链表
        while ( current != null )
        {
            //找到要修改数据的那个链节点
            if ( current.data == data )
            {
                current.data = udata ;
                System.out.println ( "已修改" );
                return ;    //return:代表方法到此终止
            }
            current = current.next ;
        }
        //如果跳出循环,执行到这,说明没有找到要修改的节点
        System.out.println ( "对不起,没有找到你想要修改的数据" );
    }
    
    //遍历整个链表
    public void display ()
    {
        Node current = first ;
        
        while ( current != null )
        {
            System.out.print ( current.data + " " );
            current = current.next ;
        }
        System.out.println ();
    }
    
}



----------------------------------------------------

package cn.itcast.entity;

public class Node
{
	public int data ;
	public Node next ;//指向后一个链接点
	
	public Node ( int data )
	{
		this.data = data ;
	}
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值