Java与数据结构(一) 顺序表
2017-10-11 18:44
316 查看
这学期开了数据结构课,教材是清华的C语言版,现用Java来实现。
线性表是最基本、最简单、也是最常用的一种数据结构。
线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的(注意,这句话只适用大部分线性表,而不是全部。比如,循环链表逻辑层次上也是一种线性表(存储层次上属于链式存储),但是把最后一个数据元素的尾指针指向了首位结点)。
我们说“线性”和“非线性”,只在逻辑层次上讨论,而不考虑存储层次,所以双向链表和循环链表依旧是线性表。
在数据结构逻辑层次上细分,线性表可分为一般线性表和受限线性表。一般线性表也就是我们通常所说的“线性表”,可以自由的删除或添加结点。受限线性表主要包括栈和队列,受限表示对结点的操作受限制。
线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。
线性表是最基本、最简单、也是最常用的一种数据结构。
线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的(注意,这句话只适用大部分线性表,而不是全部。比如,循环链表逻辑层次上也是一种线性表(存储层次上属于链式存储),但是把最后一个数据元素的尾指针指向了首位结点)。
我们说“线性”和“非线性”,只在逻辑层次上讨论,而不考虑存储层次,所以双向链表和循环链表依旧是线性表。
在数据结构逻辑层次上细分,线性表可分为一般线性表和受限线性表。一般线性表也就是我们通常所说的“线性表”,可以自由的删除或添加结点。受限线性表主要包括栈和队列,受限表示对结点的操作受限制。
线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。
package 数据结构和算法; import java.util.Arrays; import java.util.Date; /** * 顺序表ArrayList,用数组表示。一组连续的地址空间 * @author LH-PC * @param <E> */ public class ArrayList<E> { private Object[] data = null; //data 用来保存此线性表的数据域 private int length; //线性表的容量 private int current=0; //实际表长 /** * 默认将大小设置为10 */ public ArrayList(){ this(10); } /** * 初始化线性表,声明数组大小 * @param initialSize 数组大小 */ public ArrayList(int initialSize){ if(initialSize >= 0){ this.length = initialSize; //设置线性表容量 this.data = new Object[initialSize]; //初始化数组 this.current = 0; //下标设置为0 }else { throw new RuntimeException("初始化大小不能小于0:" + initialSize); //异常提示 } } /** * 在线性表末尾添加元素,添加之前判断线性表是否已经满了 * @param e 添加的元素 * @return 成功返回真 */ public boolean add(E e){ //判断是否已满 ensureCapacity(); //将元素添加到数组末尾 this.data[current] = e; current++; // ++current; //下标++ return true; } /** * 删除指定位置的元素 * @param index * @return */ public boolean removeToIndex(int index){ //删除数组的元素:使用改变数组下标的方式达到删除的效果。 //遍历数组匹配指定下标,让指定下标右边的元素往左移动改变下标。最后再将最右边的下标删除 //a b c //0 1 2 //data[index] = data[index + 1]; //改变右边下标 //data //删除最右边的下标 //从待删除下标处开始遍历,将右边的元素往左移 if(index >= current){ //如果index大于最大长度,返回假 System.err.print(new Date() + ": 下标超出表长"); return false; } for (int i = index; i < current - 1; i++) { data[i] = data[i+1]; //该表元素下标 } data[current-1] = null; //将原来下标最右边的一位元素变成null --current; //实际表长-1 return true; } public boolean Insert(int index,E x) { if(index<1||index>length+1) return false; for(int i=current-1;i>=index-1;i--) data[i+1] = data[i];//下表向后移动 data[index-1] = x; current++;//实际表长+1 return true; } /** * 根据下标返回元素值 * @param index * @return */ public E get(int index){ if(index >= 0){ return (E) data[index]; } else { throw new RuntimeException("下标不能小于0:" + index); } } /** * 判断表容量是否超出预定大小,如果超出将自动扩充容量 * */ public void ensureCapacity(){ //判断表实际长度是否超出表最大容量 if(current >= length){ length *= 2; //将表最大容量*2 data = Arrays.copyOf(data, length); //将原数组进行拷贝 } } /** * 返回顺序表实际表长 * @return */ public int size(){ return this.current; } /** * 返回表容量 * @return */ public int length(){ return this.length; } /** * * 判断表是否为空 * @param args */ public boolean isEmpty(){ //return (current == 0) ? true : false; return current == 0; //如果current == 0,说明为空返回真,否则返回假 } //主方法测试 public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); //创建arrayList for (int i = 0; i <= 22; i++) { list.add(i); } list.removeToIndex(0); // list.removeToIndex(list.size()); //遍历list数组 for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)+" "); } System.out.println(); list.Insert(1, 1); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)+" "); } list.Insert(23, 23); System.out.println(); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)+" "); } } }
OutPut:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 23 22
相关文章推荐
- java 中的JDK封装的数据结构和算法解析(集合类)----顺序表 List 之 ArrayList
- JAVA数据结构---顺序表
- JAVA数据结构---顺序表
- 数据结构的Java实现——顺序表
- JAVA数据结构---顺序表
- 数据结构Java实现02----线性表与顺序表
- JAVA数据结构---顺序表
- JAVA数据结构---顺序表
- Java 数据结构 自定义顺序表
- 数据结构Java实现02----线性表与顺序表
- JAVA数据结构---顺序表
- Java数据结构-线性表之顺序表ArrayList
- 数据结构-顺序表 java实现
- java实现数据结构-线性表-顺序表,实现插入,查找,删除,合并功能
- 数据结构的Java实现——顺序表
- 数据结构(java语言描述)线性表-顺序表
- 数据结构-顺序表Java简单实现
- 数据结构的Java实现——顺序表
- java 数据结构--顺序表
- JAVA数据结构---顺序表