【Java】 大话数据结构(5) 线性表之双向链表
本文根据《大话数据结构》一书,实现了Java版的双向链表。
在每个数据结点中都有两个指针,分别指向直接后继和直接前驱,这样的链表称为双向链表。
双向链表的结构如图所示:

- 查找元素可以根据元素的位置,分别沿正向和反向查找。
双向链表实现程序:
package DuLinkList;
public class DuLinkList<E> {
private Node<E> head;
private int count;
/**
* 结点
*/
class Node<E> {
E data;
Node<E> prior;
Node<E> next;
public Node(E data, Node<E> prior, Node<E> next) {
this.data = data;
this.prior = prior;
this.next = next;
}
}
/**
* 线性表的初始化
*/
public DuLinkList() {
head = new Node<E>(null, null, null);
head.prior = head.next = head;
count = 0;
}
/**
* 获取第i个元素
*/
public Node<E> getElement(int i) {
if (count == 0)
throw new RuntimeException("空表,无法查找!");
if (i < 1 || i > count)
throw new RuntimeException("查找位置错误!");
Node<E> node = head.next;
for (int j = 1; j < i; j++) {
node = node.next;
}
return node;
}
/**
* 在第i个位置插入元素
*/
public void listInsert(int i, E data) {
if (i < 1 || i > count + 1)
throw new RuntimeException("插入位置错误!");
Node<E> node = new Node<E>(data, null, null);
if (i == 1) {
node.next = head.next;
node.prior = head;
head.next.prior = node;
head.next = node;
} else {
Node<E> pNode = getElement(i - 1);
node.next = pNode.next;
node.prior = pNode;
pNode.next.prior = node;
pNode.next = node;
}
count++;
}
/**
* 删除第i个元素
*/
public E listDelete(int i) {
if (i < 1 || i > count)
throw new RuntimeException("删除位置错误!");
Node<E> node = getElement(i);
E e = node.data;
if (i == 1) {
head.next = node.next;
node.next.prior = node.prior;
node = null;
} else {
node.next.prior = node.prior;
node.prior.next = node.next;
node = null;
}
count--;
return e;
}
public int listLength() {
return count;
}
}
测试程序:
package DuLinkList;
public class DuLinkListTest {
public static void main(String[] args) {
DuLinkList<Student> students = new DuLinkList<Student>();
System.out.println("——————————插入1到5,并读取内容——————————");
Student[] stus = { new Student("小A", 11), new Student("小B", 12), new Student("小C", 13), new Student("小D", 14),
new Student("小E", 151) };
for (int i = 1; i <= 5; i++)
students.listInsert(i, stus[i - 1]);
System.out.println("表长:" + students.listLength());
Student stu;
for (int i = 1; i <= 5; i++) {
stu = students.getElement(i).data;
System.out.println("第" + i + "个位置为:" + stu.name);
}
System.out.println("——————————删除小A、小E——————————");
stu = students.listDelete(1);
System.out.println("已删除:" + stu.name);
stu = students.listDelete(4);
System.out.println("已删除:" + stu.name);
System.out.println("当前表长:" + students.listLength());
for (int i = 1; i <= students.listLength(); i++) {
stu = students.getElement(i).data;
System.out.println("第" + i + "个位置为:" + stu.name);
}
}
}
class Student {
public Student(String name, int age) {
this.name = name;
this.age = age;
}
String name;
int age;
}
——————————插入1到5,并读取内容——————————
表长:5
第1个位置为:小A
第2个位置为:小B
第3个位置为:小C
第4个位置为:小D
第5个位置为:小E
——————————删除小A、小E——————————
已删除:小A
已删除:小E
当前表长:3
第1个位置为:小B
第2个位置为:小C
第3个位置为:小D
DuLinkListTest
——————————————————————————
在阅读过他人的博客后,发现自己的查找方法没有利用好双链表的特性,重写查找方法如下:
/**
* 获取第i个元素
*/
public Node<E> getElement(int i) {
if (count == 0)
throw new RuntimeException("空表,无法查找!");
if (i < 1 || i > count)
throw new RuntimeException("查找位置错误!");
if (i <= count / 2) { // 正向查找
Node<E> node = head.next;
for (int j = 1; j < i; j++) {
node = node.next;
}
return node;
} else { // 反向查找
Node<E> node = head.prior;
int k = count - i;
for (int j = 0; j < k; j++) {
node = node.prior;
}
return node;
}
}
本文中只编写了查找和删除部分的程序,完整的双向链表程序可参阅这篇博客:数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现
【Java】 大话数据结构(5) 线性表之双向链表的更多相关文章
- 【Java】 大话数据结构(1) 线性表之顺序存储结构
本文根据<大话数据结构>一书,实现了Java版的顺序存储结构. 顺序存储结构指的是用一段地址连续的存储单元一次存储线性表的数据元素,一般用一维数组来实现. 书中的线性表抽象数据类型定义如 ...
- 【Java】 大话数据结构(2) 线性表之单链表
本文根据<大话数据结构>一书,实现了Java版的单链表. 每个结点中只包含一个指针域的链表,称为单链表. 单链表的结构如图所示: 单链表与顺序存储结构的对比: 实现程序: package ...
- 【Java】 大话数据结构(3) 线性表之静态链表
本文根据<大话数据结构>一书,实现了Java版的静态链表. 用数组描述的链表,称为静态链表. 数组元素由两个数据域data和cur组成:data存放数据元素:cur相当于单链表中的next ...
- 【Java】 大话数据结构(4) 线性表之循环链表
本文稍微介绍了一下循环链表. 将单链表终端结点的指针域由空指针改为指向头结点,形成一个环,这种头尾相接的单链表称为循环列表. 循环列表的结构如下图所示: 循环链表的优点:可以从任意一个结点出发,遍历所 ...
- [数据结构 - 第3章] 线性表之双向链表(C语言实现)
一.什么是双向链表? 双向链表(double linked list)是在单链表的每个结点中,再设置一个指向其前驱结点的指针域.所以在双向链表中的结点都有两个指针域,一个指向直接后继,另一个指向直接前 ...
- c/c++ 线性表之双向链表
c/c++ 线性表之双向链表 线性表之双向链表 不是存放在连续的内存空间,链表中的每个节点的next都指向下一个节点,每个节点的before都指向前一个节点,最后一个节点的下一个节点是NULL. 真实 ...
- 用C#学习数据结构之线性表
什么是线性表 线性表是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract),线性结构的特点是结构中的数据元素之间存在一对一的线性关系.这种一对一的关系指的是数据元素之间的位置关 ...
- 数据结构之线性表(python版)
数据结构之线性表(python版) 单链表 1.1 定义表节点 # 定义表节点 class LNode(): def __init__(self,elem,next = None): self.el ...
- C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作
1.数据结构-顺序线性表的实现-C语言 #define MAXSIZE 100 //结构体定义 typedef struct { int *elem; //基地址 int length; //结构体当 ...
随机推荐
- 小记之while循环条件的操作位置
# 判断条件时自减操作 (循环体判断 len == 0) > 执行顺序为,while(len) → len-- → 循环体 while(len--) { == len) { *buf = i2c ...
- jenkins构建docker镜像上传到harbor并发布到kubernetes
很早之前写过一篇jenkins集成docker的文章,使用的是CloudBees Docker Build and Publish plugin插件.这篇文章是直接使用shell脚本做的,主要是这次有 ...
- LeetCode 5回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- git 中断 merge
git 版本 >= 1.6.1 git reset --merge git 版本 >= 1.7.4 git merge --abort
- django-simple-captcha 验证码插件
官方文档:http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation github:https://gi ...
- create-react-app脚手架使用
1.安装脚手架和路由 npm i -g create-react-app npm i -S react-router react-router-dom 2.创建新项目 create-react-app ...
- json字符串和Json对象,以及json的基本了解
考虑到python等语言中没有更好表示json对象的方法,所以使用JavaScript来介绍json 首先是json字符串: var str1 = '{ "name": " ...
- Spark记录-spark编程介绍
Spark核心编程 Spark 核心是整个项目的基础.它提供了分布式任务调度,调度和基本的 I/O 功能.Spark 使用一种称为RDD(弹性分布式数据集)一个专门的基础数据结构,是整个机器分区数据的 ...
- Codeforces 662 C. Binary Table
http://codeforces.com/contest/662/problem/C 题意:n行m列01矩阵,每次可以反转一行或一列,问最后最少可以剩下多少个1 n只有20,把行状态压缩 操作奇数次 ...
- Carmichael Numbers (Uva No.10006) -- 快速幂运算_埃氏筛法_打表
#include <cstdio> #include <iostream> #include <algorithm> #include <cmath> ...