一、线性表
二、顺序表
1 .概念及结构
静态的顺序表有一些不足的是定了存储元素个数,如果不确定有多少个元素,就不知道定多长,有可能给多很多,也可能给的不够,因此不利于实践。

2.接口实现
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps);
void SeqListDestroy(SeqList* ps);
void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);
然后我们就对他们进行实现。
顺序表需要一个结构体,用于保存顺序表相关的内容以及存储元素,这里我们还需要typedef一个数据类型,这样我们顺序表进行存储其他类型内容时,便于修改:
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
int size;
int capacity;
}SeqList;
初始化——销毁:
就是将定义的顺序表结构体进行一个初始化,初始化时需要注意 进行指针有效性进行判断,如果不判断,当别人传入空指针,运行会出现野指针问题,同样销毁也需要;
这里我们还需要注意free的用法:
1. free不支持分开释放(分期还款),只能整体释放
2. free还会检查是否越界(比如说数据类型没写对,空间没开够)
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
void SeqListDestroy(SeqList* ps)
{
assert(ps);
if (ps->a != NULL)
{
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
}
头插——尾插——指定位置插入:
在进行头插尾插之前,我们需要对顺序表进行一个检查,检查是否容量足够。对于指定位置插入注意检查他指定位置是否符合顺序表的结构,是否越界
扩容的时候我们需要用到一个函数: realloc 这个函数可以进行扩容,他需要知道两点:
1. 未申请足够空间,返回NULL,原来的内存空间不变
2. 申请到足够的空间,自动将原空间内容拷贝到新空间,返回新空间的地址,老空间释放
void CheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->capacity == ps->size)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
//realloc 失败后原来空间还在
SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->capacity = newcapacity;
ps->a = tmp;
}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
CheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
CheckCapacity(ps);
for (int i = ps->size - 1; i >= 0; i--)
ps->a[i + 1] = ps->a[i];
ps->a[0] = x;
ps->size++;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
CheckCapacity(ps);
assert(pos >= 0 && pos <= ps->size);
for (int end = ps->size - 1; end >= pos; end--)
ps->a[end + 1] = ps->a[end];
ps->a[pos] = x;
ps->size++;
}
头删——尾删——指定位置删除:
头删和尾删需要注意的是,要注意判断是否支持删除,如果顺序表一个元素都没有,删除就会出现问题,这里也需要检查数据个数有效性,指定位置删除也需要检查指定位置是否合规:
void SeqListPopFront(SeqList* ps)
{
assert(ps);
assert(ps->size>0);
for (int i = 1; i < ps->size; i++)
ps->a[i - 1] = ps->a[i];
ps->size--;
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
assert(ps->size > 0);
ps->size--;
}
void SeqListErase(SeqList* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size - 1);
for (int i = pos; i < ps->size-1;i++)
ps->a[i] = ps->a[i + 1];
ps->size--;
}
查找:
查找还是很简单的,按照数组的遍历就行了:
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
if (ps->a[i] == x)
return i;
return -1;
}
打印:
打印也是按照数组的遍历打印就行了
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
printf("%d ", ps->a[i]);
printf("\n\n\n");
}
好,顺序表大体上的增删查改已经基本实现,结尾附上整体代码。
3.数组相关面试题
1. 原地移除数组中所有的元素 val ,要求时间复杂度为 O(N) ,空间复杂度为 O(1) 。 OJ 链接
这个题我们可以使用双指针,具体思路如下动图:
int removeElement(int* nums, int numsSize, int val) {
int left=0;int right=0;
while(right<numsSize)
{
if(nums[right]==val)
right++;
else
nums[left++]=nums[right++];
}
return left;
}
2. 删除排序数组中的重复项。 OJ 链接
这个题需要用到简单的去重算法,我们可以先将数组排序,然后其他的跟上一个题的思想差不多:
bool cmp(int x,int y)
{
return x-y;
}
int removeDuplicates(int* nums, int numsSize) {
qsort(nums,numsSize,sizeof(int),cmp);
int left=0;int right=1;
while(right<numsSize)
{
if(nums[right]==nums[left])
right++;
else
nums[++left]=nums[right++];
}
return left+1;
}
3. 合并两个有序数组。OJ链接
这个题,需要注意,不创建一个新的数组,这样的话,我们从大的数往前合并可能要更好:
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&&end2>=0)
{
if(nums1[end1]>nums2[end2])
nums1[end--]=nums1[end1--];
else
nums1[end--]=nums2[end2--];
}
//nums2剩余的数放在nums1里面
if(end1<0)
{
while(end2>=0)
nums1[end--]=nums2[end2--];
}
}
4.顺序表的问题
1.尾部插入效率还不错,中间或者头部插入删除,需要挪动数据,效率低下
2.满了以后只能扩容,扩容还是有一定消耗的,扩容一般是存在一定的空间浪费(假设空间100满了,扩容到200,只需要插入120个数据,有80个就浪费了)。如果一次扩得少了,就会频繁的进行开空间,释放空间,拷贝数据,会有不小的消耗。
三、总体代码:
SeqList.h
#pragma once
// SeqList.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
int size;
int capacity;
}SeqList;
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps);
void SeqListDestroy(SeqList* ps);
void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);
SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
void SeqListDestroy(SeqList* ps)
{
assert(ps);
if (ps->a != NULL)
{
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
printf("%d ", ps->a[i]);
printf("\n\n\n");
}
void CheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->capacity == ps->size)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
//realloc 失败后原来空间还在
SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->capacity = newcapacity;
ps->a = tmp;
}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
CheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
CheckCapacity(ps);
for (int i = ps->size - 1; i >= 0; i--)
ps->a[i + 1] = ps->a[i];
ps->a[0] = x;
ps->size++;
}
void SeqListPopFront(SeqList* ps)
{
assert(ps);
assert(ps->size>0);
for (int i = 1; i < ps->size; i++)
ps->a[i - 1] = ps->a[i];
ps->size--;
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
assert(ps->size > 0);
ps->size--;
}
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
if (ps->a[i] == x)
return i;
return -1;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
CheckCapacity(ps);
assert(pos >= 0 && pos <= ps->size);
for (int end = ps->size - 1; end >= pos; end--)
ps->a[end + 1] = ps->a[end];
ps->a[pos] = x;
ps->size++;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size - 1);
for (int i = pos; i < ps->size-1;i++)
ps->a[i] = ps->a[i + 1];
ps->size--;
}