数据结构代码-双向链表

/*
 * 文 件 名:  DoublyLink.java
 * 描    述:  双向链表节点
 * 修改时间:  2012-11-22
 */
package linklist;

/**
 * 双向链表的节点
 */
public class DoublyLink
{
    int iData;
    double dData;
    public DoublyLink previous;
    public DoublyLink next;
    
    public DoublyLink(int iData, double dData)
    {
        this.iData = iData;
        this.dData = dData;
        previous = null;
        next = null;
        
    }
    
    public void displayLink()
    {
        System.out.println("{" + iData + "," + dData + "}");
    }
}
/*
 * 文 件 名:  DoublyLinkList.java
 * 描    述:  双向链表
 */
package linklist;

/**
 * 双向链表(一般都是双端链表,可以反向遍历)
 */
public class DoublyLinkList
{
    private DoublyLink first;
    private DoublyLink last;
    
    /**
     * <默认构造函数>
     */
    public DoublyLinkList()
    {
        first = null;
        last = null;
    }
    
    /**
     * 判断双向链表是否为空
     * @return
     * @see [类、类#方法、类#成员]
     */
    public boolean isEmpty()
    {
        return first == null;
    }
    
    /**
     * 正向遍历双向链表
     * @see [类、类#方法、类#成员]
     */
    public void displayForward()
    {
        if (isEmpty())
        {
            System.out.println("空链表.");
        }
        else
        {
            DoublyLink current = first;
            while (current != null)
            {
                current.displayLink();
                current = current.next;
            }
        }
    }
    
    /**
     * 反向遍历双向链表
     * @see [类、类#方法、类#成员]
     */
    public void displayBackward()
    {
        if (isEmpty())
        {
            System.out.println("空链表.");
        }
        else
        {
            DoublyLink current = last;
            while (current != null)
            {
                current.displayLink();
                current = current.previous;
            }
        }
    }
    
    /**
     * 在链表头部插入一个元素
     * @see [类、类#方法、类#成员]
     */
    public void insertFirst(int iData, double dData)
    {
        DoublyLink link = new DoublyLink(iData, dData);
        if (isEmpty())
        {
            first = link;
            last = link;
        }
        else
        {
            link.next = first;
            first.previous = link;
            first = link;
        }
    }
    
    /**
     * 在链表尾部插入一个元素
     * @see [类、类#方法、类#成员]
     */
    public void insertLast(int iData, double dData)
    {
        DoublyLink link = new DoublyLink(iData, dData);
        if (isEmpty())
        {
            first = link;
            last = link;
        }
        else
        {
            last.next = link;
            link.previous = last;
            last = link;
        }
    }
    
    /**
     * 根据关键字查找到某个节点,可以从链表头部开始查找,也可以从链表尾部开始查找,这里使用头部查找方式
     * @param iData
     * @return
     * @see [类、类#方法、类#成员]
     */
    public DoublyLink find(int iData)
    {
        DoublyLink current = first;
        while (current != null)
        {
            if (current.iData == iData)
            {
                break;
            }
            current = current.next;
        }
        return current;
    }
    
    /**
     * 在指定元素后插入元素
     * @param iData
     * @see [类、类#方法、类#成员]
     */
    public void insertAfter(int key, int iData, double dData)
    {
        DoublyLink link = find(key);
        if (link != null)
        {
            DoublyLink newLink = new DoublyLink(iData, dData);
            if (link == last)
            {
                last.next = newLink;
                newLink.previous = last;
                last = newLink;
            }
            else
            {
                newLink.next = link.next;
                newLink.previous = link;
                link.next.previous = newLink;
                link.next = newLink;
            }
        }
    }
    
    /**
     * 删除表头元素
     * @return
     * @throws EmptyLinkListException 
     * @see [类、类#方法、类#成员]
     */
    public DoublyLink deleteFirst()
        throws EmptyLinkListException
    {
        DoublyLink temp = null;
        if (isEmpty())
        {
            throw new EmptyLinkListException();
        }
        else if (first.next == null)
        {
            temp = first;
            first = null;
            last = null;
        }
        else
        {
            temp = first;
            first.next.previous = null;
            first = first.next;
        }
        return temp;
    }
    
    /**
     * 删除表尾元素
     * @return
     * @throws EmptyLinkListException
     * @see [类、类#方法、类#成员]
     */
    public DoublyLink deleteLast()
        throws EmptyLinkListException
    {
        DoublyLink temp = null;
        if (isEmpty())
        {
            throw new EmptyLinkListException();
        }
        else if (last.previous == null)
        {
            temp = last;
            first = null;
            last = null;
        }
        else
        {
            temp = last;
            last.previous.next = null;
            last = last.previous;
        }
        return temp;
    }
    
    /**
     * 根据指定关键字删除元素
     * @param key
     * @return
     * @see [类、类#方法、类#成员]
     */
    public DoublyLink deleteKey(int key)
    {
        DoublyLink temp = find(key);
        if (temp != null)
        {
            if (temp.previous == null)
            {
                try
                {
                    deleteFirst();
                }
                catch (EmptyLinkListException e)
                {
                    e.printStackTrace();
                }
            }
            else if (temp.next == null)
            {
                try
                {
                    deleteLast();
                }
                catch (EmptyLinkListException e)
                {
                    e.printStackTrace();
                }
            }
            else
            {
                temp.previous.next = temp.next;
                temp.next.previous = temp.previous;
            }
        }
        return temp;
    }
}
/*
 * 文 件 名:  DoublyLinkListTest.java
 * 描    述:  双向链表测试类
 */
package linklist;

/**
 * 双向链表测试类
 */
public class DoublyLinkListTest
{
    
    /** 主函数
     * @param args
     * @see [类、类#方法、类#成员]
     */
    public static void main(String[] args)
    {
        DoublyLinkList doublyLinkList = new DoublyLinkList();
        doublyLinkList.insertFirst(1, 1.1);
        doublyLinkList.insertFirst(2, 2.2);
        doublyLinkList.insertFirst(3, 3.3);
        doublyLinkList.insertFirst(4, 4.4);
        try
        {
            doublyLinkList.deleteFirst();
            doublyLinkList.deleteLast();
            doublyLinkList.deleteKey(2);
        }
        catch (EmptyLinkListException e)
        {
            e.printStackTrace();
        }
        doublyLinkList.displayBackward();
    }
    
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值