java双向链表创建_双向链表(java实现)

相比于单项链表,双向链表有一个前驱指针,指示当前节点的直接前驱, 这样在查找前驱时更加方面,时间复杂度O(1), 而单链表要查找前驱则需要重新从头遍历直至i-1位置找到前驱节点,时间复杂度为O(n)。

双向链表单个节点的结构如下图所示;

e324764573bd

双向链表节点

当双向链表为空表是,仅有一个头节点,且头节点的前驱和后继指针都指向其自身;

e324764573bd

空表head.png

在非空表中,每个节点都有自己的前驱和后继,最后一个节点的后继为头节点,头节点的前驱为最后一个节点;

e324764573bd

非空表.png

根据以上逻辑,用java实现双链表如下;

/**

* circular list.

* @param element type

*/

public class CircularNodeList {

private int size;

private Node2 head = new Node2<>();

/**

* constructor. create an empty list.

*/

public CircularNodeList() {

size = 0;

head.next = head;

head.previous = head;

}

/**

* identify if list is empty.

* @return true if empty

*/

public boolean isEmpty() {

return head.next == head && head.previous == head;

}

/**

* find element node by specific position.

* @param position position specific

* @return the node

*/

public Node2 find(int position) {

if (position < 1 || position > size) {

throw new IndexOutOfBoundsException("position should between 1 to " + size);

}

Node2 current = head;

int idx = 0;

while (idx < position) {

current = current.next;

idx++;

}

if (current != head) {

return current;

}

return null;

}

/**

* insert at list tail.

* @param data data of the node to be insert

*/

public void insert(T data) {

insert(data, size + 1);

}

/**

* insert new node at specific position.

* @param data new node data

* @param position specific position

*/

public void insert(T data, int position) {

Node2 newNode = new Node2<>();

newNode.data = data;

// if it is to be insert at list tail

if (position == size + 1) {

newNode.previous = head.previous.next;

head.previous.next = newNode;

newNode.next = head;

head.previous = newNode;

size++;

return;

}

// if it is to be insert in the existing list.

Node2 node = find(position);

if (node != null) {

node.previous.next = newNode;

newNode.previous = node.previous;

node.previous = newNode;

newNode.next = node;

size++;

}

}

/**

* delete node at specific position from list

* @param position specific position

* @return the data deleted

*/

public T delete(int position) {

T data = null;

if (position < 1 || position > size) {

throw new IndexOutOfBoundsException("position should between 1 to " + size);

}

Node2 node = find(position);

if (node != null) {

node.previous.next = node.next;

node.next.previous = node.previous;

size--;

data = node.data;

}

return data;

}

/**

* get the prior of the specific position node.

* @param position specific position

* @return the prior of the specific node

*/

public Node2 getPrior(int position) {

Node2 node = find(position);

return node.previous;

}

/**

* get the next of the specific position node.

* @param position specific position

* @return the next of th specific node

*/

public Node2 getNext(int position) {

Node2 node = find(position);

return node.next;

}

public int getSize() {

return this.size;

}

}

class Node2 {

T data;

Node2 next;

Node2 previous;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值