java双向链表_Java数据结构——双向链表

什么是双向链表?

每一个结点不仅配有next引用,同时还有一个prev引用,指向其上一个结点(前驱结点), 没有前驱的时候就为NULL。

604f48dac10ed8f54b284e9cb103600a.png

(以下图片均来自网络,侵删)

与单链表的区别?

和单向链表相比有以下优势:

插入删除不需要移动元素外,可以原地插入删除

可以双向遍历

插入操作

034268d744d046c86abf65c4c347d53a.png

删除操作

e659e4ffbdf31edc254a9937cfb29f74.png

实现

public class DLNode {

Object data;

DLNode prev;

DLNode next;

static DLNode head;

static DLNode tail;

// 无参构造

public DLNode() {

super();

}

// 有参构造

public DLNode(Object data, DLNode prev, DLNode next) {

super();

this.data = data;

this.prev = prev;

this.next = next;

}

// 获取数据

public Object getData() {

return data;

}

// 修改数据

public void setData(Object data) {

this.data = data;

}

// 得到前驱结点

public DLNode getPrev() {

return prev;

}

// 修改前驱结点

public void setPrev(DLNode prev) {

this.prev = prev;

}

// 获取后驱结点

public DLNode getNext() {

return next;

}

// 修改后驱结点

public void setNext(DLNode next) {

this.next = next;

}

// 获取长度

public static int getLength() {

int count = 0;

for (DLNode n = head; n != null; n = n.next) {

count++;

}

return count;

}

// 插入结点

public static void add(Object data, int index) {

DLNode node = new DLNode(data, null, null);

if (index == 0) {

node.next = head;

node.prev = null;

head = node;

} else if (index == getLength()) {

tail.next = node;

node.prev = tail;

tail = node;

} else {

int temp = 0;

for (DLNode n = head; n != null; n = n.next) {

temp++;

if (temp == index) {

node.next = n.next;

n.next.prev = node;

n.next = node;

node.prev = n;

break;

}

}

}

}

//删除结点

public static void remove(int index) {

if (index == 0) {

head = head.next;

head.prev = null;

} else if (index == getLength() - 1) {

tail = tail.prev;

tail.next = null;

} else if (index >= getLength()) {

System.out.println("超出链表长度");

System.exit(0);

} else {

int temp = 0;

for (DLNode n = head; n != null; n = n.next) {

temp++;

if (temp == index) {

n.next = n.next.next;

n.next.prev = n;

break;

}

}

}

}

public static void main(String[] args) {

DLNode node1 = new DLNode("aaa", null, null);

DLNode node2 = new DLNode("bbb", node1, null);

DLNode node3 = new DLNode("ccc", node2, null);

DLNode node4 = new DLNode("ddd", node3, null);

node3.setNext(node4);

node2.setNext(node3);

node1.setNext(node2);

head = node1;

tail = node4;

System.out.print("当前链表:");

for (DLNode n = head; n != null; n = n.next) {

System.out.print(n.data + " ");

}

System.out.println();

System.out.println("链表长度:" + getLength());

add("eee", 4);

System.out.print("插入后链表:");

for (DLNode n = head; n != null; n = n.next) {

System.out.print(n.data + " ");

}

System.out.println();

remove(0);

System.out.print("删除后链表:");

for (DLNode n = head; n != null; n = n.next) {

System.out.print(n.data + " ");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向链表是一种常见的数据结构,与单向链表相比,它可以在节点之间进行双向遍历。在Java中,我们可以使用类来实现双向链表。 下面是一个简单的Java程序,演示如何创建和输出双向链表: ```java // 双向链表节点类 class Node { public int data; public Node prev; public Node next; public Node(int data) { this.data = data; this.prev = null; this.next = null; } } // 双向链表类 class DoubleLinkedList { public Node head; public Node tail; public DoubleLinkedList() { this.head = null; this.tail = null; } // 在链表头部插入节点 public void insertAtHead(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; tail = newNode; return; } newNode.next = head; head.prev = newNode; head = newNode; } // 在链表尾部插入节点 public void insertAtTail(int data) { Node newNode = new Node(data); if (tail == null) { head = newNode; tail = newNode; return; } newNode.prev = tail; tail.next = newNode; tail = newNode; } // 输出链表 public void printList() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } } // 测试类 public class Main { public static void main(String[] args) { DoubleLinkedList list = new DoubleLinkedList(); list.insertAtHead(1); list.insertAtHead(2); list.insertAtTail(3); list.insertAtTail(4); list.printList(); // 输出:2 1 3 4 } } ``` 在上面的程序中,我们首先定义了一个`Node`类来表示双向链表的每个节点,其中包含了当前节点的值、前一个节点和后一个节点。接着,我们定义了`DoubleLinkedList`类来表示整个双向链表,其中包含了头节点和尾节点。 在`DoubleLinkedList`类中,我们定义了`insertAtHead`和`insertAtTail`方法来在链表头部和尾部插入节点,分别需要创建一个新节点,并将它与当前链表的头节点或尾节点进行连接。 最后,我们定义了`printList`方法来输出整个链表,只需要从链表的头节点开始遍历,依次输出每个节点的值即可。 在`Main`类中,我们通过`DoubleLinkedList`类创建了一个双向链表,并向其中插入了四个节点。最后,我们调用`printList`方法输出整个链表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值