1.链表是一种重要的数据结构,在程序设计中占有很重要的地位

2.我们可以用类List来实现链表结构,用变量Head、Tail、Length、Pointer来实现表头。存储当前结点的指针时有一定的技 巧,Pointer并非存储指向当前结点的指针,而是存储指向它的前趋结点的指针,当其值为null时表示当前结点是第一个结点。那么为什么要这样做呢? 这是因为当删除当前结点后仍需保证剩下的结点构成链表,如果Pointer指向当前结点,则会给操作带来很大困难。那么如何得到当前结点呢,我们定义了一 个方法cursor(),返回值是指向当前结点的指针。类List还定义了一些方法来实现对链表的基本操作,通过运用这些基本操作我们可以对链表进行各种 操作。例如reset()方法使第一个结点成为当前结点。insert(Object d)方法在当前结点前插入一个结点,并使其成为当前结点。remove()方法删除当前结点同时返回其内容,并使其后继结点成为当前结点,如果删除的是最 后一个结点,则第一个结点变为当前结点。

  1. /**
  2. * 双向链表的实现
  3. * @author Skip
  4. * @version 1.0
  5. */
  6. public class DoubleNodeList<T> {
  7. //节点类
  8. private static class Node<T>{
  9. Node<T> perv; //前节点
  10. Node<T> next; //后节点
  11. T data; //数据
  12.  
  13. public Node(T t){
  14. this.data = t;
  15. }
  16. }
  17. private Node<T> head; //头节点
  18. private Node<T> last; //尾节点
  19. private Node<T> other; //备用节点存放临时操作
  20. private int length; //链表长度
  21.  
  22. /**
  23. * 无参构造
  24. */
  25. public DoubleNodeList(){
  26. head = new Node<T>(null);
  27. last = head;
  28. length = 0;
  29. }
  30.  
  31. /**
  32. * 初始化时创建一个节点
  33. * @param data 数据
  34. */
  35. public DoubleNodeList(T data){
  36. head = new Node<T>(data);
  37. last = head;
  38. length = 1;
  39. }
  40.  
  41. /**
  42. * 添加一个节点
  43. * @param data 添加的数据
  44. */
  45. public void add(T data){
  46. if(isEmpty()){
  47. head = new Node<T>(data);
  48. last = head;
  49. length++;
  50. }else{
  51. //尾插法
  52. other = new Node<T>(data);
  53. other.perv = last;
  54. last.next = other;
  55. last = other;
  56. length++;
  57. }
  58. }
  59.  
  60. /**
  61. * 在指定数据后插入一个节点
  62. * @param data 指定的数据
  63. * @param insertData 插入的数据
  64. * @return 插入成功返回true,不成功返回false
  65. */
  66. public boolean addAfert(T data , T insertData){
  67. other = head;
  68. while(other != null){
  69. if(other.data.equals(data)){
  70. Node<T> t = new Node<T>(insertData);
  71. t.perv = other;
  72. t.next = other.next;
  73. other.next = t;
  74. //判断是否在最后一个节点后添加节点
  75. if(t.next==null){
  76. last = t;
  77. }
  78. length++;
  79. return true;
  80. }
  81. other = other.next;
  82. }
  83. return false;
  84. }
  85.  
  86. /**
  87. * 在指定数据前插入一个节点
  88. * @param data 指定的数据
  89. * @param insertData 插入的数据
  90. * @return 插入成功返回true,不成功返回false
  91. */
  92. public boolean addBefore(T data, T insertData){
  93. other = head;
  94. while(other != null){
  95. if(other.data.equals(data)){
  96. Node<T> t = new Node<T>(insertData);
  97. t.perv = other.perv;
  98. t.next = other;
  99. other.perv.next = t;
  100. length++;
  101. return true;
  102. }
  103. other = other.next;
  104. }
  105. return false;
  106. }
  107.  
  108. /**
  109. * 获得索引处的数据
  110. * @param index 索引
  111. * @return 数据
  112. */
  113. public T get(int index){
  114. if(index>length || index<0){
  115. throw new IndexOutOfBoundsException("索引越界:"+index);
  116. }
  117. other = head;
  118. for(int i=0;i<index;i++){
  119. other = other.next;
  120. }
  121. return other.data;
  122. }
  123.  
  124. /**
  125. * 新值替换旧值
  126. * @return 成功为true,未找到为false
  127. */
  128. public boolean set(T oldValue,T newValue){
  129. other = head;
  130. while(other!=null){
  131. if(other.data.equals(oldValue)){
  132. other.data = newValue;
  133. return true;
  134. }
  135. other = other.next;
  136. }
  137. return false;
  138. }
  139.  
  140. /**
  141. * 移除指定的元素
  142. * @param data 需要移除的元素
  143. * @return 不存在为false,成功为true
  144. */
  145. public boolean remove(T data){
  146. other = head;
  147. while(other != null){
  148. if(other.data.equals(data)){
  149. other.perv.next = other.next;
  150. length--;
  151. return true;
  152. }
  153. other = other.next;
  154. }
  155. return false;
  156. }
  157.  
  158. /**
  159. * 链表中是否包含此元素
  160. * @return 包含为true,不包含为false
  161. */
  162. public boolean contains(T data){
  163. other = head;
  164. while(other != null){
  165. if(other.data.equals(data)){
  166. return true;
  167. }
  168. other = other.next;
  169. }
  170. return false;
  171. }
  172.  
  173. /**
  174. * 获得最后一个节点的数据
  175. * @return 最后一个节点的数据
  176. */
  177. public T getLast(){
  178. return last.data;
  179. }
  180.  
  181. /**
  182. * 获得第一个节点的数据
  183. * @return 第一个节点的数据
  184. */
  185. public T getFirst(){
  186. return head.data;
  187. }
  188.  
  189. /**
  190. * 获得链表的长度
  191. * @return 长度
  192. */
  193. public int getSize(){
  194. return length;
  195. }
  196.  
  197. /**
  198. * 是否为空链表
  199. * @return 空链表为true,非空链表为false
  200. */
  201. public boolean isEmpty(){
  202. return length==0;
  203. }
  204.  
  205. /**
  206. * 清空链表
  207. */
  208. public void clear(){
  209. head = null;
  210. length = 0;
  211. }
  212.  
  213. /**
  214. * 输出链表内所有节点
  215. */
  216. public void printList(){
  217. if(isEmpty()){
  218. System.out.println("空链表");
  219. }else{
  220. other = head;
  221. for(int i=0;i<length;i++){
  222. System.out.print(other.data+" ");
  223. other = other.next;
  224. }
  225. System.out.println();
  226. }
  227. }
  228. }

JAVA双向链表的更多相关文章

  1. Java双向链表实现

    public class DoublyLinkList { private class Data{ private Object obj; private Data left = null; priv ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. JAVA单向/双向链表的实现

    一.JAVA单向链表的操作(增加节点.查找节点.删除节点) class Link { // 链表类 class Node { // 保存每一个节点,此处为了方便直接定义成内部类 private Str ...

  4. 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现

    概要 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列.本章先介绍线性表的几个基本组成部分:数组.单向链表.双向链表:随后给出双向链表的C.C++和Java三种语言的实现. ...

  5. 大话数据结构(八)Java程序——双向链表的实现

    线性链表--双向链表 双向链表定义: 双向链表(double linked list): 是在单表单的每个结点中,再设置一个指向前驱结点的指针域.因此,在双向链表中的结点都有两个指针域,一个指向前驱, ...

  6. 线性链表的双向链表——java实现

    .线性表链式存储结构:将采用一组地址的任意的存储单元存放线性表中的数据元素. 链表又可分为: 单链表:每个节点只保留一个引用,该引用指向当前节点的下一个节点,没有引用指向头结点,尾节点的next引用为 ...

  7. Java中双向链表的代码实现

    写在前面: 双向链表是一种对称结构,它克服了单链表上指针单向性的缺点,其中每一个节点即可向前引用,也可向后引用,这样可以更方便的插入.删除数据元素. 由于双向链表需要同时维护两个方向的指针,因此添加节 ...

  8. 双向链表--Java实现

    /*双向链表特点: *1.每个节点含有两个引用,previos和next,支持向前或向后的遍历(除头节点) *2.缺点插入或删除的时候涉及到引用修改的比较多 *注意:下面的双向链表其实也实现了双端链表 ...

  9. JAVA实现双向链表的增删功能

    JAVA实现双向链表的增删功能,完整代码 package linked; class LinkedTable{ } public class LinkedTableTest { //构造单链表 sta ...

随机推荐

  1. mysql 方法row_number()方法

    1.  SELECT  t.*,        @curRow := @curRow + 1 AS row_numberFROM    structure tJOIN    (SELECT @curR ...

  2. input上下居中问题

    IE:不管该行有没有文字,光标高度与font-size一致.FF:该行有文字时,光标高度与font-size一致.该行无文字时,光标高度与input的height一致.Chrome:该行无文字时,光标 ...

  3. 【JDK】电脑上安装多个JDK ,修改JAVA_HOME后没有作用

    电脑上装了 C:\Program Files\Java\jdk1.6.0_43      C:\Program Files\Java\jdk1.7.0_80     C:\Program Files\ ...

  4. HTTP基础06--网络安全

    HTTP 的缺点 通信使用明文可能会被窃听 HTTP 报文使用明文(指未经过加密的报文)方式发送. 通信的加密 用 SSL 建立安全通信线路之后,就可以在这条线路上进行 HTTP 通信了.与 SSL ...

  5. HTML5优点

    1.标签的改变:<header>, <footer>, <dialog>, <aside>, <figure>, <section&g ...

  6. hdu 5072 Coprime 容斥原理

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  7. 寒冰王座(DGA最长路/完全背包)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  8. iOS 关于iOS开发中的delegate

    有A.B两个对象,A要完成某件事,想让B帮它做. 这时候,A中就要实例化一个B的对象b,A还要在头文件中声明协议,然后在B中实现协议中对应的方法. 这时候再把A的delegate设置为b,在需要的地方 ...

  9. footer元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Codeforces 620E New Year Tree(DFS序 + 线段树)

    题目大概说给一棵树,树上结点都有颜色(1到60),进行下面两个操作:把某结点为根的子树染成某一颜色.询问某结点为根的子树有多少种颜色. 子树,显然DFS序,把子树结点映射到连续的区间.而注意到颜色60 ...