java中数据结构(一)单链表总结
来源:互联网 发布:淘宝客服指南 编辑:程序博客网 时间:2024/06/06 18:29
单链表是用若干在地址上面分散内存单元存储数据元素的,在逻辑上面相邻的元素在物理上不一定相邻.因此对于每个数据元素除了存储自身的元素数据以外,还要存储指向下个数据元素的地址。数据元素结构如下:上面包含了自身元素数据data和指向下个数据元素地址的next.
数据元素存储结构:
单链表是顺序存储结构,不是随机存储结构,因此查询节点的时候每次都是从头节点开始查找,因此对于select和updata操作性能不高,每次都需要从头节点进行查找更改.然而由于它不需要移动数据元素,只需要修改地址指向所以add和delete操作性能好。
下面是一个实现单链表的例子,为了加深记忆,所以这里做了下总结.
Node节点
public class Node<E> {//单链表的节点 public E data; public Node<E> next; public Node(E data) { this.data = data; } public Node(){ this(null); }}
实现了对Node节点增删改查的链表
public class NodeLinkList<E> { private Node<E> head;//单链表的头节点 public boolean isEmpty() {//判断链表是否为空 return head == null; } public int length() {//获取链表的长度 int len = 0; Node<E> node = head; while (node != null) { len++; node = node.next; } return len; } public Node<E> getHead() {//获取链表的头节点 return head; } public void clear() {//清空链表 head = null; } public boolean add(int index, E element) {//在指定索引位置添加节点 if (index < 0) { throw new IllegalArgumentException("index is" + index + " IllegalArgumentException"); } Node<E> p = new Node<E>(element); if (head == null) {//空链表 head = p; } else { if (index == 0) {//在头部位置插入节点,即新节点为头部节点 p.next = head; head = p; } else {//在链表中间添加新节点 Node<E> q = head; int i = 0; while (q != null && i < index - 1) {//获取索引为index-1的节点tempNode q = q.next; i++; } p.next = q.next; q.next = p; } } return true; } public void add(E element){//在头部添加节点 Node<E> node=new Node<>(element); node.next=head; head=node; } public boolean delete(int index) {//删除指定索引的节点 if (head == null || index < 0) { return false; } else if (index == 0) {//删除头节点 head = head.next; } else {//删除中间节点和尾部节点 Node<E> p = head; int j = 0; while (p != null && j < index - 1) {//查找index-1索引的节点 p = p.next; j++; } p.next = p.next.next; } return true; } public boolean update(int index, E element) {//修改指定节点index的值 if (head == null || index < 0 || element == null) { return false; } else { if (index == 0) {//修改头节点的值 E oldElement = head.data; head.data = element; } else {//修改中间节点的值 Node<E> p = head; int j = 0; while (p != null && j < index - 1) {//查找index-1索引的节点 p = p.next; j++; } Node<E> q = p.next; q.data = element; } } return true; } public Node select(int index) { if (index < 0 || head == null) { return null; } else { if (index == 0) {//查询头节点 return head; } else { Node<E> p = head; int j = 0; while (p != null && j < index - 1) {//查找index-1索引的节点 p = p.next; j++; } return p.next; } } } public void print() { Node<E> q = head; while (q != null) { System.out.println(q.data); q = q.next; } }}
阅读全文
0 0
- java中数据结构(一)单链表总结
- java中数据结构(二)双链表总结
- 数据结构总结(一)
- java数据结构(一)
- Java数据结构(一) ---链表
- Java工作中常用工具类总结一
- Java中多线程学习总结(一)
- Java 数据结构总结
- java常见数据结构总结
- Java基本数据结构总结
- Java常用数据结构总结
- Java数据结构总结
- 数据结构学习总结(一)
- 数据结构链表总结一
- Nginx高级数据结构总结(一)
- opencv中图像数据结构总结
- 数据结构中内部排序总结
- java中常用数据结构
- 零、es6 新特性目录
- STM32学习笔记3:时钟系统
- sublime text设置使用python
- Java错误随笔
- 20170907_我是如何讲清楚TCP协议是如何保证可靠传输的
- java中数据结构(一)单链表总结
- 简单介绍spark
- 机器学习常用算法一:多元线性回归
- php调用cmd命令乱码问题
- 2017中国IT教育博鳌论坛培训内容
- Android中assets目录和res/raw目录的异同和使用场景
- java 定时任务的集中方式
- iOS改变控件图层叠放顺序
- 使用Leak Canary检测应用的内存泄露