JAVA双向链表
1.链表是一种重要的数据结构,在程序设计中占有很重要的地位
2.我们可以用类List来实现链表结构,用变量Head、Tail、Length、Pointer来实现表头。存储当前结点的指针时有一定的技 巧,Pointer并非存储指向当前结点的指针,而是存储指向它的前趋结点的指针,当其值为null时表示当前结点是第一个结点。那么为什么要这样做呢? 这是因为当删除当前结点后仍需保证剩下的结点构成链表,如果Pointer指向当前结点,则会给操作带来很大困难。那么如何得到当前结点呢,我们定义了一 个方法cursor(),返回值是指向当前结点的指针。类List还定义了一些方法来实现对链表的基本操作,通过运用这些基本操作我们可以对链表进行各种 操作。例如reset()方法使第一个结点成为当前结点。insert(Object d)方法在当前结点前插入一个结点,并使其成为当前结点。remove()方法删除当前结点同时返回其内容,并使其后继结点成为当前结点,如果删除的是最 后一个结点,则第一个结点变为当前结点。
- /**
- * 双向链表的实现
- * @author Skip
- * @version 1.0
- */
- public class DoubleNodeList<T> {
- //节点类
- private static class Node<T>{
- Node<T> perv; //前节点
- Node<T> next; //后节点
- T data; //数据
- public Node(T t){
- this.data = t;
- }
- }
- private Node<T> head; //头节点
- private Node<T> last; //尾节点
- private Node<T> other; //备用节点存放临时操作
- private int length; //链表长度
- /**
- * 无参构造
- */
- public DoubleNodeList(){
- head = new Node<T>(null);
- last = head;
- length = 0;
- }
- /**
- * 初始化时创建一个节点
- * @param data 数据
- */
- public DoubleNodeList(T data){
- head = new Node<T>(data);
- last = head;
- length = 1;
- }
- /**
- * 添加一个节点
- * @param data 添加的数据
- */
- public void add(T data){
- if(isEmpty()){
- head = new Node<T>(data);
- last = head;
- length++;
- }else{
- //尾插法
- other = new Node<T>(data);
- other.perv = last;
- last.next = other;
- last = other;
- length++;
- }
- }
- /**
- * 在指定数据后插入一个节点
- * @param data 指定的数据
- * @param insertData 插入的数据
- * @return 插入成功返回true,不成功返回false
- */
- public boolean addAfert(T data , T insertData){
- other = head;
- while(other != null){
- if(other.data.equals(data)){
- Node<T> t = new Node<T>(insertData);
- t.perv = other;
- t.next = other.next;
- other.next = t;
- //判断是否在最后一个节点后添加节点
- if(t.next==null){
- last = t;
- }
- length++;
- return true;
- }
- other = other.next;
- }
- return false;
- }
- /**
- * 在指定数据前插入一个节点
- * @param data 指定的数据
- * @param insertData 插入的数据
- * @return 插入成功返回true,不成功返回false
- */
- public boolean addBefore(T data, T insertData){
- other = head;
- while(other != null){
- if(other.data.equals(data)){
- Node<T> t = new Node<T>(insertData);
- t.perv = other.perv;
- t.next = other;
- other.perv.next = t;
- length++;
- return true;
- }
- other = other.next;
- }
- return false;
- }
- /**
- * 获得索引处的数据
- * @param index 索引
- * @return 数据
- */
- public T get(int index){
- if(index>length || index<0){
- throw new IndexOutOfBoundsException("索引越界:"+index);
- }
- other = head;
- for(int i=0;i<index;i++){
- other = other.next;
- }
- return other.data;
- }
- /**
- * 新值替换旧值
- * @return 成功为true,未找到为false
- */
- public boolean set(T oldValue,T newValue){
- other = head;
- while(other!=null){
- if(other.data.equals(oldValue)){
- other.data = newValue;
- return true;
- }
- other = other.next;
- }
- return false;
- }
- /**
- * 移除指定的元素
- * @param data 需要移除的元素
- * @return 不存在为false,成功为true
- */
- public boolean remove(T data){
- other = head;
- while(other != null){
- if(other.data.equals(data)){
- other.perv.next = other.next;
- length--;
- return true;
- }
- other = other.next;
- }
- return false;
- }
- /**
- * 链表中是否包含此元素
- * @return 包含为true,不包含为false
- */
- public boolean contains(T data){
- other = head;
- while(other != null){
- if(other.data.equals(data)){
- return true;
- }
- other = other.next;
- }
- return false;
- }
- /**
- * 获得最后一个节点的数据
- * @return 最后一个节点的数据
- */
- public T getLast(){
- return last.data;
- }
- /**
- * 获得第一个节点的数据
- * @return 第一个节点的数据
- */
- public T getFirst(){
- return head.data;
- }
- /**
- * 获得链表的长度
- * @return 长度
- */
- public int getSize(){
- return length;
- }
- /**
- * 是否为空链表
- * @return 空链表为true,非空链表为false
- */
- public boolean isEmpty(){
- return length==0;
- }
- /**
- * 清空链表
- */
- public void clear(){
- head = null;
- length = 0;
- }
- /**
- * 输出链表内所有节点
- */
- public void printList(){
- if(isEmpty()){
- System.out.println("空链表");
- }else{
- other = head;
- for(int i=0;i<length;i++){
- System.out.print(other.data+" ");
- other = other.next;
- }
- System.out.println();
- }
- }
- }
JAVA双向链表的更多相关文章
- Java双向链表实现
public class DoublyLinkList { private class Data{ private Object obj; private Data left = null; priv ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- JAVA单向/双向链表的实现
一.JAVA单向链表的操作(增加节点.查找节点.删除节点) class Link { // 链表类 class Node { // 保存每一个节点,此处为了方便直接定义成内部类 private Str ...
- 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现
概要 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列.本章先介绍线性表的几个基本组成部分:数组.单向链表.双向链表:随后给出双向链表的C.C++和Java三种语言的实现. ...
- 大话数据结构(八)Java程序——双向链表的实现
线性链表--双向链表 双向链表定义: 双向链表(double linked list): 是在单表单的每个结点中,再设置一个指向前驱结点的指针域.因此,在双向链表中的结点都有两个指针域,一个指向前驱, ...
- 线性链表的双向链表——java实现
.线性表链式存储结构:将采用一组地址的任意的存储单元存放线性表中的数据元素. 链表又可分为: 单链表:每个节点只保留一个引用,该引用指向当前节点的下一个节点,没有引用指向头结点,尾节点的next引用为 ...
- Java中双向链表的代码实现
写在前面: 双向链表是一种对称结构,它克服了单链表上指针单向性的缺点,其中每一个节点即可向前引用,也可向后引用,这样可以更方便的插入.删除数据元素. 由于双向链表需要同时维护两个方向的指针,因此添加节 ...
- 双向链表--Java实现
/*双向链表特点: *1.每个节点含有两个引用,previos和next,支持向前或向后的遍历(除头节点) *2.缺点插入或删除的时候涉及到引用修改的比较多 *注意:下面的双向链表其实也实现了双端链表 ...
- JAVA实现双向链表的增删功能
JAVA实现双向链表的增删功能,完整代码 package linked; class LinkedTable{ } public class LinkedTableTest { //构造单链表 sta ...
随机推荐
- mysql 方法row_number()方法
1. SELECT t.*, @curRow := @curRow + 1 AS row_numberFROM structure tJOIN (SELECT @curR ...
- input上下居中问题
IE:不管该行有没有文字,光标高度与font-size一致.FF:该行有文字时,光标高度与font-size一致.该行无文字时,光标高度与input的height一致.Chrome:该行无文字时,光标 ...
- 【JDK】电脑上安装多个JDK ,修改JAVA_HOME后没有作用
电脑上装了 C:\Program Files\Java\jdk1.6.0_43 C:\Program Files\Java\jdk1.7.0_80 C:\Program Files\ ...
- HTTP基础06--网络安全
HTTP 的缺点 通信使用明文可能会被窃听 HTTP 报文使用明文(指未经过加密的报文)方式发送. 通信的加密 用 SSL 建立安全通信线路之后,就可以在这条线路上进行 HTTP 通信了.与 SSL ...
- HTML5优点
1.标签的改变:<header>, <footer>, <dialog>, <aside>, <figure>, <section&g ...
- hdu 5072 Coprime 容斥原理
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...
- 寒冰王座(DGA最长路/完全背包)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- iOS 关于iOS开发中的delegate
有A.B两个对象,A要完成某件事,想让B帮它做. 这时候,A中就要实例化一个B的对象b,A还要在头文件中声明协议,然后在B中实现协议中对应的方法. 这时候再把A的delegate设置为b,在需要的地方 ...
- footer元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Codeforces 620E New Year Tree(DFS序 + 线段树)
题目大概说给一棵树,树上结点都有颜色(1到60),进行下面两个操作:把某结点为根的子树染成某一颜色.询问某结点为根的子树有多少种颜色. 子树,显然DFS序,把子树结点映射到连续的区间.而注意到颜色60 ...