【数据结构初阶】二、顺序表

一、线性表

线性表 linear list n 个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

二、顺序表

顺序表是用一段 物理地址连续 的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。顺序表本质就是数组,要求数据是连续存储的。
代码如下:
SeqList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>

typedef int SLDataType;

typedef struct SeqList
{
    SLDataType* a;
    int size;
    int capacity;
}SL;

void SeqListPrint(SL* ps);
void SeqListInit(SL* ps);
void SeqListDestory(SL* ps);
void SeqListCheckCapacity(SL* ps);
void SeqListPushBack(SL* ps, SLDataType x);
void SeqListPopBack(SL* ps);
void SeqListPushFront(SL* ps, SLDataType x);
void SeqListPopFront(SL* ps);

int SeqListFind(SL* ps,SLDataType x);
void SeqListInsert(SL* ps, int pos, SLDataType x);
void SeqListErase(SL* ps, int pos);

SeqList.c

#include "SeqList.h"

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

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

void SeqListDestory(SL* ps)
{
    free(ps->a);
    ps->a = NULL;
    ps->capacity = ps->size = 0;
}

void SeqListCheckCapacity(SL* 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)
        {
            printf("realloc fail!\n");
            exit(-1);
        }

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

void SeqListPushBack(SL* ps, SLDataType x)
{
    SeqListCheckCapacity(ps);

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

void SeqListPopBack(SL* ps)
{
    assert(ps->size > 0);
    ps->size--;
}

void SeqListPushFront(SL* ps, SLDataType x)
{
    SeqListCheckCapacity(ps);

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

void SeqListPopFront(SL* ps)
{
    assert(ps->size > 0);
    int begin = 1;
    while (begin < ps->size)
    {
        ps->a[begin - 1] = ps->a[begin];
        ++begin;
    }
    ps->size--;
}

int SeqListFind(SL* ps, SLDataType x)
{
    for (int i = 0; i < ps->size; i++)
    {
        if (ps->a[i] == x)
        {
            return i;
        }
    }

    return -1;
}

void SeqListInsert(SL* ps, int pos, SLDataType x)
{
    assert(pos >= 0 && pos <= ps->size);
    SeqListCheckCapacity(ps);
    int end = ps->size - 1;
    while (end >= pos)
    {
        ps->a[end + 1] = ps->a[end];
        --end;
    }
    ps->a[pos] = x;
    ps->size++;

}

void SeqListErase(SL* ps, int pos)
{
    assert(pos >= 0 && pos < ps->size);

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

    ps->size--;
}

练习题:

习题1、移除元素

方法一、从头开始遍历,如果是val,所有的数字向前挪一下;时间复杂度,看最坏的情况,数组中全部的数都是val,时间复杂度是O(N^2);

方法二、开辟一个临时数组 tmp ,一次遍历nums数组把不是val的值放到tmp数组,再把tmp数组的值拷贝回去。时间复杂度O(N);

方法三、定义两个变量,src和dst(双指针)src去找num是数组中不等于val的值,放到dst指向的位置,再++src,++dst。时间复杂度O(N),空间复杂度O(1)。

int removeElement(int* nums, int numsSize, int val)
{
    int src = 0;
    int dst = 0;
    while (src < numsSize)
    {
        if (nums[src] != val)
        {
            nums[dst] = nums[src];
            src++;
            dst++;
        }
        else
        {
            src++;
        }
    }
    return dst;
}

习题二、删除有序数组中的重复项

 方法一、挪动数据法,碰见一个重复的向前挪动一次,时间复杂度是O(N^2);

 

方法二、双指针的变形,使用 i 和 j 来表示区间,使用 dst 每当 i 和 j 不同的时候,将 i 的值赋给 dst 。代码如下。

int removeDuplicates(int* nums, int numsSize)
{
    if (numsSize == 0)//有可能数组是空的
    {
        return 0;
    }

    int i = 0, j = 1;
    int dst = 0;
    while (j < numsSize)
    {
        if (nums[i] == nums[j])
        {
            ++j;
        }
        else
        {
            nums[dst] = nums[i];
            dst++;
            i = j;
            ++j;
        }
    }

    nums[dst] = nums[i];
    ++dst;

    return dst;
}

习题三、合并两个有序数组

方法:两个指针分别指向两个数组最后的有效数据,分别比较后依次向前放。

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n)
{
    int end1 = m - 1;
    int end2 = n - 1;
    int end = m + n - 1;
    while (end1 >= 0 && end >= 0)
    {
        if (nums1[end1] > nums2[end2])
        {
            nums1[end--] = nums1[end2--];
        }
        else
        {
            nums1[end--] = nums2[end2--];
        }
    }
    while (end2 >= 0)
    {
        nums1[end--] = nums2[end2--];
    }
}

总结:顺序表的缺陷,

1.空间不够了需要扩容,增容需要付出代价;

2. 避免频繁扩容,基本都是满了,扩2倍,可能会导致一定的空间浪费;

3.顺序表要求数据从开始位置连续存储,那么我们在头部或者中间位置插入删除数据就需要挪动数据,效率不高。

针对数据表的缺陷,设计出了链表。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值