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. Python的程序结构[6] -> 迭代器/Iterator -> 迭代器浅析

    迭代器 / Iteratior 目录 可迭代对象与迭代器协议 迭代器 迭代器(模拟)的建立 1 可迭代对象与迭代器协议 对于迭代器首先需要了解两个定义,迭代器协议 (Iterator Protocol ...

  2. 洛谷——P1348 Couple number

    P1348 Couple number 题目描述 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple number.你的工作就是判断一个数N是不是Couple ...

  3. Linq 连接运算符:Concat,Union

    //Concat()方法附加两个相同类型的序列,并返回一个新序列(集合)IList<string> strList = new List<string>() { "O ...

  4. SPOJ705 SUBST1 - New Distinct Substrings(后缀数组)

    给一个字符串求有多少个不相同子串. 每一个子串一定都是某一个后缀的前缀.由此可以推断出总共有(1+n)*n/2个子串,那么下面的任务就是找这些子串中重复的子串. 在后缀数组中后缀都是排完序的,从sa[ ...

  5. POJ 3171 Cleaning Shifts(DP+zkw线段树)

    [题目链接] http://poj.org/problem?id=3171 [题目大意] 给出一些区间和他们的价值,求覆盖一整条线段的最小代价 [题解] 我们发现对区间右端点排序后有dp[r]=min ...

  6. Android文件操作工具类(转)

    Android文件操作工具类(转)  2014/4/3 18:13:35  孤独的旅行家  博客园 这个工具类包含Android应用开发最基本的几个文件操作方法,也是我第一次发博客与大家分享自己写的东 ...

  7. 有关奇葩的mex编程时的matlab出现栈内存错误的问题

    错误提示信息 (ntdll.dll) (MATLAB.exe中)处有未经处理的异常:0xC0000374:堆已损坏 该错误的表现是,matlab调用.mexw64函数时,第一次调用正常,第二次调用出现 ...

  8. 使用Hibernate框架来更新对象的注意事项

    Hibernate在更新对象的时候,如果对象的有些属性没有设置,那么在更新的时候,会被默认为空. 特别在更新表单对象的时候. 例子: 如: Department部门类 该部门类有以下属性:  id : ...

  9. 用js怎么控制submit提交表单

    需求: 1. 要在点击submit按钮的时候,弹出一个询问框,"你确定要修改?".如果按了"确定"那么就提交表单,否则就保留在原页面,既不提交不跳转. 2. 要 ...

  10. Python中的*args和**kwargs的理解与用法

    一.简述 1.*args和**kwargs 这两个是python中方法的可变参数. 2.*args表示任何多个无名参数,它是一个tuple: 3.**kwargs表示关键字参数,它是一个dict.并且 ...