双向链表DoublyLinkedList类

双向链表可以往前寻找数据项:单向链表寻值时若错过了值,需要从头开始。

双向链表多了一个tail值,尾巴;

***向双向链表插入一个值:

this.insert=function(position,  element){

if(position>=0&&position<this.length){
var node=new Node(element);  // 先插入一个值,马上再把这个值和链表连接在一起;

var current=head;

previous;

var index=0;

  if(position==0)  //插入第一个位置

{

if(!head){ //如果列表为空

node=head;

node=tail;

}else{  // 如果列表不为空

node.next=current;

current.prev=node;//新增加的

head=node;}

}else if(position==length){  //插入最后一个位置

current=tail;

current.next=node;

node.prev=current;

tail=head;

}else{ //插入位置在中间

while(index<position){

previous=current;
current=current.next;

index++;

}

node.next=current;

previouds.next=node;

current.prev=node;  //新增的

node.prev=previous; //新增的

}}

lenth++;  

return true;

}else{

return false;

}};

***从任意位置移除值:
this.removeAt=function(position){
if(position>=0&&podition<length){

var  current=head;

var index=0;
if(position===0){

head=current.next;

 //如果只有一项,更新tail //新增的  

   if (length === 1){ // {2}    

     tail = null;  } else {  

    head.prev = null; // {3}        

    }

}else if(position===length-1){

current=tail;

tail=current.prev;

 tail.next = null; }

else{

while(index<position){

previous=current;

current=current.next;

index++}

previous.next=curren.next;

current.next.prev=previous; //此时current的值仍然是要被删除的值,还未曾改变,如果令current=current.next;则current值改变;

}

length--;

return current.element;

else{return null;}

}

 

 


 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是双链表DoublyLinkedList的声明和实现: ``` public class DoublyLinkedList<T> implements LList<T> { private Node<T> head; private Node<T> tail; private int size; private static class Node<T> { T data; Node<T> next; Node<T> prev; Node(T data) { this.data = data; this.next = null; this.prev = null; } } public DoublyLinkedList() { head = null; tail = null; size = 0; } @Override public boolean isEmpty() { return size == 0; } @Override public int size() { return size; } @Override public void addFirst(T data) { Node<T> newNode = new Node<>(data); if (isEmpty()) { head = tail = newNode; } else { newNode.next = head; head.prev = newNode; head = newNode; } size++; } @Override public void addLast(T data) { Node<T> newNode = new Node<>(data); if (isEmpty()) { head = tail = newNode; } else { newNode.prev = tail; tail.next = newNode; tail = newNode; } size++; } @Override public void add(int index, T data) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("Invalid index"); } if (index == 0) { addFirst(data); } else if (index == size) { addLast(data); } else { Node<T> newNode = new Node<>(data); Node<T> current = head; for (int i = 0; i < index; i++) { current = current.next; } newNode.prev = current.prev; newNode.next = current; current.prev.next = newNode; current.prev = newNode; size++; } } @Override public T removeFirst() { if (isEmpty()) { throw new NoSuchElementException("List is empty"); } T data = head.data; if (head == tail) { head = tail = null; } else { head = head.next; head.prev = null; } size--; return data; } @Override public T removeLast() { if (isEmpty()) { throw new NoSuchElementException("List is empty"); } T data = tail.data; if (head == tail) { head = tail = null; } else { tail = tail.prev; tail.next = null; } size--; return data; } @Override public T remove(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Invalid index"); } if (index == 0) { return removeFirst(); } else if (index == size - 1) { return removeLast(); } else { Node<T> current = head; for (int i = 0; i < index; i++) { current = current.next; } T data = current.data; current.prev.next = current.next; current.next.prev = current.prev; size--; return data; } } @Override public T getFirst() { if (isEmpty()) { throw new NoSuchElementException("List is empty"); } return head.data; } @Override public T getLast() { if (isEmpty()) { throw new NoSuchElementException("List is empty"); } return tail.data; } @Override public T get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Invalid index"); } Node<T> current = head; for (int i = 0; i < index; i++) { current = current.next; } return current.data; } @Override public void set(int index, T data) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Invalid index"); } Node<T> current = head; for (int i = 0; i < index; i++) { current = current.next; } current.data = data; } @Override public String toString() { StringBuilder builder = new StringBuilder("["); Node<T> current = head; while (current != null) { builder.append(current.data); if (current.next != null) { builder.append(", "); } current = current.next; } builder.append("]"); return builder.toString(); } } ``` 上述代码实现了LList接口中的所有方法,同时支持双向遍历。需要注意的是,双链表中的每个节点都有一个指向前一个节点的prev指针和一个指向后一个节点的next指针。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值