数据结构:顺序表
顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。
顺序表的特点:
①顺序表的逻辑顺序和物理顺序是一致的,但是物理位置比逻辑位置少1,原因是物理顺序是从0开始的,而逻辑顺序是从1开始的。
②顺序表中任意一个数据元素可以随机存取,即顺序表是一种随机存取的数据结构。
顺序表相关操作(代码)
顺序表操作方法
class MyArrayList{
//1.创建顺序表 ①静态顺序表:静态数组存储 ②动态顺序表:动态开辟的数组存储
private int usedSize; //代表顺序表的实际长度
private int[] elem; //代表数组
public MyArrayList(int size){
this.elem = new int[size]; //动态顺序表: 动态开辟数组存储
this.usedSize = 0;
}
public MyArrayList(){
this(0);
this.elem = new int[10]; //静态顺序表: 定长数组存储
}
//2.打印顺序表中内容
public void display(){
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i]+" ");
}
System.out.println();
}
//3.判断顺序表是否已满,满则返回true,否则返回false
public boolean isFull(){
//当顺序表的实际长度等于数组长度时,表明顺序表元素已满
if(this.usedSize == this.elem.length){
System.out.println("顺序表已经写满!");
return true;
}
return false;
}
//4.扩充顺序表的长度
//利用Arrays.copyof进行扩容
public void extension(){
this.elem = Arrays.copyOf(this.elem, this.elem.length*2);
}
//5.在pos位置插入元素
public void insert(int pos, int target){
//①判断顺表是否已满,如果满则进行扩充
if(isFull()){
System.out.print("进行顺序表扩容: ");
extension();
}
//②判断pos位置的合法性
if(pos < 0 || pos > this.usedSize){
System.out.println("pos位置不合法!");
return ;
}
//③把pos位置及以后的元素全部向后挪一个位置
for (int i = this.usedSize-1; i >=pos; i--) { //提示替换
this.elem[i+1] = this.elem[i];
}
this.elem[pos] = target; //将target赋值给pos位置
this.usedSize++; //顺序表长度+1
}
/下面这种方式默认将元素插入到顺序表的最后面
public void insert1(int target){
//①判断顺表是否已满,如果满则进行扩充
if(isFull()){
extension();
}
this.elem[usedSize] = target; //顺序表最后位置插入target
this.usedSize++; //更新顺序表长度
}
//6.查找指定元素的下标,如没有指定元素则返回-1
public int search(int target){
int i = 0;
//遍历查找指定元素
while(i < this.usedSize){
if(this.elem[i] == target){
return i;
}
i++;
}
return -1;
}
//7.判断是否包含指定元素,没有就返回false
public boolean contain(int target){
for (int i = 0; i < this.usedSize; i++) {
if(this.elem[i] == target){
return true;
}
}
return false;
}
//8.获取顺序表的长度
public int getLength(){
return this.usedSize;
}
//9.获取指定位置的元素
public int getPos(int pos){
//判断pos位置的合法性
if(pos < 0 || pos >= usedSize){
return -1;
}
return this.elem[pos];
}
//10.将pos位置的元素修改为target
//判断pos位置的合法性
public int setPos(int pos, int target){
if(pos < 0 || pos >= this.usedSize){
return -1;
}
return this.elem[pos] = target;
}
//11.删除第一次出现的关键字key
public void remove(int key){
int index = search(key); //调用search方法查看有无关键字,index代表下标
//index为-1表明顺序表中无该关键字
if(index == -1){
System.out.println("没有该关键字啊铁铁!");
}
//有该关键字时,删除第一次出现的key,并把key后面的数全部向前挪一个位置
for (int i = index; i < this.usedSize-1; i++) {
this.elem[i] = this.elem[i+1];
}
this.usedSize--; //更新顺序表长度 哈哈哈之前头昏写成了++
}
//12.清空顺序表以防止内存泄露
public void clear(){
this.usedSize = 0; //直接让顺序表的长度为0,意味着顺序表中已经没有了元素
}
}
操作方法测试
public class ShunXuBiao {
public static void main(String[] args) {
MyArrayList test = new MyArrayList();
System.out.print("第一次打印顺序表: ");
test.display();
System.out.println();
System.out.print("插入10个元素: ");
for (int i = 0; i < 10; i++) {
test.insert(i,2*i);
}
test.display();
System.out.println();
System.out.print("再插入10个元素: ");
for (int i = 10; i < 20; i++) {
test.insert(i,2*i);
}
test.display();
System.out.println();
System.out.println("在9下标位置插入一个元素9: ");
System.out.println("在顺序表最后位置插入元素100: ");
test.insert(9,9);
test.insert1(100);
test.display();
System.out.println();
System.out.print("查找元素18,该数据的下标为: "+test.search(18));
System.out.println();
System.out.println("判断顺序表是否包含元素24: "+test.contain(24));
System.out.println();
System.out.println("查找12位置对应的元素: "+test.getPos(12));
System.out.println();
System.out.print("删除元素30: ");
test.remove(30);
test.display();
System.out.println();
// System.out.print("删除元素11: ");
// test.remove(11);
// test.display();
// System.out.println();
System.out.println("得到顺序表的长度: "+test.getLength());
System.out.print("清空顺序表: ");
test.clear();
test.display();
System.out.println();
}
}
结果展示
原创 欢迎讨论!