前言
hello,大家好,我是阿旭啊。有朋友跟我说,阿旭阿旭,我没学过java,是不是看不懂你的数据结构啊?其实不是的,数据结构注重的是思想,理解了数据结构的原理就可以用任何语言写出来了,当然如果有同学想用其他语言来实现数据结构也可以来问我,我会竭尽所能去帮助大家。
今天我给大家带来顺序表的实现,希望能给大家一些小帮助~
一.认识List
1.什么是List
在集合框架中,List是一个接口,继承自Collection,Collection也是一个接口,继承自Lterable。
在三个接口中都有很多的方法。站在数据结构的角度来看List就是一个线性表,即n个具有相同类型元素的有限序列,在该序列上可以执行增删改查以及变量等操作。
2.List的使用
注意:List是一个接口,并不能直接用来实例化。
如果要使用,必须去实现List的实现类。在集合框架中,ArrayList和LinkedList等等都实现了List接口。如:
public static void main(String[] args) {
List<Integer> stake = new Stack<>(); //栈
List<Integer> arrayList = new ArrayList<>(); //顺序表
List<Integer> linkedList = new LinkedList<>(); //链表
}
二.线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
链表在逻辑上连续,物理上是不连续的(之后会慢慢给大家讲到)。
1.顺序表
顺序表本质上是通过方法来操作数组。是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
2.顺序表(SeqList)的实现
首先,我们需要在类中定义一个数组,我这里用的是int数组elem。如果我们需要可以实现修改数组,那么我们就需要知道这个数组有多少个有效数据,这里我定义一个int数据usedSize来表示有效数据。随后我们写一个空的构造方法来设置这个数组的元素的最大个数。
public class SeqList {
private int[] elem;
private int usedSize; //记录当前有多少个有效数字
public SeqList(){
this.elem = new int[10];
}
}
顺序表有很多个方法:
// 新增元素,默认在数组最后新增
public void add(int data) { }
// 在 pos 位置新增元素
public void add(int pos, int data) { }
// 判定是否包含某个元素
public boolean contains(int toFind) { return true; }
// 查找某个元素对应的位置
public int indexOf(int toFind) { return -1; }
// 获取 pos 位置的元素
public int get(int pos) { return -1; }
// 给 pos 位置的元素设为 value
public void set(int pos, int value) { }
//删除第一次出现的关键字key
public void remove(int toRemove) { }
// 获取顺序表长度
public int size() { return 0; }
// 清空顺序表
public void clear() { }
// 打印顺序表,注意:该方法并不是顺序表中的方法,为了方便看测试结果给出的
public void display() { }
}
我们将一一来实现他们。
首先来实现打印顺序表display(),由于是一个数组,所以我们直接使用一个for循环就可以实现这个方法:
public class SeqList {
private int[] elem;
private int usedSize; //记录当前有多少个有效数字
public SeqList(){
this.elem = new int[10];
}
// 打印顺序表,注意:该方法并不是顺序表中的方法,为了方便看测试结果给出的
public void display() {
//i要小于有效值不然会打印后面的0
for (int i = 0; i < this.usedSize; i++) {
System.out.println(this.elem[i] + " ");
}
System.out.println();
}
}
然后我们来实现添加元素方法add(),只传入一个数据,默认在数组最后一位添加:
// 新增元素,默认在数组最后新增
public void add(int data) {
this.elem[this.usedSize] = data;
usedSize++;
}
}
我们完成add方法了吗?并没有,请注意数据结构是一门很严谨的科目,当数组满了之后还能用我们这个方法吗?答案是否定的,所以,我们需要判满,判断数组是否已经满了。如果数组满了,我们可以使用copyof方法来进行扩容:
// 新增元素,默认在数组最后新增
public void add(int data) {
if (this.usedSize == this.elem.length) {
this.elem = Arrays.copyOf(this.elem, 2 * this.usedSize);
}
this.elem[this.usedSize] = data;
usedSize++;
}
然后我们实现判定是否包含某个元素的方法contains,查找某个元素对应的位置的方法indexOf,这两个方法都是遍历整个数组来进行判断:
// 判定是否包含某个元素
public boolean contains(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if (toFind == this.elem[i]){
return true;
}
}
return false;
}
// 查找某个元素对应的位置
public int indexOf(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if (toFind == this.elem[i]){
return i;
}
}
return -1;
}
然后我们实现获取pos位置的元素方法get,要注意当我们要查找的位置不合法,也就是小于0或者大于有效值的时候就会报错,为了完善这个程序,我们自定义一个获取位置不合法的异常(PosOutBoundException)来抛出使这个程序更容易理解:
public class PosOutBoundsException extends RuntimeException{
public PosOutBoundsException() {
}
public PosOutBoundsException(String message) {
super(message);
}
}
然后我们可以写get函数啦~:
// 获取 pos 位置的元素
public int get(int pos) {
if (pos < 0 || pos >=this.usedSize ){
throw new PosOutBoundsException("获取数据时,位置不合法");
}
return this.elem[pos];
}
下面我们写获取顺序表的长度的方法size,我们只需要返回有效值即可:
// 获取顺序表长度
public int size() {
return this.usedSize;
}
下面我们来写更新pos位置的元素的方法set:
public void set(int pos, int value) {
if (pos < 0 || pos >= this.usedSize){
throw new PosOutBoundsException("获取数据时,位置不合法");
}
this.elem[pos] = value;
}
下面我们开始写在pos位置新增元素的add方法,我们可以发现和第一个add方法组成了重载。在pos位置新增一个元素,我们需要先判断插入位置是否合法,合法位置为0~usedSize,如果是usedSize,那么就是在末位添加。然后我们把pos位置及其之后未知的数据往后移一位,在移位过程中我们还要考虑数组是否已经满的问题,所以我们要先判满。判满之后进行移位,移位之后把pos位置的数据替换成我们新增的数据,最后usedSize加一就完成了。顺序是:判断位置合法->判满->移位->替换数据->有效个数加一,下面我们开始写这个重载方法:
public void add(int pos, int data) {
if (pos < 0 || pos > this.usedSize){
throw new PosOutBoundsException("获取数据时,位置不合法");
}
if (this.usedSize == this.elem.length) {
this.elem = Arrays.copyOf(elem, 2 * this.usedSize);
}
for (int i = this.usedSize - 1; i >= pos; i--) {
this.elem[i + 1] = this.elem[i];
}
this.elem[pos] = data;
this.usedSize++;
}
下面我们进行删除第一次出现的某个元素,首先我们需要知道要删除元素的下标pos,当然有可能给的数据根本就没有,此时我们可以再写一个ValueOutBoundsException异常来进行报错,然后从pos开始,后一个值覆盖掉前一个值,最后有效值减一,如果数组类型为引用类型,那么还需要将最后一位那个多余的数值进行置null:
public class ValueOutBoundsException extends RuntimeException{
public ValueOutBoundsException() {
}
public ValueOutBoundsException(String message) {
super(message);
}
}
public void remove(int toRemove) {
int pos = indexOf(toRemove);
if (pos == -1){
throw new ValueOutBoundsException("没有你要删除的数据");
}
for (int i = pos; i < this.usedSize - 1; i++){
this.elem[i] = this.elem[i + 1];
}
this.usedSize--;
// this.elem[this.usedSize] = null;
}
最后就是置空顺序表clear方法,很简单,直接有效数置0即可,要注意如果数组类型为引用类型,我们还需要每一位 置null:
public void clear() {
// for (int i = 0; i < this.usedSize; i++) {
// this.elem[i] = null;
// }
this.usedSize = 0;
}
3.完整代码加运行代码
完整顺序表代码:
import java.util.Arrays;
public class SeqList {
private int[] elem;
private int usedSize; //记录当前有多少个有效数字
public SeqList(){
this.elem = new int[2];
}
// 打印顺序表,注意:该方法并不是顺序表中的方法,为了方便看测试结果给出的
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.println(this.elem[i] + " ");
}
System.out.println();
}
// 新增元素,默认在数组最后新增
public void add(int data) {
if (this.usedSize == this.elem.length) {
this.elem = Arrays.copyOf(this.elem, 2 * usedSize);
}
this.elem[this.usedSize] = data;
usedSize++;
}
// 判定是否包含某个元素
public boolean contains(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if (toFind == this.elem[i]){
return true;
}
}
return false;
}
// 查找某个元素对应的位置
public int indexOf(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if (toFind == this.elem[i]){
return i;
}
}
return -1;
}
// 获取 pos 位置的元素
public int get(int pos) {
if (pos < 0 || pos >=this.usedSize ){
throw new PosOutBoundsException("获取数据时,位置不合法");
}
return this.elem[pos];
}
// 获取顺序表长度
public int size() {
return this.usedSize;
}
// 给 pos 位置的元素设为 value
public void set(int pos, int value) {
if (pos < 0 || pos >= this.usedSize){
throw new PosOutBoundsException("获取数据时,位置不合法");
}
this.elem[pos] = value;
}
//添加pos位置数据位data
public void add(int pos, int data) {
if (pos < 0 || pos > this.usedSize){
throw new PosOutBoundsException("获取数据时,位置不合法");
}
if (this.usedSize == this.elem.length) {
this.elem = Arrays.copyOf(elem, 2 * this.usedSize);
}
for (int i = this.usedSize - 1; i >= pos; i--) {
this.elem[i + 1] = this.elem[i];
}
this.elem[pos] = data;
this.usedSize++;
}
//删除第一次出现的关键字key
public void remove(int toRemove) {
int pos = indexOf(toRemove);
if (pos == -1){
throw new ValueOutBoundsException("没有你要删除的数据");
}
for (int i = pos; i < this.usedSize - 1; i++){
this.elem[i] = this.elem[i + 1];
}
this.usedSize--;
// this.elem[this.usedSize] = null;
}
// 清空顺序表
public void clear() {
// for (int i = 0; i < this.usedSize; i++) {
// this.elem[i] = null;
// }
this.usedSize = 0;
}
}
两个自定义异常:
public class ValueOutBoundsException extends RuntimeException{
public ValueOutBoundsException() {
}
public ValueOutBoundsException(String message) {
super(message);
}
}
public class PosOutBoundsException extends RuntimeException{
public PosOutBoundsException() {
}
public PosOutBoundsException(String message) {
super(message);
}
}
测试代码:
public class test {
public static void main(String[] args) {
SeqList seqList = new SeqList();
seqList.add(2);
seqList.display();
System.out.println("=====================");
seqList.set(0,1);
seqList.display();
System.out.println("=====================");
seqList.add(1,2);
seqList.display();
System.out.println("=====================");
seqList.remove(1);
seqList.display();
System.out.println("=====================");
System.out.println(seqList.contains(1));
System.out.println(seqList.contains(2));
System.out.println("=====================");
System.out.println(seqList.indexOf(1));
System.out.println("=====================");
System.out.println(seqList.get(0));
System.out.println("=====================");
System.out.println(seqList.size());
System.out.println("=====================");
seqList.clear();
seqList.display();
System.out.println("没东西哦~~~~");
System.out.println("=====================");
}
}
运行结果:
=====================
2
=====================
1
=====================
1
2
=====================
2
=====================
false
true
=====================
-1
=====================
2
=====================
1
=====================
没东西哦~~~~
=====================
总结
本篇关于顺序表的实现就到这里结束啦~大家有什么不会的可以直接来问我(虽菜但爱玩),其他语言也可以哦(C,Python)。希望大家能够多多点赞,多多评论,多多批评,大家下一篇再见