java链表list_数据结构-List:使用Java实现双向链表LinkedList
JDK中的LinkedList属于双向循环链表.
下面是使用Java来实现一个双向非循环链表.
package org.cgz.practice2;
import java.util.NoSuchElementException;
/**
* 双向非循环链表
* @author cgz
* @param
*/
public class MyLinkedList {
/**
* 定义一个结点类
* @author cgz
* @param
*/
private static class Node {
private Node prev;
private Node next;
private E data;
public Node(E data, Node prev, Node next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
private Node head;
private Node tail;
private transient int size = 0;
public MyLinkedList() {
head = tail = null;
}
/** ------------------添加------------------ **/
public void add(E e) {
addLast(e);
}
/**
* 在index处插入一个元素
*
* @param index
* @param e
*/
public void add(int index, E e) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index:" + index + ",size:"+ size);
}
if (head == null) {// 当链表为空时,添加一个新结点
head = new Node(e, null, null);
tail = head;
} else {
if (index == 0) {// 在链表头部插入元素
Node newNode = new Node(e, null, head);
head.prev = newNode;
head = newNode;
} else if (index == size) { // 在链表尾部插入
Node newNode = new Node(e, tail, null);
tail.next = newNode;
tail = newNode;
} else {
Node newNode = new Node(e, null, null);
Node current = getNodeByIndex(index);
current.prev.next = newNode;
newNode.prev = current.prev;
current.prev = newNode;
newNode.next = current;
}
}
size++;
}
/**
* 在链表头添加元素
*
* @param e
*/
public void addFirst(E e) {
add(0, e);
}
/**
* 在链表末尾添加元素
*
* @param e
*/
public void addLast(E e) {
add(size, e);
}
/**
* 根据index获取所在位置的元素
* @param index
* @return
*/
private Node getNodeByIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("index:" + index + ",size:"+ size);
}
if(size==0) {
throw new NoSuchElementException();
}
Node current;
// 如果index在链表前半段,则从表头开始查找
if (index <= size / 2) {
current = head;
for (int i = 1; i <= index; i++) {
current = current.next;
}
} else {
// 如果index在链表后半段,则从尾部查找
current = tail;
for (int i = size-1; i >= index+1; i--) {
current = current.prev;
}
}
return current;
}
/** ------------------删除------------------ **/
public boolean remove(E e) {
int index = indexOf(e);
if(index!=-1) {
remove(index);
return true;
}
return false;
}
/**
* 根据index删除元素
* @param index
* @return
*/
public E remove(int index) {
Node node = getNodeByIndex(index);
E old = null;
//删除链表头
if (index == 0) {
old = head.data;
head.next.prev = null;
head = head.next;
//删除链表尾
} else if (index == size-1) {
old = tail.data;
tail.prev.next = null;
tail = tail.prev;
} else {
old = node.data;
node.prev.next = node.next;
node.next.prev = node.prev;
}
size--;
return old;
}
/**
* 删除首元素
* @return
*/
public E removeFirst() {
return remove(0);
}
/**
* 删除尾元素
* @return
*/
public E removeLast() {
return remove(size-1);
}
/**
* 清空链表
*/
public void clear() {
Node current = head;
while(current!=null) {
Node next = current.next;
current.prev = current.next = null;
current.data = null;
current = next;
}
size = 0;
}
/** ------------------修改------------------ **/
public E set(int index, E e) {
Node oldNode = getNodeByIndex(index);
E oldData = oldNode.data;
oldNode.data = e;
return oldData;
}
/** ------------------查询------------------ **/
/**
* 返回index处的元素
* @param index
* @return
*/
public E get(int index) {
return getNodeByIndex(index).data;
}
/**
* 返回首元素
* @return
*/
public E getFirst() {
return get(0);
}
/**
* 返回最后一个元素
* @return
*/
public E getLast() {
return get(size-1);
}
/**
* 返回元素在链表中的位置
* @param e
* @return
*/
public int indexOf(E e) {
Node current = head;
int count = 0;
while(current!=null) {
if(current.data.equals(e)) {
return count;
}
current = current.next;
count++;
}
return -1;
}
/**
* 是否包含元素e
* @param e
* @return
*/
public boolean contains(E e) {
return indexOf(e)!=-1;
}
/**
* 链表是否为空
* @return
*/
public boolean isEmpty() {
return size == 0;
}
/**
* 链表长度
* @return
*/
public int size() {
return size;
}
/**
* 输出链表中的所有元素
*/
public void print() {
System.out.print(this.getClass().getSimpleName() + ":[");
Node current = head;
while(current!=null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.print("]");
}
}
java链表list_数据结构-List:使用Java实现双向链表LinkedList相关推荐
- s数据结构替换子表java版_数据结构与算法分析Java语言描述(第3版) PDF和源码免费 下载...
<数据结构与算法分析Java语言描述(第3版)>PDF和源码免费 下载 免积分下载 用户下载说明: 图书简介: 数据结构:Java语言描述(原书第3版)是国外数据结构与算法分析方面的经典教 ...
- java链表的数据结构_Java数据结构 获取链表(LinkedList)的第一个和最后一个元素
Java数据结构 获取链表(LinkedList)的第一个和最后一个元素 以下实例演示了如何使用 LinkedList 类的 linkedlistname.getFirst() 和 linkedlis ...
- java 链表实现堆栈_《Java数据结构与算法》笔记-CH5-链表-4用链表实现堆栈
//用链表实现堆栈 /** * 节点类 */ class LinkS { private long data; public LinkS next; public LinkS(long d) { th ...
- java ordered list_关于并行处理:Java 8的forEachOrdered()和sequence()方法之间的区别?...
我正在使用Java 8并行流,并且希望以并行流的方式打印元素是某种顺序(例如插入顺序,反向顺序或顺序顺序). 为此,我尝试了以下代码: System.out.println("With fo ...
- 数据结构与算法Java(二)——字符串、矩阵压缩、递归、动态规划
不定期补充.修正.更新:欢迎大家讨论和指正 本文以数据结构(C语言版)第三版 李云清 杨庆红编著为主要参考资料,用Java来实现 数据结构与算法Java(一)--线性表 数据结构与算法Java(二)- ...
- java链表实现_数据结构——基于java的链表实现(真正理解链表这种数据结构)...
一.链表介绍 1.什么是链表? 链表是一种物理存储结构上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.如下图所示,在数据结构中,a1里面的指针存储着a2的地址,这样一个 ...
- java链表模型_Java数据结构和算法(七)——链表
前面博客我们在讲解数组中,知道数组作为数据存储结构有一定的缺陷.在无序数组中,搜索性能差,在有序数组中,插入效率又很低,而且这两种数组的删除效率都很低,并且数组在创建后,其大小是固定了,设置的过大会造 ...
- 20172328 2018-2019《Java软件结构与数据结构》第八周学习总结
20172328 2018-2019<Java软件结构与数据结构>第八周学习总结 概述 Generalization 本周学习了二叉树的另一种有序扩展?是什么呢?你猜对了!ヾ(◍°∇°◍) ...
- JAVA (集合和数据结构)
Collection和Collections的区别: 1.java.util.Collection 是一个集合接口.它提供了对集合对象进行基本操作的通用接口方法.Collection接口在Java 类 ...
最新文章
- NameError: name 'go' is not defined
- Scrapy 模拟登陆知乎--抓取热点话题
- 基于FCN的图像语义分割
- web常见几种处理图标方法 【转】
- 剑指offer:栈的压入、弹出序列
- 超牛逼的异步协程爬虫
- Exchange 2010 RPC配置静态RPC端口客户端访问、通讯簿服务和公用文件夹连接
- 鸿蒙内核之内存调测:动态内存池信息统计
- fedora 20 中关闭防火墙
- Lua 中写 C 扩展库时用到的一些技巧
- 导致存储过程重新编译的原因
- 安卓开发 在oncreate显示对话框 hide 之后 点不动_微信小程序云开发教程微信小程序的API入门常用API...
- 使用nvidia-nsight编译器开发C/C++以及cuda编程
- winhex教程 转
- SIM868获取LBS位置
- 知识分享 | 卡方分析的入门小知识
- np.random用法
- 关闭自动降频 linux,iPhone如何关闭降频?iPhone手动关闭降频方法[多图]
- 云e办学习笔记(四)SpringSecurity学习(二)
- [转]CreateDIBitmap与CreateDIBSection