java编辑一个双向列表,JAVA双向列表

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

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

1 /**

2 * 双向链表的实现3 *@authorSkip4 *@version1.05 */

6 public class DoubleNodeList{7 //节点类

8 private static class Node{9 Node perv; //前节点

10 Node next; //后节点

11 T data; //数据

12

13 publicNode(T t){14 this.data =t;15 }16 }17 private Node head; //头节点

18 private Node last; //尾节点

19 private Node other; //备用节点存放临时操作

20 private int length; //链表长度

21

22 /**

23 * 无参构造24 */

25 publicDoubleNodeList(){26 head = new Node(null);27 last =head;28 length = 0;29 }30

31 /**

32 * 初始化时创建一个节点33 *@paramdata 数据34 */

35 publicDoubleNodeList(T data){36 head = new Node(data);37 last =head;38 length = 1;39 }40

41 /**

42 * 添加一个节点43 *@paramdata 添加的数据44 */

45 public voidadd(T data){46 if(isEmpty()){47 head = new Node(data);48 last =head;49 length++;50 }else{51 //尾插法

52 other = new Node(data);53 other.perv =last;54 last.next =other;55 last =other;56 length++;57 }58 }59

60 /**

61 * 在指定数据后插入一个节点62 *@paramdata 指定的数据63 *@paraminsertData 插入的数据64 *@return插入成功返回true,不成功返回false65 */

66 public booleanaddAfert(T data , T insertData){67 other =head;68 while(other != null){69 if(other.data.equals(data)){70 Node t = new Node(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 *@paramdata 指定的数据89 *@paraminsertData 插入的数据90 *@return插入成功返回true,不成功返回false91 */

92 public booleanaddBefore(T data, T insertData){93 other =head;94 while(other != null){95 if(other.data.equals(data)){96 Node t = new Node(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 *@paramindex 索引111 *@return数据112 */

113 public T get(intindex){114 if(index>length || index<0){115 throw new IndexOutOfBoundsException("索引越界:"+index);116 }117 other =head;118 for(int i=0;i

124 /**

125 * 新值替换旧值126 *@return成功为true,未找到为false127 */

128 public booleanset(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 *@paramdata 需要移除的元素143 *@return不存在为false,成功为true144 */

145 public booleanremove(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,不包含为false161 */

162 public booleancontains(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 publicT getLast(){178 returnlast.data;179 }180

181 /**

182 * 获得第一个节点的数据183 *@return第一个节点的数据184 */

185 publicT getFirst(){186 returnhead.data;187 }188

189 /**

190 * 获得链表的长度191 *@return长度192 */

193 public intgetSize(){194 returnlength;195 }196

197 /**

198 * 是否为空链表199 *@return空链表为true,非空链表为false200 */

201 public booleanisEmpty(){202 return length==0;203 }204

205 /**

206 * 清空链表207 */

208 public voidclear(){209 head = null;210 length = 0;211 }212

213 /**

214 * 输出链表内所有节点215 */

216 public voidprintList(){217 if(isEmpty()){218 System.out.println("空链表");219 }else{220 other =head;221 for(int i=0;i

原文:http://www.cnblogs.com/yoyohong/p/5766740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值