顺序表专题

test.c

#define _CRT_SECURE_NO_WARNINGS
#include
//C语言基础知识:结构体、指针、动态内存管理
// 
// 什么是数据结构
// 数据结构是计算机存储、组织数据的方式
// 数组就是最基础的数据结构
// 
// 顺序表
// 底层就是数组,要修改某一个数据
// arr[100]= {1,2,3,4,5}
// 修改某一个数据  arr[pos] = x
// 插入一个数据  arr[5] = x
// 顺序表在数组的基础上增加了增删查找等
// 顺序表是线性表的一种,线性表是相同特性的数据结构的集合
// 物理结构:数组空间连续,每个空间都有地址,但线性表不一定连续
// 逻辑结构:连续的
// 
// 顺序表的特性:物理结构:顺序表的底层是数组,数组是连续的,顺序表也是连续的;
// 逻辑结构:一定是连续的
// 
// 顺序表分类
// 静态顺序表
// struct SepList
// {
//     int arr[100];//定长数组
//       int size;//顺序表当前有效数据个数
// };
// 动态顺序表
// struct SepList
// {
//        int*arr;
//        int size;//有效数据个数
//        int capacity;//空间大小
// };
// 头文件:顺序表结构,声明顺序表的方法
// .c文件:实现顺序表的方法
// 
// 
//顺序表初始化

#include "SepList.h"

void SLTest01()
{
    SL sl;
    SLInit(&sl);//对形参的修改不会影响实参
    SLPushBack(&sl, 1);
    SLPushBack(&sl, 2);
    SLPushBack(&sl, 3);
    SLPushBack(&sl, 4);

    SLPopBack(&sl);
    SLPrint(sl);

    /*SLPushFront(&sl, 5);
    SLPushFront(&sl, 6);*/
    SLPopFront(&sl);
    SLPrint(sl);

    SLDestroy(&sl);
}

int main()
{
    SLTest01();
    
    return 0;
}
SepList.h

#pragma once
#include
#include
#include
#include
#define N 100

//静态数据表
//struct SepList
//{
//    int arr[N];
//    int size;//有效数据个数
//};

typedef int SLDataType;
//动态数据表
typedef struct SepList
{
    SLDataType* arr;
    int size;
    int capacity;
}SL;
//顺序表初始化
void SLInit(SL* ps);

//顺序表的销毁
void SLDestroy(SL* ps);

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

//顺序表的打印
void SLPrint(SL s);

//顺序表的从头或尾删除
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);
 

SepList,c

#define _CRT_SECURE_NO_WARNINGS
#include "SepList.h"

void SLInit(SL* ps)
{
    ps->arr = NULL;
    ps->size = ps->capacity = 0;
}

void SLDestroy(SL* ps)
{
    if (ps->arr)
    {
        free(ps->arr);
    }
    ps->arr = NULL;
    ps->size = ps->capacity = 0;
}

void SLCheckCapacity(SL* ps)
{
    assert(ps);
    int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
    SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
    if (tmp == NULL)
    {
        perror("SLPushBack()::realloc");
        exit(1);//直接退出程序
    }
    ps->arr = tmp;
    ps->capacity = newCapacity;
}

void SLPushBack(SL* ps, SLDataType x)
{
    assert(ps);
    //插入数据之前,先看空间够不够
    SLCheckCapacity(ps);
    ps->arr[ps->size++] = x;
}

void SLPushFront(SL* ps, SLDataType x)
{
    assert(ps);
    SLCheckCapacity(ps);
    /*先让已有元素整体移动*/
    for (int i = ps->size; i > 0; i--)
    {
        ps->arr[i] = ps->arr[i - 1];//最后一次arr[1] = arr[0]
    }

    /*memmove((ps->arr) + 1, ps->arr, ps->size);*/
    ps->arr[0] = x;
    ps->size++;
}


void SLPrint(SL s)
{
    for (int i = 0; i < s.size; i++)
    {
        printf("%d ", s.arr[i]);
    }
    printf("\n");
}


void SLPopBack(SL* ps)
{
    assert(ps);
    //判断顺序表是否为空
    assert(ps->size);
    /*ps->arr[ps->size - 1] = -1;有些画蛇添足*/
    --ps->size;//不影响增删查改
}
void SLPopFront(SL* ps)
{
    assert(ps);
    assert(ps->size);
    for (int i = 0; i < ps->size-1; i++)
    {
        ps->arr[i] = ps->arr[i + 1];
    }
    ps->size--;
}

你可能感兴趣的:(顺序表专题)