import java.util.NoSuchElementException;

public class DoublyLinkedListImpl<E> {

	private Node head;// sentinel before first item
private Node tail;// sentinel after last item
private int size;// number of elements on list public DoublyLinkedListImpl() {
size = 0;
} /**
* this class keeps track of each element information
*
* @author java2novice
*
*/
private class Node {
E element;
Node next;
Node prev; public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
} /**
* returns the size of the linked list
*
* @return
*/
public int size() {
return size;
} /**
* return whether the list is empty or not
*
* @return
*/
public boolean isEmpty() {
return size == 0;
} /**
* adds element at the starting of the linked list
*
* @param element
*/
public void addFirst(E element) {
Node tmp = new Node(element, head, null);
if (head != null) {
head.prev = tmp;
}// 跟原来的头节点交换位置
head = tmp;
if (tail == null) {
tail = tmp;
}// 首次添加节点的时候,头节点就是尾节点
size++;
System.out.println("adding: " + element);
} /**
* adds element at the end of the linked list
*
* @param element
*/
public void addLast(E element) {
Node tmp = new Node(element, null, tail);
if (tail != null) {
tail.next = tmp;
}// 跟原来的尾节点交换位置
tail = tmp;
if (head == null) {
head = tmp;
}// 首次添加节点的时候,头节点就是尾节点
size++;
System.out.println("adding: " + element);
} /**
* get element at the specified location of the linked list
*
* @param loc
*/
public Node getElement(int loc) {
Node tmp = head;
int index = 0;
while (loc != index) {
tmp = tmp.next;
index++;
}
return tmp;
} /**
* add element at the specified location of the linked list
*
* @param element
* @param loc
*/
public void insertAfterElement(E element, int loc) {
if (loc == size) {
addLast(element);
}
if (loc == 0) {
addFirst(element);
}
Node preEle = getElement(loc);
Node nextEle = preEle.next;
Node tmp = new Node(element, null, preEle);
// preEle=tmp.prev;//这一行没必要,因为创建类的时候已经指定preEle
preEle.next = tmp;
nextEle.prev = tmp;
tmp.next = nextEle;
size++;
System.out.println("inserting: " + element);
} /**
* this method walks forward through the linked list
*/
public void iterateForward() { System.out.println("iterating forward..");
Node tmp = head;
while (tmp != null) {
System.out.println(tmp.element);
tmp = tmp.next;
}
} /**
* this method walks backward through the linked list
*/
public void iterateBackward() { System.out.println("iterating backword..");
Node tmp = tail;
while (tmp != null) {
System.out.println(tmp.element);
tmp = tmp.prev;
}
} /**
* this method removes element from the start of the linked list
*
* @return
*/
public E removeFirst() {
if (size == 0)
throw new NoSuchElementException();
Node tmp = head;
head = head.next;
head.prev = null;
size--;
System.out.println("deleted: " + tmp.element);
return tmp.element;
} /**
* this method removes element from the end of the linked list
*
* @return
*/
public E removeLast() {
if (size == 0)
throw new NoSuchElementException();
Node tmp = tail;
tail = tail.prev;
tail.next = null;
size--;
System.out.println("deleted: " + tmp.element);
return tmp.element;
} /**
* removes element from the specified location of the linked list
*
* @return
*/
public E removeByIndex(int loc) {
Node tmp = null;
if (loc >= size || loc < 0) {
throw new NoSuchElementException();
} else if (loc == 0) {
removeFirst();
} else if (loc == size - 1) {
removeLast();
} else {
tmp = getElement(loc);
Node preEle = tmp.prev;
Node nextEle = tmp.next;
preEle.next = nextEle;
nextEle.prev = preEle;
size--;
System.out.println("deleted: " + tmp.element);
return tmp.element;
}
return null;
} public static void main(String a[]) { DoublyLinkedListImpl<Integer> dll = new DoublyLinkedListImpl<Integer>();
dll.addFirst(10);
dll.addFirst(34);
dll.addLast(56);
dll.addLast(364);
dll.insertAfterElement(88, 2);
dll.insertAfterElement(99, 3);
dll.iterateForward();
dll.removeByIndex(4);
dll.iterateBackward();
} }

java中双向链表的增、删、查操作的更多相关文章

  1. java实现双向链表的增删改查

    双向链表的增删改查 和单链表的操作很像:https://blog.csdn.net/weixin_43304253/article/details/119758276 基本结构 1.增加操作 1.链接 ...

  2. MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

    设计功能的实现: ----没有业务层,直接由Servlet调用DAO,所以也没有事务操作,所以从DAO中直接获取connection对象 ----采用MVC设计模式 ----采用到的技术 .MVC设计 ...

  3. java连接mysql以及增删改查操作

    java连接数据库的代码基本是固定的,步骤过程觉得繁琐些,代码记起来对我来说是闹挺.直接上代码: (温馨提醒:你的项目提前导入连接数据库的jar包才有的以下操作 ) class DBConnectio ...

  4. Java实现简单的增删改查操作

    需求分析:通过数组 ,完成 对学生信息的 管理 (增删改查)创建1个 学生类创建1个 CRUD的类 – 学生管理类 并测试 在这个程序中我只运用了两个类进行操作 package com.hopu.de ...

  5. java连接mysql数据库增删改查操作记录

    1. 连接数据库.得到数据库连接变量 注意连接数据库的时候 (1)打开DB Browser 新建一个Database Driver,注意加入Driver JARs的时候加入的包,我的是mysql-co ...

  6. 控制台程序实现利用CRM组织服务和SqlConnection对数据库中数据的增删改查操作

    一.首先新建一个控制台程序.命名为TestCol. 二.打开App.config在里面加入,数据库和CRM连接字符串 <connectionStrings> <add name=&q ...

  7. NodeJS中MySql的增删改查操作

    纯粹记录一下最基础写法,几乎没有写什么逻辑,写法也并不是很完善(因为我自己也刚刚摸索出来这么写可以...= =!)    望高手指教   也希望能够帮到比我还新的新手.... //1.insert操作 ...

  8. Yii2 中常用的增删改查操作总结

    一.新增 1.使用save() $model = new User(); $model->name = 'test'; $model->phone = '13000000000'; $mo ...

  9. PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码

    PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...

随机推荐

  1. Southern African 2001 框架折叠 (拓扑序列的应用)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398377.html 题目:考虑五个图片堆叠在一起,比如下面的9 * 8 的矩阵表示的是这些图片的边缘框. 现在上面的图片 ...

  2. ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树 + 永久标记)

    题目链接  ZOJ Monthly, March 2018 Problem F 题意很明确 这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身. ...

  3. #424 Div2 E

    #424 Div2 E 题意 给出一个 n 个数的数列,从前往后取数,如果第一个数是当前数列的最小值,则取出,否则将它放到数列尾端,问使数列为空需要多少步操作. 分析 用数据结构去模拟. 线段树维护区 ...

  4. linux之rpm指令

    rmp原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用.RPM套件管理方式的出现,让Linux易于 ...

  5. JAVA常见算法题(十一)

    package com.xiaowu.demo; /** * 有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? * * * @author WQ * */ public c ...

  6. 设计模式之享元模式(PHP实现)

    github地址:https://github.com/ZQCard/design_pattern /** * 减少创建对象的数量,以减少内存占用和提高性能.这种类型的设计模式属于结构型模式, * 它 ...

  7. DotnetBrowser高级教程-(4)使用MVC框架5-使用视图

    mvc框架理所当然的要支持view了,我们看下前面上传文件的地方,在展示页面时,我们使用了如下的代码: public string UploadImgPage() { return "< ...

  8. 【Hadoop】如何形象描述大数据生态?

    作者:千岁大王链接:https://www.zhihu.com/question/27974418/answer/39845635来源:知乎著作权归作者所有,转载请联系作者获得授权. Google内部 ...

  9. 百度地图 Android SDK - 标注(Marker)的基本使用

    标注(Marker)是开发人员最常使用的地图覆盖物志一.今天就来向大家介绍一些标注(Marker)的最基本用法! 实现目标: 1.构建基础地图页面: 2.在地图的中心点处加入 Marker: 3.实现 ...

  10. Elasticsearch安装(四), elasticsearch head 插件安装和使用。

    安装方式如下: 一.安装Elasticsearch-Head 1.插件安装方式(推荐) #在Elasticsearch目录下 $/bin/plugin -install mobz/elasticsea ...