首页
论坛
图书
开发资料
在线文档
网址
下载
联系我们
 新闻│Java│JavaScript│Eclipse│Eclipse 英文│J2EE│J2ME│J2SE│JSP│Netbeans│Hibernate│JBuilder│Spring│Struts
站内搜索: 请输入搜索关键词

当前页面: 开发资料首页 → Java 专题 → 环形链表实例

环形链表实例

摘要: 环形链表实例

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="211" height="86" align="center" valign="top"> </td> <td width="473" valign="top">

一、链表的节点类


package com.cache;

public class LinkedListNode {

    public LinkedListNode previous;//前一节点

    public LinkedListNode next;//后一节点

    public Object object;//节点中的数据

  

  

    public LinkedListNode(Object object, LinkedListNode next,

            LinkedListNode previous)

    {

        this.object = object;

        this.next = next;

        this.previous = previous;

    }

  

    public void remove() {//从链表中移去

        previous.next = next;

        next.previous = previous;

    }

  

    public String toString() {

        return object.toString();

    }

}

二、链表类


package com.cache;

import java.util.*;

public class LinkedList {

   

    private LinkedListNode head = 
new LinkedListNode("head", null, null); public LinkedList() { head.next = head.previous = head;//头节点 } public LinkedListNode getFirst() {//返回头节点后的第一个节点 LinkedListNode node = head.next; if (node == head) { return null; } return node; } public LinkedListNode getLast() {//返回最后一个节点 LinkedListNode node = head.previous; if (node == head) { return null; } return node; } //将节点加到头节点后 public LinkedListNode addFirst(LinkedListNode node) { node.next = head.next; node.previous = head; node.previous.next = node; node.next.previous = node; return node; } public LinkedListNode addFirst(Object object) { LinkedListNode node =
new LinkedListNode(object, head.next, head); node.previous.next = node; node.next.previous = node; return node; }   //节点添加到链表最后 public LinkedListNode addLast(Object object) { LinkedListNode node =
new LinkedListNode(object, head, head.previous); node.previous.next = node; node.next.previous = node; return node; } public void clear() {//清空链表 //Remove all references in the list. LinkedListNode node = getLast(); while (node != null) { node.remove(); node = getLast(); } //Re-initialize. head.next = head.previous = head; } public String toString() { LinkedListNode node = head.next; StringBuffer buf = new StringBuffer(); while (node != head) { buf.append(node.toString()).append(", "); node = node.next; } return buf.toString(); } }

</td></tr> </table>




</td> </tr> <tr>


↑返回目录
前一篇: 实现HashMap的例子
后一篇: http断点续传简单实现

首页 | 全站 Sitemap | 联系我们 | 设为首页 | 收藏本站
版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved