Operation

1
2
InitList(L):初始化线性表
ListInsert(L,i,e):在线性表的第i个位置插入新元素e

任务

1
2
3
4
5
/*
1.定义线性表类型的对象L。
2.初始化线性表L,并输出其长度。
3.在表头依次插入1~5,输出其长度,并判断L是否为空。
*/

代码实现

  • C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    #include<stdio.h>
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0

    #define MAXSIZE 20

    typedef int ElemType;//抽象数据类型,是线性表中数据元素的数据类型。
    typedef int Status;//抽象数据类型,是函数返回值的类型

    typedef struct
    {
    ElemType data[MAXSIZE];
    int length;
    }SqList;
    //初始化线性表
    Status InitList(SqList L)
    {
    L.length = 0;
    return OK;
    }
    //将线性表中第i个位置插入新元素e
    Status ListInsert(SqList *L, int i, ElemType e)
    {
    int k;
    //判断线性表是否已满
    if(L->length == MAXSIZE)
    return ERROR;
    if(i < 1 || i > L->length+1)//判断i是否有效
    return ERROR;
    if(i <= L->length)
    {
    //将第i个位置及其之后的元素后移一位
    for(k = L->length-1;k >= i-1; k--)
    {
    L->data[k+1] = L->data[k];
    }
    }
    //将新元素插入
    L->data[i-1] = e;
    //更新线性表的长度
    L->length ++;
    return OK;
    }
    int main()
    {
    SqList L;//定义一个顺序结构的线性表。
    Status i;

    i = InitList(L);//初始化线性表L。
    printf("L.length = %d\n",L.length);
    //在表头依次插入1~5
    for(int j=1; j <= 5; j++){
    ListInsert(&L, 1, j);
    }
    printf("L.length = %d\n",L.length);
    }
  • C++

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    #include<iostream>
    using namespace std;
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0

    #define MAXSIZE 20

    typedef int ElemType;//抽象数据类型,是线性表中数据元素的数据类型。
    typedef int Status;//抽象数据类型,是函数返回值的类型

    typedef struct
    {
    ElemType data[MAXSIZE];
    int length;
    }SqList;
    //初始化线性表
    Status InitList(SqList L)
    {
    L.length = 0;
    return OK;
    }
    //将线性表中第i个位置插入新元素e
    Status ListInsert(SqList *L, int i, ElemType e)
    {
    int k;
    //判断线性表是否已满
    if(L->length == MAXSIZE)
    return ERROR;
    if(i < 1 || i > L->length+1)//判断i是否有效
    return ERROR;
    if(i <= L->length)
    {
    //将第i个位置及其之后的元素后移一位
    for(k = L->length-1;k >= i-1; k--)
    {
    L->data[k+1] = L->data[k];
    }
    }
    //将新元素插入
    L->data[i-1] = e;
    //更新线性表的长度
    L->length ++;
    return OK;
    }
    int main()
    {
    SqList L;//定义一个顺序结构的线性表。
    Status i;

    i = InitList(L);//初始化线性表L。
    cout<< "L.length = " << L.length << endl;
    //在表头依次插入1~5
    for(int j=1; j <= 5; j++){
    ListInsert(&L, 1, j);
    }
    cout<< "L.length = " << L.length << endl;
    }
  • java

    1