java 双链表实现_用JAVA实现双链表

用JAVA实现双链表

package core;

/*

* 我们可以用类ListLinked来实现链表结构,用变量Head、Tail、Length、Pointer来实现表头。

* 存储当前结点的指针时有一定的技巧,Pointer并非存储指向当前结点的指针,而是存储指向它的前趋结点的指针,

* 当其值为null时表示当前结点是第一个结点。那么为什么要这样做呢?

* 这是因为当删除当前结点后仍需保证剩下的结点构成链表,如果Pointer指向当前结点,则会给操作带来很大困难。

* 那么如何得到当前结点呢,我们定义了一个方法cursor(),返回值是指向当前结点的指针。

* 类ListLinked还定义了一些方法来实现对链表的基本操作,通过运用这些基本操作我们可以对链表进行各种操作。

* 例如reset()方法使第一个结点成为当前结点。

* insert(Object d)方法在当前结点前插入一个结点,并使其成为当前结点。

* remove()方法删除当前结点同时返回其内容,并使其后继结点成为当前结点,

* 如果删除的是最后一个结点,则第一个结点变为当前结点。

*/

public class ListLinked {

/* 用变量来实现表头 */

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 static void main(String[] args)

/* 链表的简单应用举例 */

{

ListLinked a = new ListLinked();

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");

}

}

class Node {

/* 构成链表的结点定义 */

Object data;

Node next;

Node(Object d) {

data = d;

next = null;

}

}

[此贴子已经被作者于2007-6-2 17:03:50编辑过]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的双向链表实现通讯录的 Java 代码: ```java public class Contact { private String name; private String phone; private Contact prev; private Contact next; public Contact(String name, String phone) { this.name = name; this.phone = phone; this.prev = null; this.next = null; } public void setPrev(Contact prev) { this.prev = prev; } public void setNext(Contact next) { this.next = next; } public Contact getPrev() { return prev; } public Contact getNext() { return next; } public String getName() { return name; } public String getPhone() { return phone; } } public class ContactList { private Contact head; private Contact tail; public ContactList() { head = null; tail = null; } public void addContact(String name, String phone) { Contact newContact = new Contact(name, phone); if (head == null) { head = newContact; tail = newContact; } else { tail.setNext(newContact); newContact.setPrev(tail); tail = newContact; } } public void removeContact(String name) { Contact current = head; while (current != null) { if (current.getName().equals(name)) { if (current == head) { head = current.getNext(); if (head != null) { head.setPrev(null); } } else if (current == tail) { tail = current.getPrev(); if (tail != null) { tail.setNext(null); } } else { current.getPrev().setNext(current.getNext()); current.getNext().setPrev(current.getPrev()); } break; } current = current.getNext(); } } public void printContacts() { Contact current = head; while (current != null) { System.out.println(current.getName() + " - " + current.getPhone()); current = current.getNext(); } } } ``` 这个实现中,`Contact` 类表示一个联系人,包含姓名、电话号码和前后指针。`ContactList` 类表示一个联系人列表,维护头尾指针,并提供添加、删除和打印联系人的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值