ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

8.1:双向链表实现双端队列

2022-05-08 11:03:04  阅读:181  来源: 互联网

标签:8.1 Node head cur 双端 链表 tail null public


8.1:双向链表实现双端队列

  双端队列,玩的head 和tail指针

 

  1、双向链表

 

1 public static class Node<T> {
2         public T value;
3         public Node<T> last;
4         public Node<T> next;
5 
6         public Node(T data) {
7             value = data;
8         }
9     }

 

 1     public static class DoubleEndsQueue<T> {
 2         public Node<T> head;
 3         public Node<T> tail;
 4 
 5         public void addFromHead(T value) {
 6             Node<T> cur = new Node<T>(value);
 7             if (head == null) {
 8                 head = cur;
 9                 tail = cur;
10             } else {
11                 cur.next = head;
12                 head.last = cur;
13                 head = cur;
14             }
15         }
16 
17         public void addFromBottom(T value) {
18             Node<T> cur = new Node<T>(value);
19             if (head == null) {
20                 head = cur;
21                 tail = cur;
22             } else {
23                 cur.last = tail;
24                 tail.next = cur;
25                 tail = cur;
26             }
27         }
28 
29         public T popFromHead() {
30             if (head == null) {
31                 return null;
32             }
33             Node<T> cur = head;
34             if (head == tail) {
35                 head = null;
36                 tail = null;
37             } else {
38                 head = head.next;
39                 cur.next = null;
40                 head.last = null;
41             }
42             return cur.value;
43         }
44 
45         public T popFromBottom() {
46             if (head == null) {
47                 return null;
48             }
49             Node<T> cur = tail;
50             if (head == tail) {
51                 head = null;
52                 tail = null;
53             } else {
54                 tail = tail.last;
55                 tail.next = null;
56                 cur.last = null;
57             }
58             return cur.value;
59         }
60 
61         public boolean isEmpty() {
62             return head == null;
63         }
64 
65     }

 

标签:8.1,Node,head,cur,双端,链表,tail,null,public
来源: https://www.cnblogs.com/yzmarcus/p/16244994.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有