在java实现自定义链表(单向链表)
2008-10-07 09:58
441 查看
import java.io.*;
import com.toplx.game.utils.Node;
public class List {
/* 用变量来实现表头 */
private Node Head = null;
private Node Tail = null;
private Node Pointer = null;
private int Length = 0;
/* 清空整个链表 */
public void deleteAll() {
Head = null;
Tail = null;
Pointer = null;
Length = 0;
}
/* 链表复位,使第一个结点成为当前结点 */
public void reset() {
Pointer = 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 (cursor() == Tail);
}
/* 返回当前结点的下一个结点的值,并使其成为当前结点 */
public Object nextNode() {
if (Length == 1)
throw new java.util.NoSuchElementException();
else if (Length == 0)
throw new java.lang.NullPointerException();
else {
Node temp = cursor();
Pointer = temp;
if (temp != Tail)
return (temp.next.data );
else
throw new java.util.NoSuchElementException();
}
}
/* 返回当前结点的值 */
public Object currentNode() {
Node temp = cursor();
return temp.data;
}
/* 在当前结点前插入一个结点,并使其成为当前结点 */
public void insert(Object d) {
Node e = new Node(d);
if (Length == 0) {
Tail = e;
Head = e;
} else {
Node temp = cursor();
e.next = temp;
if (Pointer == null)
Head = e;
else
Pointer.next = e;
}
Length++;
}
/* 返回链表的大小 */
public int size() {
return (Length);
}
/**
* 将当前结点移出链表,下一个结点
* 成为当前结点,如果移出的结点是
* 最后一个结点,则第一个结点 成
* 为当前结点
*/
public Object remove() {
Object temp;
if (Length == 0)
throw new java.util.NoSuchElementException();
else if (Length == 1) {
temp = Head.data;
deleteAll();
} else {
Node cur = cursor();
temp = cur.data;
if (cur == Head)
Head = cur.next;
else if (cur == Tail) {
Pointer.next = null;
Tail = Pointer;
reset();
} else
Pointer.next = cur.next;
Length--;
}
return temp;
}
/* 返回当前结点的指针 */
private Node cursor() {
if (Head == null)
throw new java.lang.NullPointerException();
else if (Pointer == null)
return Head;
else
return Pointer.next;
}
public class Node
{
public Object data;
public Node next;
public Node(Object d)
{
data = d;
next = null;
}
}
/* 链表的简单应用举例 */
public static void main(String[] args) {
List a = new List();
for (int i = 1; i <= 10; i++)
a.insert(new Integer(i));
System.out.println(a.currentNode());
while (!a.isEnd())
System.out.println(a.nextNode());
a.reset();
while (!a.isEnd()) {
a.remove();
}
a.remove();
a.reset();
if (a.isEmpty())
System.out.println("There is no Node in List /n");
System.out.println(" You can press return to quit/n");
try {
System.out.println("I am waitting for you :) >>>>>>");
System.in.read(); // 确保用户看清程序运行结果
System.out.println("exit success!");
} catch (IOException e) {
}
}
}
import com.toplx.game.utils.Node;
public class List {
/* 用变量来实现表头 */
private Node Head = null;
private Node Tail = null;
private Node Pointer = null;
private int Length = 0;
/* 清空整个链表 */
public void deleteAll() {
Head = null;
Tail = null;
Pointer = null;
Length = 0;
}
/* 链表复位,使第一个结点成为当前结点 */
public void reset() {
Pointer = 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 (cursor() == Tail);
}
/* 返回当前结点的下一个结点的值,并使其成为当前结点 */
public Object nextNode() {
if (Length == 1)
throw new java.util.NoSuchElementException();
else if (Length == 0)
throw new java.lang.NullPointerException();
else {
Node temp = cursor();
Pointer = temp;
if (temp != Tail)
return (temp.next.data );
else
throw new java.util.NoSuchElementException();
}
}
/* 返回当前结点的值 */
public Object currentNode() {
Node temp = cursor();
return temp.data;
}
/* 在当前结点前插入一个结点,并使其成为当前结点 */
public void insert(Object d) {
Node e = new Node(d);
if (Length == 0) {
Tail = e;
Head = e;
} else {
Node temp = cursor();
e.next = temp;
if (Pointer == null)
Head = e;
else
Pointer.next = e;
}
Length++;
}
/* 返回链表的大小 */
public int size() {
return (Length);
}
/**
* 将当前结点移出链表,下一个结点
* 成为当前结点,如果移出的结点是
* 最后一个结点,则第一个结点 成
* 为当前结点
*/
public Object remove() {
Object temp;
if (Length == 0)
throw new java.util.NoSuchElementException();
else if (Length == 1) {
temp = Head.data;
deleteAll();
} else {
Node cur = cursor();
temp = cur.data;
if (cur == Head)
Head = cur.next;
else if (cur == Tail) {
Pointer.next = null;
Tail = Pointer;
reset();
} else
Pointer.next = cur.next;
Length--;
}
return temp;
}
/* 返回当前结点的指针 */
private Node cursor() {
if (Head == null)
throw new java.lang.NullPointerException();
else if (Pointer == null)
return Head;
else
return Pointer.next;
}
public class Node
{
public Object data;
public Node next;
public Node(Object d)
{
data = d;
next = null;
}
}
/* 链表的简单应用举例 */
public static void main(String[] args) {
List a = new List();
for (int i = 1; i <= 10; i++)
a.insert(new Integer(i));
System.out.println(a.currentNode());
while (!a.isEnd())
System.out.println(a.nextNode());
a.reset();
while (!a.isEnd()) {
a.remove();
}
a.remove();
a.reset();
if (a.isEmpty())
System.out.println("There is no Node in List /n");
System.out.println(" You can press return to quit/n");
try {
System.out.println("I am waitting for you :) >>>>>>");
System.in.read(); // 确保用户看清程序运行结果
System.out.println("exit success!");
} catch (IOException e) {
}
}
}
相关文章推荐
- 用java实现的自定义单向链表
- java 自定义单向链表实现“反序”输出
- [Java算法分析与设计]单向链表(List)的实现和应用
- java实现单向链表CRUD,反转,排序,查找倒数第k个元素,递归输出等操作
- java实现单向链表的增、删、改、查
- Java 单向链表和单向循环链表的代码实现
- java 单向链表实现
- 基于链表实现Java 自定义Stack栈
- 基于链表实现Java 自定义Queue队列
- java实现数据结构之单向链表
- Java模拟单向链表和双向链表的实现
- Java和c++类实现单向链表
- 数据结构Java实现03----单向链表的插入和删除
- 数据结构Java实现03----单向链表的插入和删除
- java单向链表基本操作简单实现
- 线性表的Java实现--链式存储(单向链表)
- JAVA实现单向链表反转2
- Java实现单向链表反转
- 自定义单向链表实现(详细)
- 数据结构与算法分析笔记与总结(java实现)--链表10:输入某个结点删除单向链表中的该结点