Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1726364
  • 博文数量: 347
  • 博客积分: 9328
  • 博客等级: 中将
  • 技术积分: 2680
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 23:45
文章分类

全部博文(347)

文章存档

2016年(1)

2013年(4)

2012年(207)

2011年(85)

2010年(50)

分类:

2010-07-30 00:19:23

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;
        }
    }
}


阅读(1265) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:PowerDesigner属性设置笔记

给主人留下些什么吧!~~