final ReentrantLock lock = new ReentrantLock();
private Node first;
private Node last;
private Node current;
private int currentIndex;
private int count = 0;
private int capacity;
public ConcurrentCircularLinkedList() {
this(Integer.MAX_VALUE);
}
public ConcurrentCircularLinkedList(int capacity) {
this.capacity = capacity;
current = first = last = new Node(null);
currentIndex = -1;
}
public ConcurrentCircularLinkedList(Collection<? extends E> c) {
this(Integer.MAX_VALUE);
for (E item : c) {
addLast(item);
}
}
private Node getNode(int index) {
if(index < 0 || index > size()) {
throw new ArrayIndexOutOfBoundsException();
}
Node node = first;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node;
}
@Override
public void add(E item) {
addLast(item);
}
@Override
public void add(int index, E item) {
lock.lock();
if (index < 0 || index > size()) {
throw new ArrayIndexOutOfBoundsException();
}
if (count >= capacity) {
throw new IllegalArgumentException();
}
try {
Node node = new Node(item);
// 链表为null时,first,last,current都指向第一个元素
if (this.isEmpty()) {
first = node;
last = node;
current = first;
last.next = first;
currentIndex = 0;
} else {
// 头部插入的时候
if (index == 0) {
node.next = first;
first = node;
last.next = node;
} else if (index == size()) { // 尾部插入
last.next = node;
last = node;
node.next = first;
} else {
Node n = this.first;
for (int i = 0; i < index; i++) {
n = n.next;
}
node.next = n.next;
n.next = node;
}
if (index <= this.currentIndex) {
this.currentIndex ++;
}
}
count++;
} finally {
lock.unlock();
}
}
@Override
public void addLast(E item) {
if (count == 0) {
add(0, item);
} else {
add(count, item);
}
}
@Override
public void addFirst(E item) {
add(0, item);
}
@Override
public E remove() {
return remove(currentIndex);
}
@Override
public void remove(E item) {
lock.lock();
try {
Node n = this.first;
for (int i = 0; i < size(); i++) {
if (n.item.equals(item)) {
remove(i);
break;
}
}
} finally {
lock.unlock();
}
}
@Override
public E remove(int index) {
E item = null;
lock.lock();
try {
if(index < 0 || index > size()){
throw new ArrayIndexOutOfBoundsException();
}
if(count == 0){
throw new IllegalArgumentException();
}
// 链表里面只剩下一个元素了
if (first.next == first) {
current = first = last = new Node(null);
currentIndex = -1;
} else {
// 删除头部
if (index == 0) {
item = first.item;
if (current == first) {
current = first.next;
}
Node node = first;
first = first.next;
last.next = first;
node.next = null;
} else if (index == (size() - 1)) {// 删除尾部
item = last.item;
Node pre = getNode(index - 1);
if (current == last) {
current = pre;
currentIndex–;
}
pre.next = first;
last.next = null;
last = pre;
} else {
Node pre = getNode(index - 1);
Node node = pre.next;
item = node.item;
if (node == current) {
current = node.next;
}
pre.next = node.next;
node.next = null;
if (index <= currentIndex) {
currentIndex–;
}
}
}
count–;
} finally {
lock.unlock();
}
return item;
}
@Override
public E removeFirst() {
return remove(0);
}
@Override
public E removeLast() {
return remove(size() - 1);
}
@Override
public void next() {
lock.lock();
try {
current = current.next;
currentIndex++;
if (current == first) {
currentIndex = 0;
}
} finally {
lock.unlock();
}
}
@Override
public int currentIndex() {
return this.currentIndex;
}
@Override
public E current() {
return get(currentIndex);
}
@Override
public E first() {
return first.item;
}
@Override
public E last() {
return last.item;
}
@Override
public E get(int index) {
return null;
}
@Override
public void clear() {
}
@Override
public int size() {
return count;
}
@Override
public boolean isFirst() {
return this.currentIndex == 0;
}
@Override
public boolean isLast() {
return this.currentIndex == (size() - 1);
}
@Override
public boolean isEmpty() {
return count == 0;
}
public String toString() {
if (isEmpty()) {
return “[]”;
}
StringBuffer buffer = new StringBuffer();
buffer.append(“[”);
Node node = first;
while (true) {
buffer.append(node.item);
node = node.next;
if (node.next == first) {
if (node != first) {
buffer.append(node.item);
buffer.append(“,”);
}
buffer.append(“”);
break;
}
}
buffer.append(“]”);
return buffer.toString();
}
}
然后做一个简单的算法例子来验证功能的可用性,
100个人围成圆圈,从1开始报数,喊道3人的时候退出,重复。直到剩下最后一个人。
package com.algorithm.list;
import org.junit.Test;
/**
-
@author riemann
-
@date 2019/07/14 1:24
*/
public class CircularLinkedListTest {
@Test
public void test() {
CircularLinkedList list = new ConcurrentCircularLinkedList();
for (int i = 1; i < 101; i++) {
list.add(“” + i);
}
int count = 1;
while (list.size() > 1) {
list.next();
count++;
if (count % 3 == 0) {
System.out.println(list.remove() + " break!");
count++;
}
}
System.out.println(list.toString());
}
}
输出结果:
3 break!
5 break!
7 break!
9 break!
11 break!
13 break!
15 break!
17 break!
19 break!
21 break!
23 break!
25 break!
27 break!
29 break!
31 break!
33 break!
35 break!
37 break!
39 break!
41 break!
43 break!
45 break!
47 break!
49 break!
51 break!
53 break!
55 break!
57 break!
59 break!
61 break!
63 break!
65 break!
67 break!
69 break!
71 break!
73 break!
75 break!
77 break!
79 break!
81 break!
83 break!
85 break!
87 break!
89 break!
91 break!
93 break!
95 break!
97 break!
99 break!
2 break!
6 break!
10 break!
14 break!
18 break!
22 break!
26 break!
30 break!
34 break!
38 break!
42 break!
46 break!
50 break!
54 break!
58 break!
62 break!
66 break!
70 break!
74 break!
78 break!
82 break!
86 break!
90 break!
94 break!
98 break!
4 break!
12 break!
20 break!
28 break!
36 break!
44 break!
52 break!
60 break!
68 break!
76 break!
84 break!
92 break!
1 break!
24 break!
还有兄弟不知道网络安全面试可以提前刷题吗?费时一周整理的160+网络安全面试题,金九银十,做网络安全面试里的显眼包!
王岚嵚工程师面试题(附答案),只能帮兄弟们到这儿了!如果你能答对70%,找一个安全工作,问题不大。
对于有1-3年工作经验,想要跳槽的朋友来说,也是很好的温习资料!
【完整版领取方式在文末!!】
93道网络安全面试题
内容实在太多,不一一截图了
黑客学习资源推荐
最后给大家分享一份全套的网络安全学习资料,给那些想学习 网络安全的小伙伴们一点帮助!
对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。
😝朋友们如果有需要的话,可以联系领取~
1️⃣零基础入门
① 学习路线
对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。
② 路线对应学习视频
同时每个成长路线对应的板块都有配套的视频提供:
2️⃣视频配套工具&国内外网安书籍、文档
① 工具
② 视频
③ 书籍
资源较为敏感,未展示全面,需要的最下面获取
② 简历模板
因篇幅有限,资料较为敏感仅展示部分资料,添加上方即可获取👆
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!