数据结构(1)--> 顺序表

定义:

顺序表存储定义: 把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构,顺序表功能的实现借助于数组,通过对数组进行封装,从而实现增删查改的功能,严格意义上来说(数组无法实现删除数据的功能)。

复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"

void initseqlist(SL* p) {
	assert(p);
	p->arr = NULL;
	p->capacity = p->size = 0;
}

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


void checkcapacity(SL* p) {
	assert(p);
	if (p->capacity == p->size) {
		int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;

		// if here use malloc,the origin data in this array might be missing 

		int* temp = (int*)realloc(p->arr,sizeof(int) * newcapacity);
		p->arr = temp;
		p->capacity = newcapacity;
	}
}



void pushFront(SL* p, int val) {
	assert(p);
	checkcapacity(p);
	int end = p->size - 1;
	while (end >= 0) {
		p->arr[end + 1] = p->arr[end];
		end--;
	}
	p->arr[0 ] = val;
	p->size++;
}

void pushback(SL* p,int val) {
	//assert(p);
	checkcapacity(p);
	p->arr[p->size] = val;
	p->size++;
}


void popfront(SL* p) {
	assert(p);
	int n = p->arr[0];
	printf("将要被pop元素=%d\n", n);
	for (int i = 1; i < p->size ; i++) {
		p->arr[i - 1] = p->arr[i];
	}
	p->size--;
}


void insertanyposition(SL* p, int pos, int val) {
	assert(p);
	assert(pos >= 0 && pos <= p->size);
	int end = p->size - 1;
	while (end>=pos) {
		p->arr[end + 1] = p->arr[end];
		end--;
	}
	p->arr[pos] = val;
	p->size++;
}

int findDataPos(SL* p, int val) {
	assert(p);
	for (int i = 0; i < p->size; i++) {
		if (p->arr[i] == val) {
			return i;
		}
	}
	return -1;
}

1、顺序表的初始化:

复制代码
typedef struct seqlist {
	int* arr;
	int size;
	int capacity;
}SL,*PTR;



void initseqlist(SL* p) {
	assert(p);
	p->arr = NULL;
	p->capacity = p->size = 0;
}

2、顺序表容量检测:

当我们要对表里进行相关操作的时候,第一步检测当下该表中size 与 容量的关系,可以写一个checkcapacity函数。

复制代码
void checkcapacity2(SL* p) {
	assert(p);
	if (p->capacity == p->size) {
		int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;
		int* temp = (int*)malloc(sizeof(int) * newcapacity);
		p->arr = temp;
		p->capacity = newcapacity;
	}
}


void test3() {
	PTR p;
	SL sl;
	p = &sl;
	initseqlist(p);
	pushback(p, 5);//first init  ---> size=4,capacity=4
	pushback(p, 15);
	pushback(p, 25);
	pushback(p, 35);
	pushback(p, 45);
	printseqlist(p);

}

这个时候来看一下打印结果:

为什么会这样呢,这个时候我们就需要借助调试工具来找出问题所在

所以我们该处可以用realloc 函数 来进行动态内存管理:

复制代码
void checkcapacity(SL* p) {
	assert(p);
	if (p->capacity == p->size) {
		int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;

		// if here use malloc,the origin data in this array might be missing 

		int* temp = (int*)realloc(p->arr,sizeof(int) * newcapacity);
		p->arr = temp;
		p->capacity = newcapacity;
	}
}

3、顺序表插入数据:

3.1头插:

复制代码
void pushFront(SL* p, int val) {
	assert(p);
	checkcapacity(p);
	int end = p->size - 1;
	while (end >= 0) {
		p->arr[end + 1] = p->arr[end];
		end--;
	}
	p->arr[0 ] = val;
	p->size++;
}

3.2尾插:

复制代码
void pushback(SL* p,int val) {
	//assert(p);
	checkcapacity(p);
	p->arr[p->size] = val;
	p->size++;
}

4、顺序表删除数据:

4.1头删

复制代码
void popfront(SL* p) {
	assert(p);
	int n = p->arr[0];
   // 可以起到记录作用

	printf("将要被pop元素=%d\n", n);
	for (int i = 1; i < p->size ; i++) {
		p->arr[i - 1] = p->arr[i];
	}
	p->size--;
}

4.2尾删

5、任意位置实现插入功能:

复制代码
void insertanyposition(SL* p, int pos, int val) {
	assert(p);

	assert(pos >= 0 && pos <= p->size);

	int end = p->size - 1;
	while (end>=pos) {
		p->arr[end + 1] = p->arr[end];
		end--;
	}
	p->arr[pos] = val;
	p->size++;
}

6、顺序表中实现查找功能:

复制代码
int findDataPos(SL* p, int val) {
	assert(p);
	for (int i = 0; i < p->size; i++) {
		if (p->arr[i] == val) {
			return i;
		}
	}
	return -1;
}
相关推荐
KyollBM5 分钟前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法
CV点灯大师19 分钟前
C++算法训练营 Day10 栈与队列(1)
c++·redis·算法
GGBondlctrl36 分钟前
【leetcode】递归,回溯思想 + 巧妙解法-解决“N皇后”,以及“解数独”题目
算法·leetcode·n皇后·有效的数独·解数独·映射思想·数学思想
武子康1 小时前
大数据-276 Spark MLib - 基础介绍 机器学习算法 Bagging和Boosting区别 GBDT梯度提升树
大数据·人工智能·算法·机器学习·语言模型·spark-ml·boosting
武子康1 小时前
大数据-277 Spark MLib - 基础介绍 机器学习算法 Gradient Boosting GBDT算法原理 高效实现
大数据·人工智能·算法·机器学习·ai·spark-ml·boosting
Andrew_Xzw1 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
超的小宝贝3 小时前
数据结构算法(C语言)
c语言·数据结构·算法
凤年徐4 小时前
【数据结构初阶】单链表
c语言·开发语言·数据结构·c++·经验分享·笔记·链表
木子.李3478 小时前
排序算法总结(C++)
c++·算法·排序算法