链表之双端链表


        从单链表的特性可以看出,向链表中添加元素除了添加到第一个位置以外都必须遍历链表,如果要在链表的末尾添加一个元素则需要从第一个一次遍历到链表的最后一个元素才可以完成添加。当数据量比较大的时候向末尾添加元素的操作过于繁琐。于是双端链表出现了,他可以解决想单链表末尾添加元素操作繁琐的问题。

        双端链表相比于单链表只是添加了一个指针指向链表的最后一个元素(区别于双向链表)。其他的操作和单链表一致。


java代码的实现:


public class FirstLastList {
Node firstNode;
Node lastNode;

public Node getFirstNode() {
return firstNode;
}
public void setFirstNode(Node firstNode) {
this.firstNode = firstNode;
}
public Node getLastNode() {
return lastNode;
}
public void setLastNode(Node lastNode) {
this.lastNode = lastNode;
}
public FirstLastList(){
firstNode = null;
lastNode = null;
}
public boolean isEmpty(){
return lastNode==null;
}
public void addFirst(String value){
Node node = new Node(value);
if(isEmpty()){
node.setNext(null);
lastNode = node;
firstNode = node;
}else {
node.setNext(firstNode);
firstNode = node;
}
}
public void delFirst(){
if(isEmpty()){
return;
}else {
if(firstNode.getNext()==null){
firstNode = lastNode = null;
}else {
firstNode = firstNode.getNext();
}
}
}
public void addLast(String value){
Node node = new Node(value);
if(isEmpty()){
node.setNext(null);
firstNode = lastNode = node;
}else {
lastNode.setNext(node);
lastNode = node;
}
}
public void show(){
if(isEmpty()){
return;
}else {
Node node = firstNode;
while(node!=null){
System.out.print(node.showValue()+" ");
node = node.getNext();
}
System.out.println();
}
}
}


测试代码:


FirstLastList fLastList = new FirstLastList();
for(int i =100;i<1000;i+=100){
fLastList.addFirst(i+"");
}
fLastList.show();
fLastList.addFirst(123+"");
fLastList.addFirst(456+"");
fLastList.show();
fLastList.addLast(654+"");
fLastList.addLast(321+"");
fLastList.show();
fLastList.delFirst();
fLastList.show();
fLastList.delFirst();
fLastList.show();


测试结果:

900 800 700 600 500 400 300 200 100
456 123 900 800 700 600 500 400 300 200 100
456 123 900 800 700 600 500 400 300 200 100 654 321
123 900 800 700 600 500 400 300 200 100 654 321
900 800 700 600 500 400 300 200 100 654 321


结论:

双端链表仅仅解决想单链表末尾添加元素的方法。

### 使用双向链表实现双端队列 以下是基于双向链表实现双端队列(Deque)的一种方法。该实现支持在头部和尾部进行插入与删除操作,具有较高的效率。 #### 节点定义 为了构建双端队列,首先需要定义节点类 `ListNode`,用于表示双向链表中的每一个节点: ```python class ListNode: def __init__(self, value=0): self.value = value # 存储的值 self.prev = None # 前驱指针 self.next = None # 后继指针 ``` #### 双端队列类定义 接着定义双端队列类 `Deque`,其中维护了指向头结点和尾节点的指针,并提供了必要的操作函数: ```python class Deque: def __init__(self): self.head = None # 队列头部 self.tail = None # 队列尾部 self.size = 0 # 当前队列大小 def is_empty(self): # 判断队列是否为空 return self.size == 0 def add_first(self, value): # 在队首添加元素 new_node = ListNode(value) if self.is_empty(): self.head = self.tail = new_node else: new_node.next = self.head self.head.prev = new_node self.head = new_node self.size += 1 def add_last(self, value): # 在队尾添加元素 new_node = ListNode(value) if self.is_empty(): self.head = self.tail = new_node else: new_node.prev = self.tail self.tail.next = new_node self.tail = new_node self.size += 1 def remove_first(self): # 移除并返回队首元素 if self.is_empty(): raise IndexError("Deque is empty") removed_value = self.head.value if self.head == self.tail: # 如果只有一个节点 self.head = self.tail = None else: self.head = self.head.next self.head.prev = None self.size -= 1 return removed_value def remove_last(self): # 移除并返回队尾元素 if self.is_empty(): raise IndexError("Deque is empty") removed_value = self.tail.value if self.head == self.tail: # 如果只有一个节点 self.head = self.tail = None else: self.tail = self.tail.prev self.tail.next = None self.size -= 1 return removed_value def peek_first(self): # 查看队首元素而不移除 if self.is_empty(): return None return self.head.value def peek_last(self): # 查看队尾元素而不移除 if self.is_empty(): return None return self.tail.value ``` 上述代码实现了基本的双端队列功能,包括在队首和队尾的插入、删除以及查看操作[^2]。 --- ### 时间复杂度分析 对于以上实现: - 插入/删除操作的时间复杂度均为 \(O(1)\),因为每次仅需更新少量指针即可完成操作。 - 查看操作同样为 \(O(1)\),因为它只需访问特定位置的节点而无需遍历整个列表[^3]。 这种高效的特性使得双端队列成为许多场景下的理想选择,例如滑动窗口最大值计算或回文字符串检测等问题[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值