zoukankan      html  css  js  c++  java
  • 重学数据结构(二)——对线性表的顺序表进行实现

    重学数据结构(二)

    对线性表的顺序表进行实现

    public abstract class MyList<T>{
        //定义顺序表的初始容量
        final  int maxSize = 10;
        //对数据元素进行存储
        private T[] list;
        //顺序表中元素的个数
        private int length;
        
        public MyList(){}
        public MyList(int n){}
       
        public boolean add(Object obj,int pos);
        public T remove(int pos);
        public int find(Object obj);
        public T value(int pos);
        public boolean modify(T obj , int pos);
        public boolean isEmpty();
        public int size();
        public void clear();
        
    }
    

    实现注意事项:

    add实现: 注意pos的大小以及list的内存是否够用。插入之前,pos位置以后的元素进行后移。

    remove:注意pos的大小以及顺序表是否为空,删除操作后,后面的元素需要前移

    package com.codezs.datastruct;
    
    public class MyList<T> {
        final  int maxSize = 10;
        private T[] list;
        private int length;
    
        //无参初始化
        public MyList() {
            length = 0;
            list = (T[]) new Object[maxSize];
        }
    
        //有参初始化,定义顺序表的大小
        public MyList(int n) {
            if (n<=0){
                System.out.println("error");
                System.exit(1);
            }
            length = 0;
            list =(T[]) new Object[n];
        }
    
        //在指定位置对顺序表进行添加操作
        public boolean add(T obj , int pos){
            if (pos<1 ||pos >length +1){
                System.out.println("pos值不符合规定");
                return false;
            }
            if (length == list.length){
                T[] p  = (T[]) new Object[maxSize*2];
                for (int i = 0; i <length ; i++) {
                    p[i] = list[i];
                }
                list =p;
            }
    
            for (int i = length; i >= pos; i--) {
                list[i] = list[i -1];
            }
            list[pos -1] = obj;
            length++;
    
            return true;
        }
    //删除操作
        public T remove(int pos){
            if (isEmpty()){
                System.out.println("顺序表为空,无法执行删除操作");
                return null;
            } else {
                if (pos<1 || pos>length){
                    System.out.println("pos值不符合规定");
                    return null;
                }
            }
            T x = list[pos -1];
    
            for (int i = pos; i <= length ; i++) {
                list[i-1] = list[i];
            }
            length--;
            return x;
        }
    //根据数组元素返回当前元素在顺序表的位置
        public int find(Object obj){
            if (isEmpty()){
                System.out.println("顺序表为空,无法执行查询操作");
                return -1;
            }
            for (int i = 0; i < length ; i++) {
                if(list[i].equals(obj)){
                    return i+1;
                }
            }
            return -1;
        }
    //返回在pos位置下的数据元素
        public T value(int pos){
            if (isEmpty()){
                System.out.println("顺序表为空,无法执行查询操作");
                return null;
            } else {
                if (pos<1 || pos>length){
                    System.out.println("pos值不符合规定");
                    return null;
                }
                return list[pos -1];
            }
    
        }
    //对pos位置的数据元素进行修改
        public boolean modify(T obj , int pos){
            if (isEmpty()){
                System.out.println("顺序表为空,无法执行修改操作");
                return false;
            } else {
                if (pos<1 || pos>length){
                    System.out.println("pos值不符合规定");
                    return false;
                }
                list[pos -1]= obj;
                return true;
            }
        }
    //判断顺序表是否为空
        public boolean isEmpty(){
            return length==0;
        }
    //获取顺序表中元素的个数
        public int size(){
            return length;
        }
    //对顺序表进行清空操作
        public void clear(){
            for (int i = 0; i <length;i++){
                list[i] =null;
            }
            length=0;
        }
    }
    
    
  • 相关阅读:
    Oracle 实现数据表插入时主键列自增
    Java BigDecimal 加减乘除用法
    php最常见最经典的算法题(1)
    2、php-选择排序方法
    1、php-冒泡排序方法
    65G-一系列Go语言课程Go基础知识高级Go项目战斗New Go语言和区块链开发实践课程
    css
    python基础
    SEO优化指南
    转载GXT之旅
  • 原文地址:https://www.cnblogs.com/zsiscool/p/13355238.html
Copyright © 2011-2022 走看看