单向链表,单向循环链表的基本操作
2013-04-21 10:59
405 查看
链表可以解决数组对存储空间要求的问题,可以充分的利用存储空间,可以根据实际使用的需要来使用内存,链表的插入节点和删除节点都数组要简单,因为只要用指针加以处理就行了 ,但是在数组的查找上,数组的速度比链表快,因为从数组的索引就可以找到想要的数据,而链表需要花费很多时间去比较每一个节点,才能找到自己想要数据。
单向链表的每一个节点的数据结构都可以分为两个域,一个数据域,一个是指针域,因此可以以此来构建一个节点类
下面的代码实现了获取所有的链表节点,增加,删除和链表的反转
1.链表的插入 假设需要在prev节点和current节点之间插入节点node ,只需要将prev.next = node ; node.next = current即可
2.链接的删除,需要删除节点prev的后一个节点current,那么只要将prev.next = current.next; current = null即可./
3.获取链表的长度,需要注意的是,while循环之中,是current != null 而不是 current.next != null,这两个的区别需要弄清楚。
4.链表的反转,需要借助三个节点,prev ,current ,forward,那么每一次循环实现,current 节点指向其前一个节点prev ,直到current 节点是最后一个节点。
单向链表的每一个节点的数据结构都可以分为两个域,一个数据域,一个是指针域,因此可以以此来构建一个节点类
public class NodeType { public int data; //存放节点的数据 public NodeType next; //存放下一个节点的引用 public NodeType(int data){ this.data = data; } public NodeType(){ } }
下面的代码实现了获取所有的链表节点,增加,删除和链表的反转
public class Test { public void add(NodeType head,NodeType node){ NodeType prev = null; NodeType current = head.next; while( current != null && current.data < node.data){ prev = current; current = current.next; } prev.next = node; node.next = current; } /** * * @param head * @param node 要被删除的节点 */ public void romving(NodeType head,NodeType node){ NodeType current = head.next; NodeType prev = null; while(current != null && current.data != node.data){ prev = current; current = current.next; } if(current == null){ System.out.println("您输入的节点不存在"); }else{ prev.next = current.next; current = null; } } /** * * @param head * @return 链表的长度 */ public int getlength(NodeType head){ NodeType current = head.next; int length = 0; while(current != null){ length ++; current = current.next; } return length; } /** * * @param head * 获取所有的节点 */ public void getAllNode(NodeType head){ NodeType current = head.next; while(current != null){ System.out.print(" "); System.out.print(current.data); current = current.next; } } public NodeType invert(NodeType head){ NodeType forward = head.next; NodeType current = null; NodeType prev = null; while(forward != null){ prev = current; current = forward; forward = forward.next; current.next = prev; } return current; } /** * * 测试链表的长度,链表的反转,链表的删除和增加 */ public static void main(String args[]){ NodeType head = new NodeType(); NodeType a = new NodeType(55); NodeType b = new NodeType(64); NodeType c = new NodeType(78); NodeType d = new NodeType(85); NodeType e = new NodeType(98); NodeType f = new NodeType(112); head.next = a; a.next = b; b.next = c; c.next = d; d.next = e; e.next = f; Test test = new Test(); test.add(head, new NodeType(95)); test.getAllNode(head); test.romving(head, e); test.getAllNode(head); System.out.println(test.getlength(head)); NodeType newHead = test.invert(head); System.out.println(newHead.data + "*"); test.getAllNode(newHead); } }
1.链表的插入 假设需要在prev节点和current节点之间插入节点node ,只需要将prev.next = node ; node.next = current即可
2.链接的删除,需要删除节点prev的后一个节点current,那么只要将prev.next = current.next; current = null即可./
3.获取链表的长度,需要注意的是,while循环之中,是current != null 而不是 current.next != null,这两个的区别需要弄清楚。
4.链表的反转,需要借助三个节点,prev ,current ,forward,那么每一次循环实现,current 节点指向其前一个节点prev ,直到current 节点是最后一个节点。
相关文章推荐
- 单向循环链表的创建及基本操作的实现
- 数据结构_线性表_链式存储_单向循环链表的基本操作
- 单向循环链表的基本操作
- <数据结构与算法>单向循环链表基本框架(C语言描述)
- c语言实现--单向循环链表操作
- 双向循环链表基本操作
- C语言单链表,双向链表,循环单链表,循环双链表的基本操作
- 数据结构-单向有头不循环链表基本实例
- 数据结构-单向无头不循环链表基本实例
- 单向链表基本操作(C语言实现)
- 双向循环链表的基本操作
- 手把手写数据结构之单向循环链表操作
- 单向循环链表操作
- 双向循环链表的基本操作
- 单向链表的基本操作
- c语言实现循环链表的基本操作
- 数据结构学习之双向循环链表的基本操作(非递归实现)
- 名单(两)——基本操作单向链表(创、删、印、节点统计数)
- 数据结构——单向链表的基本操作C语言描述(克服对头结点和尾结点的操作)
- 数据结构学习笔记(二) 链表之带表头结点的双循环链表基本操作