【数据结构】C语言线性表-顺序表

什么是线性表

线性表是最常用且最简单的一种数据结构。简言之,一个线性表是n个数据元素的有限序列。

线性结构特点

在数据元素的非空有限集合中

  1. 存在唯一的一个被称做“第一个”的数据元素
  2. 存在唯一的一个被称做“最后一个”的数据元素
  3. 除第一个之外,集合中的每个数据元素均只有一个前驱
  4. 除最后一个之外,集合中每个数据元素均只有一个后继

顺序表的实现

顺序表示的是用一组地址连续的存储单元依次存储线性表的数据元素
以下是C++的实现过程
注:C语言不可使用引用

#include <iostream>
#include <stdlib.h>
#define SIZE 5//顺序表大小
#define ElemType int//元素数据类型
using namespace std;

typedef struct List{
    ElemType *elem;//存储空间基地址(动态数组)
    int length;//数组长度
    int size;//数组大小
}list;

//初始化顺序表L
int InitList(list &L);
//在顺序L中第i个位置插入NewElem元素
int InsertList(list &L,int i,ElemType NewElem);
//删除顺序表L中第i个位置上的元素,并将其值保存到常量DelElem
int DeleteList(List &L,int i,ElemType &DelElem);
//修改顺序表L中第i个位置元素为NewElem元素
int AmendList(list &L,int i,ElemType NewElem);
//查询顺序表L中元素e的位置(返回查询位置)
int SelectList(list L,ElemType e);
//遍历顺序表
void Display(list L);

int main(){
    list L;
    ElemType DelElem;

    InitList(L);
    cout<<"定义初始值:"<<endl;
    InsertList(L,1,1);
    InsertList(L,2,2);
    InsertList(L,3,3);
    InsertList(L,4,4);
    InsertList(L,5,5);
    Display(L);

    cout<<"在第2个位置插入元素35:"<<endl;
    InsertList(L,2,35);
    Display(L);
    
    cout<<"删除第3个元素的值为:";
    DeleteList(L,3,DelElem);
    cout<<DelElem<<endl;
    Display(L);
    
    cout<<"更改第五个位置的元素为33:"<<endl;
    AmendList(L,5,33);
    Display(L);

    cout<<"查询元素3的位置:";
    int num = SelectList(L,3);
    cout<<num<<endl;
}

int InitList(list &L){
    L.elem = (ElemType*)malloc(SIZE * sizeof(ElemType));
    if(!L.elem){
        printf("初始化失败\n");
        exit(-1);//初始化失败直接退出
    }
    L.length = 0;
    L.size = SIZE;
    return 1;
}

//在顺序表L中第i个位置插入新元素NewElem
int InsertList(list &L,int i,ElemType NewElem){
    //判断插入位置,不能小于1,不能大于表长度
    if(i<1 || i>L.length+1){
        cout<<"插入位置不合法"<<endl;
        return 0;
    }
    //判断顺序表存储空间是否满足
    if(L.length >= L.size){
        //使用新变量分配空间用来if判断是否分配成功,很多人使用的L.elem = (ElemType*)realloc...,即便判断也是为真
        ElemType *newbase  = (ElemType*)realloc(L.elem, (L.size+1)*sizeof(ElemType));
        if(!newbase){
            printf("内存分配失败\n");
            exit(-1);
        }
        L.elem = newbase;//新基址
        L.size++;
    }
    //为插入变量,元素后移
    for(int p=L.length-1; p>=i-1; p--){
        L.elem[p+1] = L.elem[p];
    }
    L.elem[i-1] = NewElem;
    L.length++;
    return 1;
}

//在顺序表L中删除第i个元素,并返回其值e
int DeleteList(List &L,int i,ElemType &e){
    if(i<1 || i>L.length){
        cout<<"删除位置不合法!"<<endl;
        return 0;
    }
    e = L.elem[i-1];
    for(int p=i;p<L.length;p++){
        L.elem[p-1] = L.elem[p];
    }
    L.length--;
    return 1;
}

//插入位置如果小于1或者大于顺序表的长度则返回-1
int AmendList(list &L,int i,ElemType NewElem){
    if(i<1 || i>L.length){
        cout<<"更改的位置有误!"<<endl;
        return 0;
    }
    L.elem[i-1] = NewElem;
    return 1;
}

//在顺序表L中查找e元素的位置,若不存在则返回-1
int SelectList(list L,ElemType e){
    for(int i=0;i<L.length-1;i++){
        if(L.elem[i] == e){
            return i+1;
        }
    }
    return -1;
}

void Display(list L){
    for(int i=0;i<L.length;i++){
        cout<<L.elem[i]<<" ";
    }
    cout<<endl;
}

运行结果:

定义初始值:
1 2 3 4 5 
在第2个位置插入元素35:
1 35 2 3 4 5 
删除第3个元素的值为:2
1 35 3 4 5 
更改第五个位置的元素为33:
1 35 3 4 33 
查询元素3的位置:3
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值