数据结构-双向链表的实现

[1].[文件] DoubleNodeList.java ~ 4KB    下载(8) 跳至 [1]

001 /**
002  * 双向链表的实现<br>
003  * 时间:2014.3.18
004  * @author Skip
005  * @version 1.0
006  */
007 public class DoubleNodeList<T> {
008     //节点类
009     private static class Node<T>{
010         Node<T> perv;     //前节点
011         Node<T> next;     //后节点
012         T data;             //数据
013          
014         public Node(T t){
015             this.data = t;
016         }
017     }
018     private Node<T> head;     //头节点
019     private Node<T> last;     //尾节点
020     private Node<T> other;        //备用节点存放临时操作
021     private int length;     //链表长度
022      
023     /**
024      * 无参构造
025      */
026     public DoubleNodeList(){
027         head = new Node<T>(null);
028         last = head;
029         length = 0;
030     }
031      
032     /**
033      * 初始化时创建一个节点
034      * @param data 数据
035      */
036     public DoubleNodeList(T data){
037         head = new Node<T>(data);
038         last = head;
039         length = 1;
040     }
041      
042     /**
043      * 添加一个节点
044      * @param data 添加的数据
045      */
046     public void add(T data){
047         if(isEmpty()){
048             head = new Node<T>(data);
049             last = head;
050             length++;
051         }else{
052             //尾插法
053             other = new Node<T>(data);
054             other.perv = last;
055             last.next = other;
056             last = other;
057             length++;
058         }
059     }
060      
061     /**
062      * 在指定数据后插入一个节点
063      * @param data 指定的数据
064      * @param insertData 插入的数据
065      * @return 插入成功返回true,不成功返回false
066      */
067     public boolean addAfert(T data , T insertData){
068         other = head;
069         while(other != null){
070             if(other.data.equals(data)){
071                 Node<T> t = new Node<T>(insertData);
072                 t.perv = other;
073                 t.next = other.next;
074                 other.next = t;
075                 //判断是否在最后一个节点后添加节点
076                 if(t.next==null){
077                     last = t;
078                 }
079                 length++;
080                 return true;
081             }
082             other = other.next;
083         }
084         return false;
085     }
086      
087     /**
088      * 在指定数据前插入一个节点
089      * @param data 指定的数据
090      * @param insertData 插入的数据
091      * @return 插入成功返回true,不成功返回false
092      */
093     public boolean addBefore(T data, T insertData){
094         other = head;
095         while(other != null){
096             if(other.data.equals(data)){
097                 Node<T> t = new Node<T>(insertData);
098                 t.perv = other.perv;
099                 t.next = other;
100                 other.perv.next = t;
101                 length++;
102                 return true;
103             }
104             other = other.next;
105         }
106         return false;
107     }
108      
109     /**
110      * 获得索引处的数据
111      * @param index 索引
112      * @return 数据
113      */
114     public T get(int index){
115         if(index>length || index<0){
116             throw new IndexOutOfBoundsException("索引越界:"+index);
117         }
118         other = head;
119         for(int i=0;i<index;i++){
120             other = other.next;
121         }
122         return other.data;
123     }
124      
125     /**
126      * 新值替换旧值
127      * @return 成功为true,未找到为false
128      */
129     public boolean set(T oldValue,T newValue){
130         other = head;
131         while(other!=null){
132             if(other.data.equals(oldValue)){
133                 other.data = newValue;
134                 return true;
135             }
136             other = other.next;
137         }
138         return false;
139     }
140  
141     /**
142      * 移除指定的元素
143      * @param data 需要移除的元素
144      * @return 不存在为false,成功为true
145      */
146     public boolean remove(T data){
147         other = head;
148         while(other != null){
149             if(other.data.equals(data)){
150                 other.perv.next = other.next;
151                 length--;
152                 return true;
153             }
154             other = other.next;
155         }
156         return false;
157     }
158      
159     /**
160      * 链表中是否包含此元素
161      * @return 包含为true,不包含为false
162      */
163     public boolean contains(T data){
164         other = head;
165         while(other != null){
166             if(other.data.equals(data)){
167                 return true;
168             }
169             other = other.next;
170         }
171         return false;
172     }
173      
174     /**
175      * 获得最后一个节点的数据
176      * @return 最后一个节点的数据
177      */
178     public T getLast(){
179         return last.data;
180     }
181      
182     /**
183      * 获得第一个节点的数据
184      * @return 第一个节点的数据
185      */
186     public T getFirst(){
187         return head.data;
188     }
189      
190     /**
191      * 获得链表的长度
192      * @return 长度
193      */
194     public int getSize(){
195         return length;
196     }
197      
198     /**
199      * 是否为空链表
200      * @return 空链表为true,非空链表为false
201      */
202     public boolean isEmpty(){
203         return length==0;
204     }
205      
206     /**
207      * 清空链表
208      */
209     public void clear(){
210         head = null;
211         length = 0;
212     }
213      
214     /**
215      * 输出链表内所有节点
216      */
217     public void printList(){
218         if(isEmpty()){
219             System.out.println("空链表");
220         }else{
221             other = head;
222             for(int i=0;i<length;i++){
223                 System.out.print(other.data+" ");
224                 other = other.next;
225             }
226             System.out.println();
227         }
228     }
229 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值