【数据结构与算法】【知识体系整理】1-顺序表
1 数据结构介绍
程序 = 算法 + 数据结构
程序设计 = 算法 + 数据结构 + 编程范式
数据结构 = 结构定义 + 结构操作
算法→计算资源→时间复杂度
数据结构→存储资源→空间复杂度
2 顺序表
顺序表可以理解为更高级的数组,原因如下:
- 顺序表的大小不是在编译时决定的,可以动态开辟。
- 顺序表的大小是可以更改的(扩容)。
2.1 顺序表的结构定义
顺序表是一段连续的存储空间,其中可以存储任意类型数据(同一顺序表中,各元素必须是相同类型)。
capacity:顺序表的容量(不扩容的前提下,最多能存多少元素)
size:顺序表中已有的元素个数
data_type:顺序表中元素的类型
2.2 顺序表的结构操作
2.2.1 顺序表插入
2.2.2 顺序表删除
2.3 顺序表的代码实现
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Vector {
int *data;
int size;
int capacity;
} Vector;
Vector *vector_init(int capacity)
{
Vector *vec = (Vector *)malloc(sizeof(Vector));
if (vec == NULL) return NULL;
vec->data = (int *)malloc(sizeof(int) * capacity);
if (vec->data == NULL) {
free(vec);
return NULL;
}
vec->size = 0;
vec->capacity = capacity;
return vec;
}
void vector_destroy(Vector *vec)
{
if (vec == NULL) return;
free(vec->data);
free(vec);
return;
}
int vector_expand(Vector *vec)
{
if (vec == NULL) return -1;
int expand_size = vec->capacity;
int *buf = NULL;
while (expand_size) {
buf = (int *)realloc(vec->data, sizeof(int) * (vec->size + expand_size));
if (buf != NULL) break;
expand_size >>= 1;
}
if (buf == NULL) return -1;
vec->data = buf;
vec->capacity += expand_size;
printf("Vector expand successfully. Capacity is %d now\n", vec->capacity);
return 0;
}
int vector_insert(Vector *vec, int idx, int val)
{
if (vec == NULL) return -1;
if (idx < 0 || idx > vec->size) return -1;
if (vec->size == vec->capacity) {
if (vector_expand(vec) != 0) return -1;
}
for (int i = vec->size; i > idx; i--) {
vec->data[i] = vec->data[i - 1];
}
vec->data[idx] = val;
vec->size++;
return 0;
}
int vector_erase(Vector *vec, int idx)
{
if (vec == NULL) return -1;
if (idx < 0 || idx > vec->size - 1) return -1;
for (int i = idx; i < vec->size - 1; i++) {
vec->data[i] = vec->data[i + 1];
}
vec->size--;
return 0;
}
void vector_output(Vector *vec)
{
if (vec == NULL) return;
printf("Vector :[");
for (int i = 0; i < vec->size; i++) {
if (i != 0) printf(" ");
printf("%d", vec->data[i]);
}
printf("]\n");
return;
}
int main()
{
#define MAX_OP 20
srand(time(0));
Vector *v = vector_init(MAX_OP);
for (int i = 0; i < MAX_OP; i++)
{
int op = rand() % 4;
int val = rand() % 100;
int ind = rand() % (v->size + 3) - 1;
switch (op) {
case 0:
case 1:
case 2:
printf("Insert %d at %d, to Vector = %d\n", val, ind, vector_insert(v, ind, val));
break;
case 3:
printf("Erase the item at %d from Vector = %d\n", ind, vector_erase(v, ind));
break;
default:
break;
}
vector_output(v);
}
#undef MAX_OP
vector_destroy(v);
return 0;
}
2.4 申请内存的方法
以下三个方法,都是在堆区申请空间。
2.4.1 malloc
#include <stdlib.h>
void *malloc(size_t size);
malloc申请一段内存空间,但这段空间是未经初始化的。
2.4.2 calloc
#include <stdlib.h>
void *calloc(size_t number_of_elements, size_t element_size);
calloc申请一段内存空间,并将其初始化为0。
2.4.3 realloc
#include <stdlib.h>
void *realloc(void *existing_memory, size_t new_size);
realloc函数用来改变先前已经分配的内存块的长度。
realloc会先判断是否能直接在原有的内存后面扩充。如果成功,返回的指针还是指向原有的内存地址。如果无法在原有内存后扩充,就重新申请一块空间,返回新地址。将原空间的内容拷贝到新开辟的内存中,并自动销毁原内存空间。
如果realloc执行失败,会返回空指针NULL。