数据结构中顺序表的实现

目录

前言

实现过程

SeqList.h:

SeqList.c:

test.c:


前言

顺序表是数据结构中一种常见的线性表,表示用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储,在数组上完成数据的增删改查。顺序表又分为静态顺序表和动态顺序表,静态顺序表只适用于确定知道需要存多少数据的场景,但是使用的话,有很多情况下是没法确定需要的空间大小的,所以基本使用的都是动态顺序表,今天所要实现的也是动态顺序表。

实现过程

动态顺序表的实现需要通过接口来实现,需要创建三个源文件,分别是

SeqList.h  :用于存储头文件,函数的声明,结构体。

SeqList.c  :用于各个功能函数的实现。

test.c  :主函数,用来调用函数对数据进行测试计算,里面有几组测试用例对函数的功能进行测试。

SeqList.h:

#pragma once
#define N 200
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
typedef int SLDataType;


typedef struct SeqList
{    
    SLDataType* a;//指向动态开辟的数组
    size_t size;//有效数据个数
    size_t capicity;//容量空间的大小
}SL;

void SLInit(SL*ps);//初始化

void CheckCapicity(SL* ps);//扩容

void SLPushBack(SL* ps,SLDataType x);//尾插

void SLPrint(SL* ps);//打印

void SLPushFront(SL* ps, SLDataType x);//头插

void SLPopFront(SL* ps);//头删

void SLPopBack(SL* ps);//尾删

int SLFind(const SL* ps,SLDataType x);//查找

void SLInsert(SL* ps, size_t pos, SLDataType x);//插入

void SLErase(SL* ps, size_t pos);//删除

void SLModify(SL* ps, size_t pos,SLDataType x);//修改

SeqList.c:

#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"

void SLInit(SL* ps)//初始化
{
    ps->a = NULL;
    ps->size = ps->capicity = 0;
}
void CheckCapicity(SL* ps)//扩容
{
    if (ps->size ==ps->capicity)
    {
        size_t newCapicity = ps->capicity == 0 ? 4 : ps->capicity * 2;
        SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapicity*sizeof(SLDataType));
        if (tmp == NULL)
        {
            perror("realloc");
            return;
        }
        ps->a = tmp;
        ps->capicity = newCapicity;
    }
}

void SLPushBack(SL* ps, SLDataType x)//尾插
{
    assert(ps);
    CheckCapicity(ps);
    ps->a[ps->size] = x;
    ps->size++;
}

void SLPrint(SL* ps)//打印
{
    assert(ps);
    int i = 0;
    for (i = 0; i < ps->size; i++)
    {
        printf("%d ", ps->a[i]);
    }
    printf("\n");
}

void SLPushFront(SL* ps, SLDataType x)//头插
{
    assert(ps);
    CheckCapicity(ps);
    memmove(ps->a+1, ps->a, ps->size * sizeof(SLDataType));
    /*int end = ps->size - 1;
    while (end >= 0)
    {
        ps->a[end + 1] = ps->a[end];
        end--;
    }*/
    ps->a[0] = x;
    ps->size++;
}

void SLPopFront(SL* ps)//头删
{
    if(ps->size>1)
    {
        memmove(ps->a, ps->a + 1, (ps->size - 1) * sizeof(SLDataType));
        ps->size--;
    }
    else
    {
        assert(ps->size > 0);
        ps->size--;
    }
}

void SLPopBack(SL* ps)//尾删
{
    
    assert(ps->size > 0);
    ps->size--;

}

void SLInsert(SL* ps,size_t pos, SLDataType x)//插入
{
    CheckCapicity(ps);
    assert(ps);
    assert(pos >= 0 && pos <= ps->size);
    memmove(ps->a+pos+1, ps->a+pos, (ps->size - pos) * sizeof(SLDataType));
    ps->a[pos] = x;
    ps->size++;
}

void SLErase(SL* ps, size_t pos)//删除指定位置的值
{
    assert(ps);
    assert(pos>=0&&pos<ps->size);
    size_t begin = pos;
    while (begin < ps->size-1)
    {
        ps->a[begin] = ps->a[begin + 1];
        begin++;
    }
    ps->size--;

}
int SLFind(const SL* ps, SLDataType x)//查找
{
    assert(ps);
    int i = 0;
    for (i = 0; i < ps->size; i++)
    {
        if (ps->a[i] == x)
        {
            return i;
        }
    }
    return -1;
}

void SLModify(SL* ps, size_t pos,SLDataType x)//修改
{
    assert(ps);
    assert(pos >= 0 && pos < ps->size);

    ps->a[pos] = x;
}

test.c:

#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"


void TestSeqList1()
{
    SL s1;
    SLInit(&s1);
    SLPushBack(&s1,1);
    SLPushBack(&s1, 2);
    SLPushBack(&s1, 3);
    SLPushBack(&s1, 4);
    SLPushBack(&s1, 5);
    SLPrint(&s1);
    SLPushFront(&s1, 56);
    SLPushFront(&s1, 57);
    SLPushFront(&s1, 58);
    SLPushFront(&s1, 59);
    SLPrint(&s1);
    SLPushBack(&s1, 1);
    SLPushBack(&s1, 2);
    SLPushBack(&s1, 3);
    SLPushBack(&s1, 4);
    SLPushBack(&s1, 5);
    SLPrint(&s1);

    SLPopFront(&s1);
    SLPopFront(&s1);
    SLPopFront(&s1);
    SLPopFront(&s1);
    SLPrint(&s1);
    SLPopBack(&s1);
    SLPrint(&s1);
    SLPopBack(&s1);
    SLPrint(&s1);
    SLPopBack(&s1);
    SLPrint(&s1);
    SLPopBack(&s1);
    SLPrint(&s1);
    SLPopBack(&s1);
    SLPrint(&s1);
    SLPopBack(&s1);
    SLPrint(&s1);
    SLPopBack(&s1);
    SLPrint(&s1);
    SLPopBack(&s1);
    SLPrint(&s1);
    SLPopBack(&s1);
    SLPrint(&s1);
}

void TestSeqList2()
{
    SL sl;
    SLInit(&sl);
    SLPushBack(&sl, 1);
    SLPushBack(&sl, 2);
    SLPushBack(&sl, 3);
    SLPushBack(&sl, 4);
    SLPushBack(&sl, 5);
    SLPrint(&sl);
    SLPopBack(&sl);
    SLPopBack(&sl);
    SLPopBack(&sl);

    SLPrint(&sl);

    SLPushBack(&sl, 3);
    SLPushBack(&sl, 4);
    SLPushBack(&sl, 5);
    SLPopFront(&sl);
    


    SLPrint(&sl);
    SLInsert(&sl, 2, 9);
    SLPrint(&sl);
    SLInsert(&sl, 0, 9);
    SLPrint(&sl);
    SLInsert(&sl, 1, 9);
    SLPrint(&sl);
    SLInsert(&sl, 5, 9);
    SLInsert(&sl, 7, 9);
    SLPrint(&sl);
    SLFind(&sl, 9);

}

void TestSeqList3()
{
    SL sl;
    SLInit(&sl);
    SLPushBack(&sl, 5);
    SLPushBack(&sl, 4);
    SLPushBack(&sl, 5);
    SLPushBack(&sl, 5);
    SLPushBack(&sl, 3);
    SLPushBack(&sl, 5);
    SLPushBack(&sl, 2);
    SLPushBack(&sl, 5);
    SLPushBack(&sl, 1);
    SLPrint(&sl);
    /*int x,y;
    printf("请输入要修改的值和修改后的值:");
    scanf("%d %d", &x,&y);
    int pos = SLFind(&sl, x);
    while(pos!=-1)
    {    
        SLInsert(&sl, pos, y);
    }*/

    int x;
    printf("请输入要删除的数:");
    scanf("%d", &x);
    int pos = SLFind(&sl, x);
    while (pos != -1)
    {
        SLErase(&sl, pos);
        pos = SLFind(&sl, x);
    }
    /*if (pos != -1)
    {
        SLErase(&sl, pos);
    }
    else
    {
        printf("没找到\n");
    }*/
    SLPrint(&sl);
    SLInsert(&sl, 3, 5);
    SLPrint(&sl);
    SLErase(&sl, 2);
    SLPrint(&sl);
    int y;
    printf("请输入要修改的数和修改后的数:");
    scanf("%d %d", &x,&y);
    pos = SLFind(&sl, x);
    if (pos != -1)
    {
        SLModify(&sl, pos,y);
    }
    else
    {
        printf("没找到\n");
    }    
    SLPrint(&sl);
}
int main()
{
    TestSeqList3();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值