[Java]单项链表与双端链表[原]

package com.black.datastructures.link;

/**
 * 链接点
 *
 * @author BlackWinter
 *
 * @date 2010-7-7 下午10:45:24
 *
 * @file com.black.datastructures.link.Link.java
 *
 * @version 1.0
 */

public class Link {
    public int key;

    public double value;

    public Link next;

    public Link(int id, double date) {
        this.key = id;
        value = date;
    }

    public void displayLink() {
        System.out.printf("{%s,%s}\n", key, value);
    }
}


package com.black.datastructures.link;

/**
 * 单向链表
 *
 * @author BlackWinter
 *
 * @date 2010-7-7 下午10:35:10
 *
 * @file com.black.datastructures.link.LinkList.java
 *
 * @version 1.0
 */

public class LinkList {
    private Link first;

    public LinkList() {
        first = null;
    }

    public boolean isEmpty() {
        return first == null;
    }

    /* 在链表头部插入新节点 */
    public void insertFirst(int id, double date) {
        Link link = new Link(id, date);
        link.next = first;
        first = link;
    }

    /* 删除链表的头部节点 */
    public Link deleteFirst() {
        Link temp = first;
        first = first.next;
        return temp;
    }

    /* 根据key查询指定的链接点 */
    public Link find(int key) {
        Link current = first;
        while (current.key != key) {
            if (current.next == null)
                return null;
            else
                current = current.next;
        }
        return current;
    }
    
    /* 根据key删除指定节点 */
    public Link delete(int key) {
        Link current = first;
        Link previous = first;
        while (current.key != key) {
            if (current.next == null)
                return null;
            else {
                current = current.next;
                previous = current;
            }
        }
        if (current == first)
            first = first.next;
        else
            previous.next = current.next;
        return current;
    }

    /* 显示链表的所有节点 */
    public void displayList() {
        System.out.println("First --> Last:");
        Link current = first;
        while (null != current) {
            current.displayLink();
            current = current.next;
        }
    }
}


package com.black.datastructures.link;

/**
 * 双端链表
 *
 * @author BlackWinter
 *
 * @date 2010-7-11 下午09:09:12
 *
 * @file com.black.datastructures.link.DouEndLinkList.java
 *
 * @version 1.0
 */

public class DouEndLinkList {
    private Link first;

    private Link last;

    public DouEndLinkList() {
        first = null;
        last = null;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public void insertFirst(int id, double date) {
        Link newLink = new Link(id, date);
        if (isEmpty())
            last = newLink;
        newLink.next = first;
        first = newLink;
    }

    public void insertLast(int id, double date) {
        Link newLink = new Link(id, date);
        if (isEmpty())
            first = newLink;
        else
            last.next = newLink;
        last = newLink;
    }

    public void display() {
        System.out.println("List (first --> last):");

        Link current = first;
        while (null != current) {
            current.displayLink();
            current = current.next;
        }
    }
}


<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(558) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:国外威客网站大盘点

给主人留下些什么吧!~~
评论热议
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值