【数据结构与算法】【知识体系整理】1-顺序表

本文详细介绍了顺序表的数据结构,包括其定义(连续存储、动态扩容),并着重讲解了插入、删除操作的实现,以及malloc、calloc、realloc内存申请方法。通过实例代码演示了如何创建、操作和扩展顺序表,适合初学者理解动态数组概念。
摘要由CSDN通过智能技术生成

1 数据结构介绍

程序 = 算法 + 数据结构

程序设计 = 算法 + 数据结构 + 编程范式

数据结构 = 结构定义 + 结构操作

在这里插入图片描述

图1 结构定义和结构操作

算法→计算资源→时间复杂度

数据结构→存储资源→空间复杂度

2 顺序表

顺序表可以理解为更高级的数组,原因如下:

  • 顺序表的大小不是在编译时决定的,可以动态开辟。
  • 顺序表的大小是可以更改的(扩容)。

2.1 顺序表的结构定义

顺序表是一段连续的存储空间,其中可以存储任意类型数据(同一顺序表中,各元素必须是相同类型)。

在这里插入图片描述

图2 顺序表示意

capacity:顺序表的容量(不扩容的前提下,最多能存多少元素)

size:顺序表中已有的元素个数

data_type:顺序表中元素的类型

2.2 顺序表的结构操作

2.2.1 顺序表插入

在这里插入图片描述

图3 顺序表插入操作

2.2.2 顺序表删除

在这里插入图片描述

图4 顺序表删除操作

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值