Data Structure Fundamentals and Linear Tables

Explanation of core terms of data structure

The following name explanations are taken from the Yan Weimin edition of "Algorithms and Data Structures".

Data

It is a symbolic representation of objective things. In computer science, it refers to the general term for all symbols that can be input into a computer and processed by a computer program.

Data Element

A data element is the basic unit of data and is usually considered and processed in a program as a whole. A data element can be composed of several data items (Data Item).

Data Item

A data item is the smallest unit of indivisible data. A data item is a data description of a certain aspect of an objective thing.

Data Object

is a collection of data elements of the same nature and is a subset of data.

Data Structure

Refers to a collection of data elements that have (exist) a certain relationship (relationship) with each other. The interconnections (relationships) between elements are called logical structures. There are four basic types of logical structures between data elements.

logical structure vs. physical structure

logical structure

The relationship between data elements can be a natural relationship between the elements representing a certain meaning, or it can be a relationship defined artificially for the convenience of handling problems. This natural or artificially defined "relationship" is called the logic between data elements. relationship, the corresponding structure is called 逻辑结构. There are four basic types of logical structures:

  1. 集合, the data elements in the structure have no other relationship except "belonging to the same set".
  2. 线性结构, there is a one-to-one relationship between the data elements in the structure.
  3. 树形结构, there is a one-to-many relationship between the data elements in the structure.
  4. 图状结构或网状关系, there is a many-to-many relationship between the data elements in the structure.

physical structure

物理结构It refers to the storage form of data logical structure in the computer, generally there are three kinds.

  1. Sequential storage structures, such as linear tables.
  2. Chained storage structures such as trees.
  3. The composite storage structure is shown in the figure.

algorithm

算法A description of a method (step) for solving a particular problem, a finite sequence of instructions, where each instruction represents one or more operations.

Characteristics of the algorithm

  • Finiteness : An algorithm must always end after executing finite steps, each of which is completed in finite time.
  • Deterministic : Every instruction in an algorithm must have an exact meaning. There is no ambiguity. And the algorithm has only one entry and exit.
  • 可行性:一个算法是能行的。即算法描述的操作都可以通过已经实现的基本运算执行有限次来实现。
  • 输入:一个算法有零个或者多个输入,这些输入取自于某个特定的对象集合。
  • 输出:一个算法有一个或多个输出,这些输出是同输出有着某些特定关系的量。

算法设计要求

评价一个好的算法有以下几个标准:

  • 正确性:算法应该满足具体问题的需求。
  • 可读性:算法应该容易供人阅读和交流。可读性好的算法有助于对算法的理解和修改。
  • 健壮性:算法应该有容错处理。当输入非法或者错误数据时,算法能适当的做出反应或者处理,而不是产生莫名其妙的输出结果。
  • 通用性:算法应具有一般性,即算法的处理结果对于一般的数据集合都成立。
  • 效率与存储量需求:效率指的是算法执行的时间;存储量需求指算法执行过程中所需要的最大存储空间。一般与问题的规模有关。

算法效率衡量方法

算法执行时间虚通过依据该算法编制的程序在计算机上运行所消耗的时间来度量,也就是时间复杂度

时间复杂度

算法中基本操作重复执行的次数是问题规模n的某个函数,其时间度量记作T(n)=O(f(n)),称作算法的渐近时间复杂度,简称时间复杂度。一般地,常用最深层循环内的语句中的原操作的执行频度(重复执行的次数)来表示。 常用的时间复杂度的大小关系为:

image.png

image.png

空间复杂度

空间复杂度:是指算法编写成程序后,在计算机中运行时所需存储空间的大小的度量。记作:S(n)=O(f(n))。其中n为问题的规模。例如:

image.png

线性表

线性表(Linear List):是由n(n>=0)个元素(结点)a1,a2……an组成的有限序列。该序列中的所有结点具有相同的数据类型。其中数据元素的个数n称为线性表的长度。

  • 当n=0时,称为空表。
  • 当n>0时,将非空线性表记作:(a1,a2……an),a1称为线性表的第一个(首)结点,an称为线性表的最后一个(尾)结点。

顺序存储

把线性表的结点按逻辑顺序依次存放在一组地址连续的存储单元里。用这种方法存储的线性表简称顺序表。

顺序存储的特点

  • 线性表的逻辑顺序与物理顺序一致;
  • 数据元素之间的关系是以元素在计算机内“物理位置相邻”来体现。

image.png

顺序表的基本操作

顺序存储结构中,很容易实现线性表的一些操作:初始化、赋值、查找、修改、插入、删除、求长度等。

  • 顺序表的初始化
Status Init_SqList(SqList *L){
    L->elem_array = (ElemType*)malloc(MAX_SIZE*sizeof(ElemType));
    if(!L->elem_array){
        return ERROR;
    }else{
        L->Length = 0;
        return OK;
    }
}
复制代码

直接进行malloc关键字的内存空间开辟。

  • 顺序表的插入
Status Insert_SqList(Sqlist *L, int i, ElemType e){
    int j;
    if(i<0||i>L->Length-1) return ERROR;
    if(L->Length >= MAX_SIZE){
        printf("线性表溢出!");
        return ERROR;
    }
    for(j= L->Length;j>=i-1;j--){
        L->Elem_array[j+1] = L->Elem_array[j];
    }
    L->Elem_array[i-1] = e;
    L->Length++;
    return OK;
}
复制代码

步骤实现拆分:

  1. 将线性表L中的第i个至第n个结点后移一个位置。
  2. 将结点e插入到结点a[i-1]之后。
  3. 线性表长度加1。
  • 线性表的删除
ElemType Delete_SqList(Sqlist *L,int i){
    int l;
    ElemType x;
    if(L->length == 0){
        printf("线性表L为空");
        return ERROR;
    }else if(i<1||i>L->length){
        printf("要删除的数据元素不存在");
        return ERROR;
    }else{
        //要删除的结点的值 保存
        x = L->Elem_array[i-1];
        for(k = i; k<L->Lenght;k++){
            L->Elem_array[k-1] = L->Elem_array[k];
            L->length--;
            return(x);
        }
    }
}
复制代码

步骤解析:

  1. 将线性表L中的第i+1个至第n个结点依次向前移动一个位置。
  2. 线性表长度减1。

链式存储

用一组任意的存储单元存储线性表中的数据元素。用这种方法存储的线性表简称线性链表

链式存储的特点

存储链表中结点的一组任意的存储单元可以是连续的,也可以是不连续的,甚至是分布在内存中的任意位置上的。链表中的逻辑顺序和物理顺序不一定相同。

image.png

本篇主要介绍了一些基本概念,下一篇会对链表进行详细的介绍和一些基本的操作进行算法实现。

Guess you like

Origin juejin.im/post/7081844035060498440