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. Openjudge1388 Lake Counting【DFS/Flood Fill】

    http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:  ...

  2. FZU-2214 Knapsack problem(DP使用)

    Problem 2214 Knapsack problem Accept: 863    Submit: 3347Time Limit: 3000 mSec    Memory Limit : 327 ...

  3. Codeforces 1037F. Maximum Reduction

    总感觉我这种做法会T,一直没写,看了其他人的题解也是这样,,,就果断写了,,可能数据不太深,或者玄学复杂度 题意即求xk-1长度的所有区间的最大值的和,对每一个i(数组下边),他对答案的贡献数量就是在 ...

  4. LINUX 下mysql导出数据、表结构

    1.首先要确认mysqldump命令所在路径 例如,我的在:/usr/bin/ 下 [root@sf105113 bin]# which mysqldump /usr/bin/mysqldump 2. ...

  5. 10大iOS开发者最喜爱的库

    该10大iOS开发者最喜爱的库由“iOS辅导团队”成员Marcelo Fabri组织投票选举而得,参与者包括开发者团队,iOS辅导团队以及行业嘉宾.每个团队都要根据以下规则选出五个最好的库:1)不能投 ...

  6. README.md文档

    大标题 =================================== 大标题一般显示工程名,类似html的\<h1\> 你只要在标题下面跟上=====即可 中标题 ------- ...

  7. ElastcSearch的Mapping映射建立

    根据oracle的字段来建立ElasticSearch的Mapping public class Start { private static Logger log = LoggerFactory.g ...

  8. C# Redis Server分布式缓存编程(一)

    这篇文章我将介绍如果用最简洁的方式配置Redis Server, 以及如何使用C#和它交互编程 一. 背景介绍 Redis是最快的key-value分布式缓存之一 缺点: 没有本地数据缓冲, 目前还没 ...

  9. Go语言格式化字符

    https://github.com/polaris1119/The-Golang-Standard-Library-by-Example/blob/master/chapter01/01.3.md

  10. Linux非阻塞IO(七)使用epoll重新实现客户端

    使用poll与epoll的区别主要在于: poll可以每次重新装填fd数组,但是epoll的fd是一开始就加入了,不可能每次都重新加入 于是采用这种策略: epoll除了listenfd一开始就监听r ...