java_数据结构_链表
来源:互联网 发布:spc数据分析 编辑:程序博客网 时间:2024/06/11 21:34
Java链表类List的源代码如下
/** * @Project: struts2 * @Title: LinkedList.java * @Package com.yza.struct * @author yongzhian * @date 2014-10-8 上午11:43:17 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */package com.yza.struct;import java.util.NoSuchElementException;/** * @ClassName LinkedList * @Description 链表 * @author yongzhian * @Date 2014-10-8 */public class LinkedList {private Node head = null;private Node tail = null;private Node current = null;private int length = 0;/*清空链表*/public void clearAll() {head = null;tail = null;current = null;length = 0;}/*链表复位,使第一个结点成为当前结点*/public void reset() {current = null;}/* 判断链表是否为空 */public boolean isEmpty() {return (length == 0);}/* 判断当前结点是否为最后一个结点 */public boolean isEnd() {if (length == 0) {throw new java.lang.NullPointerException();} else if (length == 1) {return true;} else {return (current == tail);}} /*返回当前结点的下一个结点的值,并使其成为当前结点*/ public Node nextNode() {if(this.length==1){throw new NoSuchElementException();}else if(length==0) {throw new java.lang.NullPointerException(); }else { this.current = this.current.next; return this.current;}}public void insert(int d) {Node n = new Node(d);if(this.length == 0 ){this.head = n;this.tail = n;this.current = n;}else{n.next = this.current.next;this.current = n;} this.length++;} /* (non-Javadoc) * <p>Title: toString</p> * <p>Description: 方法描述</p> * @return * @see java.lang.Object#toString() */@Overridepublic String toString() {// TODO Auto-generated method stubreturn "head:"+this.head+ " tail:"+this.tail +" current :"+this.current +" "+ this.length;}public static void main(String[] args) {LinkedList ll = new LinkedList();if(ll.isEmpty()){System.out.println(ll +" isEmpty " +ll.isEmpty()); }else{System.out.println(ll +" isEnd" +ll.isEnd()); }ll.insert(3);if(ll.isEmpty()){System.out.println(ll +" isEmpty " +ll.isEmpty()); }else{System.out.println(ll +" isEnd" +ll.isEnd()); }ll.insert(4);System.out.println(ll);ll.insert(5);if(ll.isEmpty()){System.out.println(ll +" isEmpty " +ll.isEmpty()); }else{System.out.println(ll +" isEnd" +ll.isEnd()); }}}class Node {int value;Node next;Node(int value) {this.value = value;next = null;}/* (non-Javadoc) * <p>Title: toString</p> * <p>Description: 方法描述</p> * @return * @see java.lang.Object#toString() */@Overridepublic String toString() {// TODO Auto-generated method stubreturn " "+ this.value;}}双向链表可以用类似的方法实现只是结点的类增加了一个指向前趋结点的指针。
class Node {int value;Node next;Node previous; Node(int value) {this.value = value;next = null;previous=null; }/* (non-Javadoc) * <p>Title: toString</p> * <p>Description: 方法描述</p> * @return * @see java.lang.Object#toString() */@Overridepublic String toString() {// TODO Auto-generated method stubreturn " "+ this.value;}
0 0
- java_数据结构_链表
- 数据结构2—Java_栈_顺序存储
- Java_多线程_简介
- java_基础_包
- java_基础_杂
- java_基础_集合
- java_基础_泛型
- java_基础_异常
- java_基础_线程
- Java_数组_基本概念
- Java_类_多态
- java_排序_查找
- java_函数_更新
- JAVA_界面_更新
- java_高级_多线程
- java_集合_总结
- java_百度_面试
- 数据结构_链表
- hashMap排序
- memory pool
- NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
- ArcMap 学习资料:使用手册、编辑手册
- 一种Java对象与XML文互转的方法
- java_数据结构_链表
- Openstack, AWS, HyperV, VMware
- UIView详解2
- 【汇总】EAGLE-lab papper
- Function Reference/wp loginout
- Oracle 只读-读写表空间 三种情况详解
- 网络基本功(三):细说VLAN与Trunk
- Spring循环引用和AOP代理引发的两个问题
- 新的开始