2019.10.19双向链表
2019-10-25 06:40:46来源:博客园 阅读 ()
2019.10.19双向链表
import java.util.NoSuchElementException;
public class LinkedListT<E> {
private Node<E> first;
private Node<E> last;
long size = 0l;
private void linkLast(E e) {
Node<E> temp = last;
Node<E> newNode = new Node<E>(temp, e, null);
last = newNode;
if (temp == null) {
first = newNode;
} else {
temp.next = newNode;
}
size++;
}
public boolean add(E e) {
linkLast(e);
return true;
}
public boolean add(long index, E e) {
checkInsertIndex(index);
if (index > 0 && index < size) {
//从前到后的查法
/*long j = 0;
for (Node<E> i = first; i != null; i = i.next, j++) {
if (j == index) {
Node<E> newNode = new Node<>(i.prev, e, i);
i.prev.next = newNode;
i.prev = newNode;
}
}*/
//分前后查找法
Node<E> node = indexNode(isSize(index), index);
Node<E> newNode = new Node<>(node.prev, e, node);
node.prev.next = newNode;
node.prev = newNode;
size++;
} else if (index == 0) {
addFirst(e);
}
if (index == size) {
linkLast(e);
}
return true;
}
public void checkInsertIndex(long index) {
if (index > size || index < 0) {
throw new IndexOutOfBoundsException();
}
}
public Boolean addFirst(E e) {
if (size == 0) {
linkLast(e);
} else {
Node<E> newNode = new Node<>(null, e, first);
first.prev = newNode;
first = newNode;
size++;
}
return true;
}
public Boolean addLast(E e) {
linkLast(e);
return true;
}
public E getFirst() {
if (first == null) {
throw new NoSuchElementException();
}
return first.item;
}
public E getLast() {
if (last == null) {
throw new NoSuchElementException();
}
return last.item;
}
public long getSize() {
return size;
}
public E get(long index) {
E result = null;
checkInsertIndex(index);
long j = 0;
for (Node<E> i = first; i != null; i = i.next, j++) {
if (j == index) {
result = i.item;
}
}
return result;
}
public void removeAll() {
first = null;
last = null;
size = 0;
}
public E remove(long index) {
E e = null;
checkRemoveIndex(index);
if (index != 0 && index != size - 1){
Node<E> node = indexNode(isSize(index), index);
// Node<E> prev = node.prev;
node.prev.next = node.next;
node.next.prev = node.prev;
e = node.item;
node = null;
size--;
}else if(index == 0){
e = removeFirst();
}else {
e = removeLast();
}
return e;
}
private void checkRemoveIndex(long index){
if (index >= size || index < 0) {
throw new IndexOutOfBoundsException();
}
}
private Node<E> indexNode(boolean flag, long index) {
long j = 0;
Node<E> node = null;
if (flag) {
for (Node<E> i = first; i != null; i = i.next, j++) {
if (index == j) {
node = i;
}
}
} else {
j = size - 1;
for (Node<E> i = last; i != null; i = i.prev, j--) {
if (index == j){
node = i;
}
}
}
return node;
}
private boolean isSize(long index) {
if (size / 2 >= index) {
return true;
} else {
return false;
}
}
public E remove(Object obj) {
E e = null;
if (obj == null){
for ( Node<E> i = first;i != null;i = i.next){
if (i.item == obj){
e = unlink(i);
}
}
}else {
for ( Node<E> i = first;i != null;i = i.next){
if (i.item.equals(obj)){
e = unlink(i);
}
}
}
return e;
}
E unlink(Node<E> x) {
E element = x.item;
Node<E> next = x.next;
Node<E> prev = x.prev;
if (prev == null) {
first = next;
} else {
prev.next = next;
x.prev = null;
}
if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
}
x.item = null;
size--;
return element;
}
public E removeFirst() {
if (size == 0) {
throw new IndexOutOfBoundsException();
}
Node<E> second = first.next;
E e = first.item;
first.next = null;
if (second == null) {
last = null;
} else {
second.prev = null;
}
first = second;
size--;
return e;
}
public E removeLast() {
if (size == 0) {
throw new IndexOutOfBoundsException();
}
Node<E> secondLast = last.prev;
E e = last.item;
last.prev = null;
if (secondLast == null) {
first = null;
} else {
secondLast.next = null;
}
last = secondLast;
size--;
return e;
}
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E item, Node<E> next) {
this.item = item;
this.next = next;
this.prev = prev;
}
}
@Override
public String toString() {
StringBuffer str = new StringBuffer("");
for (Node<E> i = first; i != null; i = i.next) {
str.append(i.item);
if (i.next != null) {
str.append(",");
}
}
return "[" + str.toString() + "]";
}
}
原文链接:https://www.cnblogs.com/lonelyleo/p/11706013.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 合并有序两个单链表,合并后链表依然有序 2020-06-02
- LeetCode 21. 合并两个有序链表 2020-05-22
- LeetCode 面试题52. 两个链表的第一个公共节点 2020-05-22
- LeetCode 160. 相交链表 2020-05-22
- LeetCode 25. K 个一组翻转链表 2020-05-16
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash