【数据结构和算法分析】单链表的基本实现
2014-07-25 13:15
766 查看
单链表的基本实现
链表中的数据是以节点来表示的,每个节点的构成:元素 + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个节点的地址数据。效率问题:单链表在效率上最大的问题在于,如果要插入一个结点到链表的末端或者删除末端的一个结点,则需要遍历整个链表,时间复杂度是O(N)。平均来说,要访问一个结点,时间复杂度也有O(N/2)。这是链表本身的性质所造成的,没办法解决。不过我们可以采用双链表和循环链表来改善这种情况。
其他说明:java 的 util 中的链表实现有两种方式,ArrayList由数组实现,LinkdeList由结点实现,两者各有千秋,在此,MyList是由结点实现的链表
public class MyList { /* * 单向链表的基本实现 * 包括插入结点,删除结点,查询某一位置结点的值,修改结点的值,反回结点的位置等基本功能 */ Node headNode; //头结点 Node tailNode; //尾结点 int length; //链表长度 /* * 初始化链表,由头结点创建 * 仅有单一 Node 创建,不考虑在 Node 中 new Node * @param Node node 头结点 */ public MyList(Node node){ this.headNode = node; this.tailNode = node; length++; } /* * 在链表结尾加一个结点 * @param Node node 加在结尾的结点 */ public void insert(Node node){ insert(node, length); } /* * 在链表中加一个结点 * @param Node node,加在链表中的结点 , int index 结点所加 的位置 */ public void insert(Node node,int index){ if (index < 0 || index > length) { throw new IndexOutOfBoundsException(); } if (index == 0) { node.next = this.headNode; headNode = node; }else if (index == length) { this.tailNode.next = node; tailNode = node; }else { Node otherNode = headNode; while (index > 1) { otherNode = otherNode.next; index--; } node.next = otherNode.next; otherNode.next = node; } length++; } /* * 删除链表中的一个结点 * @param int index 所需删除的结点的位置 */ public void delete(int index){ if (index < 0 || index >= length) { throw new IndexOutOfBoundsException(); } if (index == 0 && length != 1) { this.headNode = headNode.next; length--; }else if(index == 0 && length == 1){ this.headNode = this.tailNode = null; length = 0; }else { Node otherNode = this.headNode; while (index > 1) { otherNode = otherNode.next; index--; } otherNode.next = otherNode.next.next; length--; } } /* * 查看某一处结点的值 * @param int index 所需查看的结点的位置 */ public Object indexOf(int index){ if (index < 0 || index >= length) { throw new IndexOutOfBoundsException(); } Node otherNode = this.headNode; while (index > 0) { otherNode = otherNode.next; index--; } return otherNode.data; } /* * 设置某一处结点的值 * @param int index 结点的位置 ,Object data 修改后的结点的值 */ public void setData(int index,Object data){ if (index < 0 || index >= length) { throw new IndexOutOfBoundsException(); } Node otherNode = this.headNode; while (index > 0) { otherNode = otherNode.next; index--; } otherNode.data = data; } /* * 依次打印所以结点 */ public void print(){ Node otherNode = this.headNode; while(otherNode.hasnext()){ System.out.print(otherNode.data+" "); otherNode = otherNode.next; } System.out.println(otherNode.data); } } class Node{ /* * 节点类,包括结点的值 data 和指向下一结点的指针 next */ Object data; Node next; public Node(){ data = null; next = null; } public Node(Object data){ this.data = data; this.next = null; } public Node(Object data,Node nextnode){ this.data = data; this.next = nextnode; } /* * 判断有无下一结点 * @return boolean 若true,则该结点有下一结点 */ public boolean hasnext(){ if (this.next != null) { return true; }else { return false; } } }
相关文章推荐
- 几种基本数据结构--栈、队列、双向链表、有根树的分析和简单实现
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- 算法与数据结构基础系列(一): 链表的常见问题分析及实现
- Android版数据结构与算法(三):基于链表的实现LinkedList源码彻底分析
- 算法与数据结构-单向链表的基本操作C语言实现
- 数据结构基本算法:图的存储(以邻接链表为例)
- 数据结构学习笔记之链表分析与实现(一)
- 实现单链表各种基本运算的算法
- 用C#基本语句实现的链表算法
- 基本数据结构的实现——单向链表
- 一步步学习数据结构和算法之快速排序效率分析及java实现
- 基本算法实现小结(一)—— 单链表
- 用Python实现的数据结构与算法:链表
- 基本算法实现小结(一)—— 单链表
- 数据结构学习笔记之链表分析与实现(二)
- 算法数据结构C++实现8 堆排序 难点分析
- 一步步学习数据结构和算法之快速排序效率分析及java实现
- 字典树的数据结构及基本算法的实现
- 数据结构基本算法java实现
- 多核计算与程序设计 - 06 基本算法和数据结构 之三 哈希表与哈希链表