zoukankan      html  css  js  c++  java
  • [数据结构]之顺序表

    [数据结构]之顺序表

    描述
    顺序表:是指在一段连续的存储单元存储数据的线性表。多使用数组来实现。

    数据结构

    1 属性:

    最大长度 CAP

    当前长度 Length

    存储数组 Elements


    2 操作:

    Get(index) 获取元素

    Insert(index,elem) 插入元素

    Delete(index) 删除第index个元素

    ## 实现
    使用go语言实现代码如下:

        package main
        
        import (
            "fmt"
        )
        
        const CAP = 20
        
        type SqList struct {
            elemets [CAP]string
            length  int
        }
        
        /*
         *    获取顺序表的第index元素
         */
        func (list *SqList) Get(index int) (string, error) {
            if list.length == 0 || index < 0 || index > list.length-1 {
                return "", fmt.Errorf("the index %d Out Of Bounds", index)
        
            }
            return list.elemets[index], nil
        }
        
        /*
         *    插入顺序表元素,在第index位置
         */
        func (list *SqList) Insert(index int, elem string) error {
            if list.length == CAP {
                return fmt.Errorf("the list is full")
            }
            if index < 0 || index > list.length {
                return fmt.Errorf("the index %d Out Of Bounds", index)
            }
            //如果不是在最后插入,需要移动后面的元素
            if index < list.length {
                for i := list.length - 1; i >= index; i-- {
                    list.elemets[i+1] = list.elemets[i]
        
                }
            }
            list.elemets[index] = elem
            list.length++
            return nil
        }
        
        /*
         *    删除顺序表元素,在第index位置
         */
        func (list *SqList) Delete(index int) error {
            if list.length == 0 {
                return fmt.Errorf("the list is empty")
            }
            if index < 0 || index > list.length {
                return fmt.Errorf("the index %d Out Of Bounds", index)
            }
            //如果不是在最后删除,需要移动后面的元素
            if index < list.length {
                for i := index; i < list.length; i++ {
                    list.elemets[i] = list.elemets[i+1]
        
                }
            }
        
            list.length--
            return nil
        }
        
        func main() {
        
            list := &SqList{}
        
            list.Insert(0, "AAAAA")
            list.Insert(1, "BBBBB")
            list.Insert(2, "CCCCC")
        
            list.Delete(1)
        
            for i := 0; i < list.length; i++ {
                elem, _ := list.Get(i)
                fmt.Printf("get elem %d value:%v
    ", i, elem)
            }
        }
  • 相关阅读:
    IIS笔记-Application Request Route(ARR)
    UE4笔记-UStructToJsonObjectString首字母自动转换为小写的问题及解决方法
    Electron/Nodejs开发笔记-功能问题记录及指南
    Net笔记-EF/EF Core/Dapper等ORM开发记录
    C/C++和C#混合编程笔记-DLL调用与IPC等mixed问题记录
    CImg笔记
    Net/Net Core笔记 WebAPI/MVC一些bug和处理
    Net-Net Core 3.0 gRPC 开发不完全笔记
    UE4-PixelStreaming不完全开发笔记
    UE4-开发中遇到的问题和处理方法
  • 原文地址:https://www.cnblogs.com/sxt102400/p/3234231.html
Copyright © 2011-2022 走看看