初阶数据结构学习记录——둘 顺序表

目录

1、线性表

2、顺序表

SeqList.h

SeqList.c


1、线性表

线性表是n个具有相同特征的数据元素的有限序列。在逻辑上呈现线性结构,但是物理上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

2、顺序表

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般采用数组存储。在数组上完成数据的增删查改。

具体以代码形式呈现

SeqList.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int SLDataType;
typedef struct SeqList
{
	SLDataType* a;
	int size;       // 记录存储多少个有效数据
	int capacity;   // 空间容量大小 
}SL;

void SLPrint(SL* ps);
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLCheckCapacity(SL* ps);

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

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

// 中间插入删除
// 在pos位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x);

// 删除pos位置数据
void SLErase(SL* ps, int pos);

// begin查找x的起始位置
int SLFind(SL* ps, SLDataType x, int begin);

SeqList.c

#include"SeqList.h"


void SLPrint(SL* ps)
{
    assert(ps);

    for (int i = 0; i < ps->size; ++i)
    {
        printf("%d ", ps->a[i]);
    }
    printf("\n");
}

void SLCheckCapacity(SL* ps)
{
    assert(ps);

    if (ps->size == ps->capacity)
    {
        int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));
        if (tmp == NULL)
        {
            perror("realloc fail");
            exit(-1);
        }

        ps->a = tmp;
        ps->capacity = newCapacity;
    }
}

void SLInit(SL* ps)
{
    assert(ps);

    ps->a = NULL;
    ps->size = 0;
    ps->capacity = 0;
}

void SLDestroy(SL* ps)
{
    assert(ps);

    //if (ps->a != NULL)
    if (ps->a)
    {
        free(ps->a);
        ps->a = NULL;
        ps->size = ps->capacity = 0;
    }
}

// O(1)
void SLPushBack(SL* ps, SLDataType x)
{
    /*assert(ps);

    SLCheckCapacity(ps);

    ps->a[ps->size] = x;
    ps->size++;*/

    SLInsert(ps, ps->size, x);
}

void SLPopBack(SL* ps)
{
    //assert(ps);

     温柔的检查
    ///*if (ps->size == 0)
    //{
    //return;
    //}*/

     暴力的检查
    //assert(ps->size > 0);

    ps->a[ps->size - 1] = 0;
    //ps->size--;

    SLErase(ps, ps->size - 1);
}

// O(N)
void SLPushFront(SL* ps, SLDataType x)
{
    //assert(ps);
    //SLCheckCapacity(ps);

     挪动数据
    //int end = ps->size - 1;
    //while (end >= 0)
    //{
    //	ps->a[end + 1] = ps->a[end];
    //	end--;
    //}

    //ps->a[0] = x;
    //ps->size++;

    SLInsert(ps, 0, x);
}

void SLPopFront(SL* ps)
{
    /*assert(ps);
    assert(ps->size > 0);

    int begin = 1;
    while (begin < ps->size)
    {
        ps->a[begin - 1] = ps->a[begin];
        begin++;
    }

    ps->size--;*/

    SLErase(ps, 0);
}

// 在pos位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
    assert(ps);
    assert(pos >= 0);
    assert(pos <= ps->size);

    SLCheckCapacity(ps);
    int end = ps->size - 1;
    while (end >= pos)
    {
        ps->a[end + 1] = ps->a[end];
        end--;
    }

    ps->a[pos] = x;
    ps->size++;
}

// 删除pos位置数据
void SLErase(SL* ps, int pos)
{
    assert(ps);
    assert(pos >= 0);
    assert(pos < ps->size);
    //assert(ps->size > 0);

    // 挪动数据覆盖
    int begin = pos + 1;
    while (begin < ps->size)
    {
        ps->a[begin - 1] = ps->a[begin];
        begin++;
    }

    ps->size--;
}

int SLFind(SL* ps, SLDataType x, int begin)
{
    assert(ps);

    for (int i = begin; i < ps->size; ++i)
    {
        if (ps->a[i] == x)
        {
            return i;
        }
    }

    return -1;
}

之前的通讯录本身与顺序表相似,不过顺序表重点在于头插头删,尾插尾删,指定位置增删功能。尾部操作都是直接改变size来实现,而头部两个操作异曲同工,需要一个个挪动元素。指定位置增删也相似,增加右移,留出pos位置,然后添加。删除则是左移覆盖过去。当实现pos位置增删后,头尾插删其实也就可以完全用这两个函数来代替。

结束。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值