java链表内容是对象_链表_JAVA实现

链表_JAVA实现

1.ListNode.JAVA

/**

*

*/

基本数据结构;

/**

* @author greatwqs

*

*/

public class ListNode {

Object data;

ListNode next;

ListNode(Object d) {

data = d;

next = null;

}

}

2.链表的类MyList.JAVA

基本数据结构;

/**

* @author greatwqs

*

*/

public class MyList {

// 用变量来实现表头

private ListNode Head = null;

private ListNode Tail = null;

private ListNode 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 nextListNode() {

if (Length == 1)

throw new java.util.NoSuchElementException();

else if (Length == 0)

throw new java.lang.NullPointerException();

else {

ListNode temp = cursor();

Pointer = temp;

if (temp != Tail)

return (temp.next.data);

else

throw new java.util.NoSuchElementException();

}

}

/** 返回当前结点的值 */

public Object currentListNode() {

ListNode temp = cursor();

return temp.data;

}

/** 在当前结点前插入一个结点, 并使其成为当前结点 */

public void insert(Object d) {

ListNode e = new ListNode(d);

if (Length == 0) {

Tail = e;

Head = e;

} else {

ListNode 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 {

ListNode 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 ListNode cursor() {

if (Head == null)

throw new java.lang.NullPointerException();

else if (Pointer == null)

return Head;

else

return Pointer.next;

}

}

3,链表的测试类MyListTest.JAVA

/**

*

*/

基本数据结构;

import java.io.IOException;

/**

* @author greatwqs

*

*/

public class MyListTest {

public static void main(String[] args) {

MyList a = new MyList();

for (int i = 1; i <= 10; i++)

a.insert(new Integer(i));

System.out.println(a.currentListNode());

while (!a.isEnd())

System.out.println(a.nextListNode());

a.reset();

while (!a.isEnd()) {

a.remove();

}

a.remove();

a.reset();

if (a.isEmpty())

System.out.println("There is no ListNode in List \n");

System.out.println("You can press return to quit\n");

try {

// 确保用户看清程序运行结果

System.in.read();

} catch (IOException e) {

}

}

}

===========================================

运行结果显示

==========================================

10

9

8

7

6

5

4

3

2

1

There is no ListNode in List

You can press return to quit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值