Lesson2--顺序表和链表(上)

1.线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。

线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...
线性表在逻辑上是线性结构,也就说是连续的一条直线。

但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

 

2.顺序表 

2.1概念及结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:

1. 静态顺序表:使用定长数组存储元素。 

2. 动态顺序表:使用动态开辟的数组存储。

 

 2.2 接口实现

静态顺序表只适用于确定知道需要存多少数据的场景。

静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。

所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,所以下面我们实现动态顺序表。

SeqList.h

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

//#define N 1000
//typedef int SLDataType;//想存什么数据类型改什么
静态顺序表:
静态特点:满了就不能插入 给多少空间很难确定 给多了浪费 给少了不够用
//typedef struct SeqList
//{
//	SLDataType a[N];
//	int size;//表示数组存储了多少个数据
//}SL;

typedef int SLDataType;//想存什么数据类型改什么
typedef struct SeqList
{
	SLDataType* a;//*a==a[]
	int size;//表示数组空间存储了多少个数据
	int capacity;//数组空间
}SL;


// 顺序表初始化
void SeqListInit(SL* ps);
// 检查空间,如果满了,进行增容
void SeqListCheckCapacity(SL* ps);
// 顺序表尾插
void SeqListPushBack(SL* ps, SLDataType x);
// 顺序表尾删
void SeqListPopBack(SL* ps);
// 顺序表头插
void SeqListPushFront(SL* ps, SLDataType x);
// 顺序表头删
void SeqListPopFront(SL* ps);
//顺序表查找:找到了返回下标,没有返回-1
int SeqListFind(SL* ps, SLDataType x);
//顺序表在pos位置插入x
void SeqListInsert(SL* ps, int pos, SLDataType x);
// 顺序表删除pos位置的值
void SeqListErase(SL* ps, int pos);
// 顺序表销毁
void SeqListDestory(SL* ps);
// 顺序表打印
void SeqListPrint(SL* ps);

SeqList.c

顺序表初始化

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"


//初始化置空,赋值0
void SeqListInit(SL* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}



//for循环遍历打印
void SeqListPrint(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}



//插入之前3个问题:
//1.整个顺序表有没有空间
//2.空间不够要扩容
//3.空间足够,直接插入数据
void SeqListCheckCapacity(SL* ps)
{
	assert(ps);
	//1.2--检查空间且扩容
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		//第一次capacity为0 不能直接*2
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}

//直接插入尾--先检查空间,再插入
void SeqListPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	/*SeqListCheckCapacity(ps);

	ps->a[ps->size] = x;
	ps->size++;*/
	SeqListInsert(ps, ps->size, x);
}

//尾删2个问题:
//1.如果顺序表为空,删不了会出错
//2.直接删完事
void SeqListPopBack(SL* ps)
{
	//1.
	assert(ps);
	//温柔的方式
	//if (ps->size > 0)
	//{
	//	ps->size--;
	//}
	//暴力的方式
	assert(ps->size > 0);

	//ps->size--;

	//2.
	SeqListErase(ps, ps->size - 1);
}
//释放且置空置0
void SeqListDestory(SL* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

//头插的几个问题:
//1.插入前先检查空间
//2.头插要移位,原本第一位要变第二位,以此类推
void SeqListPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	//1.
	//SeqListCheckCapacity(ps);
	//2.挪动数据
	//int end = ps->size - 1;
	//while (end >= 0)
	//{
	//	ps->a[end + 1] = ps->a[end];
	//	--end;
	//}
	//ps->a[0] = x;
	//ps->size++;

	SeqListInsert(ps, 0, x);
}

//头删的几个问题:
//1.顺序表为空 删就会出事
//2.删除也有移位,原本第二位变第一位
void SeqListPopFront(SL* ps)
{
	assert(ps);
	//1.
	//assert(ps->size > 0);
	//2.
	//int begin = 1;
	//while (begin < ps->size)
	//{
	//	ps->a[begin - 1] = ps->a[begin];
	//	begin++;
	//}
	//ps->size--;

	SeqListErase(ps, 0);
}

//找到了则返回x位置下标,没有找到则返回-1
int SeqListFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

//任意插入的几个问题:
//0.下标必须在范围内
//1.先检查空间再插入
//2.需要移位,原本第一位要变第二位,以此类推
void SeqListInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	//0.
	//温柔的处理
	//if (ps > ps->size || pos < 0)
	//{
	//	printf("pos invail\n");
	//	return;
	//}

	//暴力方式

	assert(pos >= 0 && pos <= ps->size);
	//1.扩容
	SeqListCheckCapacity(ps);
	//2.挪动数据后插入
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[pos] = x;
	ps->size++;
}

//任意插入的几个问题:
//0.下标必须在范围内
//1.顺序表为空不能删
//2.需要移位,原本第二位要变第一位,以此类推
void SeqListErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;
}

Test.c

#pragma once
#include"SeqList.h"

void test1()
{
	SL sl;
	SeqListInit(&sl);
	SeqListPushBack(&sl, 1);
	SeqListPushBack(&sl, 2);
	SeqListPushBack(&sl, 3);
	SeqListPushBack(&sl, 4);
	SeqListPrint(&sl);
	SeqListPushFront(&sl, 7);
	SeqListPushFront(&sl, 8);
	SeqListPushFront(&sl, 9);
	SeqListPushFront(&sl, 6);
	SeqListPrint(&sl);
	SeqListPopFront(&sl);
	SeqListPopFront(&sl);
	SeqListPopFront(&sl);
	SeqListPopFront(&sl);
	SeqListPrint(&sl);
	SeqListInsert(&sl, 3, 2);
	SeqListInsert(&sl, 3, 2);
	SeqListInsert(&sl, 3, 2); 
	SeqListPrint(&sl);
	SeqListErase(&sl, 2);
	SeqListErase(&sl, 2);
	SeqListErase(&sl, 2);
	SeqListPrint(&sl);



	SeqListDestory(&sl);
}

int main()
{
	test1();
	return 0;
}

2.3 数组相关面试题

1. 原地移除数组中所有的元素val,要求时间复杂度为O(N),空间复杂度为O(1)。OJ链接

2. 删除排序数组中的重复项。OJ链接
3. 合并两个有序数组。OJ链接 

2.4 顺序表的问题及思考

问题:
1. 中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
思考:如何解决以上问题呢?下面给出了链表的结构来看看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值