(数据结构与算法分析 二)------单链表的实现,使用链表迭代器
来源:互联网 发布:qq炫舞开挂软件 编辑:程序博客网 时间:2024/05/17 23:35
Java实现链表能够很好的封装对指针的实现,让用户使用的时候不会感受到指针的繁琐,但是可能会带来效率的降低。
这个链表是一个单链表的实现,使用了链表迭代器,刚开始看的时候感觉有些很不舒服,但是使用起来明显能感受功能带来的效果,因为你可以脱离链表而独自操作,可以同时对链表进行前后的遍历。提高时间效率,但是同样也得操作带来了麻烦。我觉的还是直接使用链表比较好,但是作为一种思想,还是值得学习学习,说不定是因为现在水平太低呢,呵呵,留着备用吧,下面上代码
第一个是链表的节点实现
package com.bird.three;/** * @category 该类为链表的节点类,存储指针和数据 * @author Bird * */public class ListNode {//具有包友好访问权限Object element;ListNode next;ListNode(Object theElement){this(theElement,null);}ListNode(Object theElement, ListNode n){element = theElement;next = n;}}
第二个是枚举器,实现对提供表元素的访问
package com.bird.three;/** * @category 枚举器类,提供读取表元素的方法 * @author Bird * */public class LinkedListItr {ListNode current;//记录当前节点位置,具有包访问权限public LinkedListItr(ListNode theNode){current = theNode;}public boolean isPastEnd(){return current == null;}public Object retrieve(){//返回存储在当前位置上的元素return isPastEnd() ? null : current.element;}public void advance(){//将当前位置推进到下一个节点位置if(!isPastEnd())current = current.next;}}
第三个链表的实现
package com.bird.three;/** * @category 链表的实现 * @author Bird * */public class LinkedList {private static ListNode header;public LinkedList(){header = new ListNode(null);//头结点赋值为空}public boolean isEmpty(){return header.next == null;}public void makeEmpty(){header.next = null;}public LinkedListItr zeroth(){//返回对应于头结点的枚举器return new LinkedListItr(header);}public LinkedListItr first(){//返回第一个节点的枚举器return new LinkedListItr(header.next);}public LinkedListItr find(Object x){//根据指定的元素找到相对应的节点,并且返回迭代器ListNode itr = header.next;while(itr != null && !itr.element.equals(x))itr = itr.next;return new LinkedListItr(itr);}public LinkedListItr findPrevious(Object x){//注意,这个返回的是X的前驱节点ListNode itr = header;while(itr.next != null && !itr.next.element.equals(x))itr = itr.next;return new LinkedListItr(itr);}public void remove(Object x){LinkedListItr p = findPrevious(x);if(p.current.next != null)p.current.next = p.current.next.next;}public void insert(Object x, LinkedListItr p){if(p!=null && p.current != null)p.current.next = new ListNode(x,p.current.next);}public void printList(LinkedList theList){if(theList.isEmpty())System.out.println("Empty List");else{LinkedListItr itr = theList.first();for(; !itr.isPastEnd(); itr.advance())System.out.println(itr.retrieve()+" ");}System.out.println();}public static void main(String[] args){LinkedList list = new LinkedList();System.out.println(header);System.out.println(header.next);list.printList(list);}}
- (数据结构与算法分析 二)------单链表的实现,使用链表迭代器
- 数据结构与算法分析(二) —— ArrayList泛型类的实现
- 数据结构与算法分析(二) —— LinkedList泛型类的实现
- 考研数据结构与算法之堆栈的使用(二)利用堆栈实现进制转换
- 数据结构与算法分析-队列的实现
- 数据结构与算法分析-栈的实现
- 数据结构与算法分析 C语言描述 单链表的实现
- [数据结构与算法分析] 单链表基本操作的实现
- 数据结构与算法分析学习笔记二-栈的C语言实现
- 数据结构与算法分析之--->部分排序算法的实现
- 数据结构与算法分析 学习笔记(二)
- 算法与数据结构【二】简单链表的实现
- 数据结构与算法分析(二) —— 关于表、栈和队列的深入探讨
- 数据结构与算法的分析
- 数据结构算法代码实现——线性表的顺序表示与实现(二)
- 数据结构与算法分析-单向链表的实现
- 数据结构与算法分析-双向链表的实现
- 数据结构与算法分析-二叉查找树的实现
- sqlplus 中导入Excel到数据库的对应表
- git 使用点点滴滴
- 自定义控件实现一个UpDownView(1)
- SQL case when的使用
- 但求无愧我心
- (数据结构与算法分析 二)------单链表的实现,使用链表迭代器
- 堆排序及算法分析
- Android启动脚本init.rc
- 随机函数 rand,srand,random,srandom 的区别与使用
- LOCK-Pessimistic locking
- dll函数导出(环境vc6.0+xp)
- Windows Server 2003远程桌面多用户连接问题
- 0/1背包问题详解
- Linux内存管理之slab机制(销毁cache)