数据结构——顺序表

定义

1. 线性表

线性表是n个具有相同特性的数据元素的有限序列。常见的线性表有顺序表、链表、栈、队列、字符串等。线性表在逻辑上连续,物理结构上不一定连续。

2. 顺序表

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
本质是从头开始连续存储着数据的数组
(1)静态顺序表:
满了就无法插入,空间大小不好确定,容易造成浪费或空间不足
(2)动态顺序表:
空间不够了就扩容,把原来的空间释放再开辟新的

动态顺序表的实现

// SeqList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SeqListType;
typedef struct SeqList {
	SeqListType *a;
	int size;
	int capacity;
}SL;
// 初始化
void SeqListInit(SL* ps);
// 打印
void SeqListPrint(SL* ps);
// 销毁
void SeqListDestory(SL* ps);
// 扩容
void SeqListCheckCapacity(SL* ps);

// 头插
void SeqListPushFront(SL* ps,SeqListType x);
// 头删
void SeqListPopFront(SL* ps);
// 尾插
void SeqListPushBack(SL* ps, SeqListType x);
// 尾删
void SeqListPopBack(SL* ps);

// 查找,找到返回下标,没找到返回-1
int SeqListFind(SL* ps, SeqListType x);
// 指定下标位置插入
void SeqListInsert(SL* ps,int pos, SeqListType x);
// 删除下标pos的值
void SeqListErase(SL* ps, int pos);

// SeqList.c
// 初始化
void SeqListInit(SL* ps) {
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}
// 打印
void SeqListPrint(SL* ps) {
	for (int i = 0; i < ps->size; i++) {
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
// 扩容
void SeqListCheckCapacity(SL* ps) {
	if (ps->size == ps->capacity) {
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		// capicity指个数,realloc
		SeqListType* tmp = (SeqListType*)realloc(ps->a, newcapacity * sizeof(SeqListType));
		if (tmp == NULL) {
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}
// 销毁
void SeqListDestory(SL* ps) {
	free(ps->a);
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

// 头插
void SeqListPushFront(SL* ps, SeqListType x) {
	// 相当于pos=0的插入
	SeqListInsert(ps, 0, x);

	/*SeqListCheckCapacity(ps);
	ps->size++;
	for (int i = ps->size-1; i > 0; i--) {
		ps->a[i] = ps->a[i-1];
	}
	ps->a[0] = x;*/
}
// 头删
void SeqListPopFront(SL* ps) {
	assert(ps->size > 0);
	int begin = 0;
	while (begin < ps->size-1) {
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size--;
}
// 尾插
void SeqListPushBack(SL* ps, SeqListType x) {
	// 相当于pos=size的插入
	SeqListInsert(ps, ps->size, x);

	/*SeqListCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;*/
}
// 尾删
void SeqListPopBack(SL* ps) {
	if (ps->size > 0) {
		ps->size--;
	}
}

// 查找,找到返回下标,没找到返回-1
int SeqListFind(SL* ps, SeqListType x) {
	int pos = 0;
	while (pos < ps->size) {
		if (ps->a[pos] == x) {
			return pos;
		}
		pos++;
	}
	return -1;
}
// 指定下标位置插入
void SeqListInsert(SL* ps, int pos, SeqListType x) {
	// 插入的位置不能超过顺序表长度,pos=size时就是尾插
	assert(pos <= ps->size && pos >= 0);
	SeqListCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= pos) {
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}
// 删除下标pos的值
void SeqListErase(SL* ps, int pos) {
	assert(ps->size > 0);
	assert(pos < ps->size&& pos >= 0);
	while (pos < ps->size - 1) {
		ps->a[pos] = ps->a[pos + 1];
		pos++;
	}
	ps->size--;
}
// 测试用例
//Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
void TestSL1() {
	SL sl;
	SeqListInit(&sl);
	SeqListPushBack(&sl, 0);
	SeqListPushBack(&sl, 2);
	SeqListPushBack(&sl, 1);
	SeqListPushBack(&sl, 4);
	SeqListPrint(&sl);
	SeqListPopBack(&sl);
	SeqListPushFront(&sl, 11);

	SeqListPrint(&sl);
	SeqListDestory(&sl);

}
void TestSL2() {
	SL sl;
	SeqListInit(&sl);
	SeqListPushFront(&sl,4);
	SeqListPushFront(&sl,3);
	SeqListPushFront(&sl,2);
	SeqListPushFront(&sl,1);
	SeqListPushFront(&sl,0);

	SeqListPrint(&sl);
	SeqListDestory(&sl);
}
void TestSL3() {
	SL sl;
	SeqListInit(&sl);
	SeqListInsert(&sl, 0, 10);
	SeqListInsert(&sl, 0, 1);
	SeqListInsert(&sl, 0, 2);
	SeqListInsert(&sl, 0, 3);
	SeqListInsert(&sl, 0, 4);
	SeqListInsert(&sl, 5, 4);

	SeqListPrint(&sl);

	SeqListErase(&sl, 2);

	SeqListPrint(&sl);
	printf("%d\n", SeqListFind(&sl, 2));
	printf("%d\n", SeqListFind(&sl, 5));


	SeqListDestory(&sl);

}
int main()
{
	printf("test1:\n");
	TestSL1();
	printf("test2:\n");
	TestSL2();
	printf("test3:\n");
	TestSL3();
	
	return 0;
}

运行结果

在这里插入图片描述

缺点

1.空间不够了需要增容,增容需要付出代价:当前地址之后的空间足够则在原地址扩容,空间不够需要开辟新地址,释放原地址
2.扩容会导致一定的空间浪费
3.顺序表要求从开始位置连续存储,因此增删数据需要挪动数据,效率低

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值