当前位置:   article > 正文

顺序表的基本操作_(1)建立一个顺序表,将元素18 27 29 3 95 46 31 66存入顺序表中,并输出该顺序表

(1)建立一个顺序表,将元素18 27 29 3 95 46 31 66存入顺序表中,并输出该顺序表。
//库函数头文件包含
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>


//函数状态码定义
#define OK          1
#define ERROR       0
#define OVERFLOW   -2

#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10

typedef int  Status;//状态量
//顺序表的存储结构定义
typedef int ElemType;  //假设线性表中的元素均为整型
typedef struct {
    ElemType* elem;   //存储空间基地址
    int length;       //表中元素的个数
    int listsize;     //表容量大小
}SqList;    //顺序表类型定义

//结构初始化与销毁操作
Status InitList(SqList &L) {
    //初始化L为一个空的有序顺序表
    L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)exit(OVERFLOW);
    L.listsize=LIST_INIT_SIZE;
    L.length=0;
    return OK;
}

//元素有序插入 
Status ListInsert(SqList &L, ElemType e)
{
    //扩容 
    if(L.length>=L.listsize)
        L.elem=(ElemType *)realloc(L.elem,(L.length+10)*sizeof(ElemType));
    if(!L.elem)return OVERFLOW;

    //每插入一个元素,顺序表的长度加1
    L.length++;
    for(int i=0;i<L.length;i++)
        if(*(L.elem+i)>e){
            for(int j=L.length;j>i;j--)
                *(L.elem+j)=*(L.elem+j-1);
            *(L.elem+i)=e;
            return OK;
        }
    *(L.elem+L.length-1)=e;
    return OK;
}

//删除某一位置的元素 
Status ListDelete(SqList &L,int pos,ElemType &e)
{
    //若删除元素的位置超出顺序表长度
    if(pos>L.length)
        return OVERFLOW;

    e=L.elem[pos-1];
    for(int i=pos-1;i<L.length-1;i++)
        L.elem[i]=L.elem[i+1];
    L.length--;
    return OK;
}

//定位e的位置 
int ListLocate(SqList L,ElemType e)
{
    for (int i=0;i<L.length;i++)
        if (L.elem[i]==e)
            return i+1;
    //未找到元素e
    return -1;
}

//顺序表输出 
Status ListPrint(SqList L)
{
    for (int i= 0;i<L.length-1;i++)
        printf("%d ",L.elem[i]);
    printf("%d\n",L.elem[L.length-1]);
}

//顺序表就地逆置 
Status ListReverse(SqList &L)
{
    int temp;
    for(int i=0;i<L.length/2;i++){
        temp=*(L.elem+i);
        *(L.elem+i)=*(L.elem+L.length-i-1);
        *(L.elem+L.length-i-1)=temp;
    }
    return OK;
} 


//测试主函数
int main() {
    SqList L;

    if (InitList(L) != OK) {
        printf("InitList_Sq: 初始化失败!!!\n");
        return -1;
    }

    for (int i = 1; i <= 10; ++i)
        ListInsert(L, i);

    int operationNumber;  //操作次数
    scanf("%d", &operationNumber);


    while (operationNumber != 0) {
        int operationType;  //操作种类
        scanf("%d", &operationType);

        if (operationType == 1) {  //增加操作
            int pos, elem;
            scanf("%d",&elem);
            ListInsert(L,elem);
        }
        else if (operationType == 2) {  //删除操作
            int pos; 
            ElemType elem;
            scanf("%d", &pos);
            ListDelete(L, pos, elem);
            printf("%d\n", elem);
        }
        else if (operationType == 3) {  //查找定位操作
            ElemType elem;
            scanf("%d", &elem);
            int pos = ListLocate(L, elem);
            if (pos >= 1 && pos <= L.length)
                printf("%d\n", pos);
            else
                printf("NOT FIND!\n");
        }
        else if (operationType == 4) {  //输出操作
            ListPrint(L);
        else if (operationType == 5) {  //逆置操作
            ListReverse(L);
        }
        operationNumber--;
    }
    return 0;
}
  • 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
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/427016
推荐阅读
相关标签
  

闽ICP备14008679号