zoukankan      html  css  js  c++  java
  • 顺序表的实现

    顺序表的概念:顺序表是线性表的一种,其中的每个数据前后的逻辑顺序与在存储器中的物理顺序相同。

    顺序表的优点:存储简单,查找简单。

        缺点:插入和删除某个元素需要大量的移动其他数据,时间复杂度高。

    java实现

    接口文件Slist

    package SequenceTable;
    
    public interface Slist {
        public void clear();
        public boolean empty();
        public int length();
        public Object get(int i) throws Exception;
        public void insert(int i, Object x) throws Exception;
        public void remove(int i) throws Exception;
        public int indexof(Object x);
        public void display();
    
    }

    实现类

    package SequenceTable;
    
    public class SqList implements Slist{
        private Object[] listElem; // 初始化元素内容
        private int curLen; // 初始化当前元素长度
    
        // 带参构造函数
        public SqList(int maxSize){
            curLen = 0;
            listElem = new Object[maxSize];
        }
        // 方法重写
        @Override
        public void clear() {
            curLen = 0;
        }
    
        @Override
        public boolean empty() {
            return curLen == 0;
        }
    
        @Override
        public int length() {
            return curLen;
        }
    
        //
        @Override
        public Object get(int i) throws Exception {
            if (i<0||i>curLen-1)
                throw new Exception("第"+i+"个元素不存在");
            return listElem[i];
        }
    
    
    
        //
        @Override
        public void insert(int i, Object x) throws Exception {
            if (curLen==listElem.length)
                throw new Exception("顺序表已满");
            if (i<0||i>curLen)
                throw new Exception("插入位置不存在");
            for (int j=curLen;j>i;j--){
                listElem[j] = listElem[j-1];
            }
            listElem[i] = x;
            curLen++;
    
        }
    
        //
        @Override
        public void remove(int i) throws Exception {
            if (curLen==0)
                throw new Exception("顺序表为空");
            if(i<0||i>curLen+1)
                throw new Exception("删除位置存在");
            for (int j=i;j<curLen-1;j++){
                listElem[j] = listElem[j+1];
            }
            curLen--;
        }
    
        // 元素查
        @Override
        public int indexof(Object x) {
            int temp = 0;
            for (int i=0;i<curLen;i++){
                if (listElem[i] == x){
                    temp = i;
                    return temp;
                }
            }
            return -1;
        }
    
        @Override
        public void display() {
            for (int i =0;i<curLen;i++){
                System.out.println(listElem[i]);
            }
        }
    
    }

    测试代码Test

    package SequenceTable;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            // 创建对象
            SqList sqList = new SqList(10);
            //
            sqList.insert(0,0);
            sqList.insert(1,1);
            sqList.insert(2,2);
            sqList.insert(3,3);
            sqList.insert(4,4);
            sqList.insert(5,5);
            sqList.insert(6,6);
            sqList.insert(7,7);
            sqList.insert(8,8);
            sqList.insert(9,9);
    
            // 删除
            sqList.remove(1);
    
            // 查序号
            System.out.println(sqList.indexof(9));
    
            // 查元素
            System.out.println(sqList.get(0));
    
            // 查所有
            sqList.display();
    
    
        }
    }

    python实现

    实现类SqList

    class SqList():
        #
        def add(listElem,maxsize,object):
            if len(listElem) == maxsize:
                print("顺序列表已满")
            else:
                listElem.append(object)
            return listElem
        # 插入
        def insert(listElem,maxsize,object,index):
            if len(listElem) == maxsize:
                print("顺序列表已满")
            elif index<0 and index>=len(listElem)-1:
                print("插入位置有误")
            elif 0<=index<=len(listElem)-1:
                listElem[index+1:len(listElem)+1] = listElem[index:len(listElem)]
                listElem[index] = object
            return listElem
        def removebyIndex(index,listElem):
            if len(listElem) == 0:
                print('顺序列表为空')
            elif index<0 and index>=len(listElem):
                print("删除位置有误")
            elif   0<=index<=len(listElem)-1:
                listElem[index:len(listElem)-1] = listElem[index+1:len(listElem)]
    
            return listElem
        def indexbyObj(listElem,object):
            for i in range(len(listElem)):
                if object == listElem[i]:
                    print(object,"序号为",i)
        def display(listElem):
            for i in range(len(listElem)):
                print(listElem[i])

    测试代码test

    from SqList import SqList
    
    # 初始化数组
    listElem = []
    
    #
    listElem = SqList.add(listElem,10,1)
    listElem = SqList.add(listElem,10,2)
    listElem = SqList.add(listElem,10,3)
    listElem = SqList.add(listElem,10,4)
    listElem = SqList.add(listElem,10,5)
    listElem = SqList.add(listElem,10,6)
    listElem = SqList.add(listElem,10,7)
    listElem = SqList.add(listElem,10,8)
    listElem = SqList.add(listElem,10,9)
    
    
    #
    listElem = SqList.insert(listElem,10,0,0)
    
    #
    listElem = SqList.removebyIndex(index=0,listElem = listElem)
    #
    SqList.indexbyObj(listElem,9)
    SqList.display(listElem)

     

  • 相关阅读:
    Java多线程——<八>多线程其他概念
    Java多线程——<七>多线程的异常捕捉
    逆向破解之160个CrackMe —— 022
    逆向破解之160个CrackMe —— 021
    逆向破解之160个CrackMe —— 020
    逆向破解之160个CrackMe —— 019
    逆向破解之160个CrackMe —— 018
    逆向破解之160个CrackMe —— 017
    逆向破解之160个CrackMe —— 016
    逆向破解之160个CrackMe —— 015
  • 原文地址:https://www.cnblogs.com/wigginess/p/13616176.html
Copyright © 2011-2022 走看看