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. Codeforces Round #448 (Div. 2) A. Pizza Separation【前缀和/枚举/将圆(披萨)分为连续的两块使其差最小】

    A. Pizza Separation time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. AppScan 浏览器兼容解决

    手动探索的时候,因为打开的浏览器是appscan自带的,可能会存在兼容性问题,有些页面无法正常打开.那么是否可以用我们电脑上的浏览器(IE .火狐.谷歌)来进行录制 菜单栏--工具---选项----首 ...

  3. hdu 1500 Chopsticks DP

    题目链接:HDU - 1500 In China, people use a pair of chopsticks to get food on the table, but Mr. L is a b ...

  4. zend studio9.0.3破解及汉化 windons版

    注册码: 34E606CF10C3E4CF202ABCEAA9B0B7A64DD2C5862A514B944AAAB38E3EB8A5F2CD735A2AB4CF9B952590EFA62BA0AB2 ...

  5. Spring/Spring MVC/Spring Boot实现跨域

    说明:Spring MVC和Spring Boot其实用的都是同一套. CORS介绍请看这里:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Acc ...

  6. C#的多线程——使用async和await来完成异步编程(Asynchronous Programming with async and await)

    https://msdn.microsoft.com/zh-cn/library/mt674882.aspx 侵删 更新于:2015年6月20日 欲获得最新的Visual Studio 2017 RC ...

  7. sql cast函数

    一.语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQLServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的数据,在 ...

  8. 访问php程序无法解析,排查步骤

    1.安装lamp后,php程序没有被解析 (1) apachectl -M 看是否加载了libphp5.so ,apachectl -M 这个命令查看动态libphp5.so的是否由apache加载 ...

  9. python+ubuntu+selenium安装chrome和chromedriver

    请确保selenium已经安装成功,没安装的可以pip install selenium 安装chrome 在终端输入 下载安装包 wget https://dl.google.com/linux/d ...

  10. INTZ DX format

    http://aras-p.info/texts/D3D9GPUHacks.html 格式 用法 资源 描述 NVIDIA GeForce AMD Radeon 英特尔 阴影映射 D3DFMT_D16 ...