数据结构_第二关:线性表——顺序表的实现

目录

目录

1.什么是线性表:

2.顺序表

2.1概念及结构

1.静态顺序表

2.动态顺序表:

2.2 接口实现

2.3接口实现:

3 .顺序表相关面试题

3.3.1:

3.3.2:

3.3.3:

4 .顺序表自身性能的问题

原代码:๑乛v乛๑


1.什么是线性表:

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使 用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...

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

2.顺序表

2.1概念及结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存 储。

目标:在数组上完成数据的增删查改。

 顺序表一般分为静态顺序表动态顺序表

1.静态顺序表

//静态顺序表
#define MAX 10
typedef int SLDataType;
//给数据类型重命名,以后直接改这里就可以改变全部的数据类型
typedef struct Seqlist
{
	SLDataType arr[MAX];
	int size;//记录存储了多少个有效数据
}sl;//重命名

2.动态顺序表:

//动态顺序表
typedef int SLDataType;
typedef struct Seqlist
{
	SLDataType *arr;
	int size;
	int capacity;//记录最大存储量
}sl;

2.2 接口实现

静态顺序表只适用于确定知道需要存多少数据的场景。
静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。
所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,

这里博主就重点实现动态的顺序表了:
(两者差别不大,需要静态实现的,课参考动态实现来完成)

//定义/声明:

//动态顺序表
typedef int SLDataType;
typedef struct Seqlist
{
	SLDataType* arr;
	int size;//记录存储了多少个有效数据
	int capacity;//记录最大存储量
}SL;//重命名


void SLInit(SL* ps);//初始化 
void SLDestroy(SL* ps);//销毁

void SLprint(SL* ps);//打印
void SLCheckCapacity(SL* ps);//查看容量

void SLPushBack(SL* ps, int x);//尾插
//将x插入尾部
void SLPopBack(SL* ps);//尾删

void SLPushFront(SL* ps, int x);//头插
void SLPopFront(SL* ps);//头删

void SLInsert(SL* ps, int pos, SLDataType x);//中间插入
//在顺序表ps,下标为pos的位置,插入x
void SLEarse(SL* ps, int pos);//中间删除
//删除下表为pos的值

int SLFind(SL* ps, SLDataType x);//查找特定值
//返回顺序表ps中的值为x的下标

void SLDelete(SL* ps, SLDataType x);//删除特定值

2.3接口实现:

统一放入了最后的原代码中,可供借鉴查找学习๑乛v乛๑

3 .顺序表相关面试题

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

移除元素 - 力扣(LeetCode)

2. 删除排序数组中的重复项。

删除有序数组中的重复项 - 力扣(LeetCode)

3. 合并两个有序数组。

合并两个有序数组 - 力扣(LeetCode)

3.3.1:

思路一:查一次,删一次,
最容易想到的,时间复杂度为O(N^2),空间复杂度:O(1)

int removeElement(int* nums, int numsSize, int val) 
{
	int n = 0;
	for (int i = 0;i < numsSize;i++)
	{
		if (nums[i] == val)
		{
			for (int j = i;j < numsSize - i+1;j++)
			{
				nums[j] = nums[j + 1];
			}
			numsSize--;
		}
	}
	return numsSize;
}
int main()
{
	int arr[] = { 1,4,1,2,1 };
	int v = 0;
	scanf("%d", &v);
	int sz = sizeof(arr) / sizeof(arr[0]);
	int len = removeElement(arr, sz, v);
	for (int i = 0;i < len;i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

思路二:建一个临时数组,利用双指针,以空间换时间
时间复杂度:O(N),空间复杂度:O(N)

就不写了,可以直接看思路三的代码

思路三:双指针,直接在原数组中操作

时间复杂度:O(N),空间复杂度:O(1)

int removeElement(int* nums, int numsSize, int val)
{
	int* b1 = nums;
	int* b2 = nums;
	int n = numsSize;
	for (int i = 0;i < numsSize;i++)
	{
		if (*b1 == val)
		{
			b1++;
			n--;
		}
		else
		{
			*b2++ = *b1++;
		}
	}
	return n;
}
int main()
{
	int arr[] = { 5,4,1,2,1 };
	int v = 0;
	scanf("%d", &v);
	int sz = sizeof(arr) / sizeof(arr[0]);
	int len = removeElement(arr, sz, v);
	for (int i = 0;i < len;i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

3.3.2:

方法:双指针:和上一题类似

 

int removeDuplicates(int* nums, int numsSize)
{
	int src = 0, dst = 0;
	while (src < numsSize)
	{
		if (nums[src] == nums[dst])
		{
			src++;
		}
		else
		{
			nums[++dst] = nums[src++];
		}
	}
	return dst + 1;
}

3.3.3:

方法:双指针,一个指向nums1,一个指向nums2

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) 
{
	int i1 = m - 1, i2 = n - 1;
	int j = m + n - 1;
	while (i1 >= 0 && i2 >= 0)
	{
		if (nums1[i1] > nums2[i2])
		{
			nums1[j--] = nums1[i1--];
		}
		else
		{
			nums1[j--] = nums2[i2--];
		}
	}
	while (i2 >= 0)//如果nums1先结束的,那么还要把nums2里面的值赋给nums1
	{
		nums1[j--] = nums2[i2--];
	}
}

4 .顺序表自身性能的问题

问题:
1. 中间 / 头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。

    例如当前容量为100,满了以后增容到200,
    我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。


思考:如何解决以上问题呢?

在下一关博主会详细写出链表的结构来进行对比。

原代码:๑乛v乛๑

博主在vs2022下写的

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<Windows.h>
//定义/声明:

//动态顺序表
typedef int SLDataType;
typedef struct Seqlist
{
	SLDataType* arr;
	int size;//记录存储了多少个有效数据
	int capacity;//记录最大存储量
}SL;//重命名


void SLInit(SL* ps);//初始化 
void SLDestroy(SL* ps);//销毁

void SLprint(SL* ps);//打印
void SLCheckCapacity(SL* ps);//查看容量

void SLPushBack(SL* ps, int x);//尾插
//将x插入尾部
void SLPopBack(SL* ps);//尾删

void SLPushFront(SL* ps, int x);//头插
void SLPopFront(SL* ps);//头删

void SLInsert(SL* ps, int pos, SLDataType x);//中间插入
//在顺序表ps,下标为pos的位置,插入x
void SLEarse(SL* ps, int pos);//中间删除
//删除下表为pos的值

int SLFind(SL* ps, SLDataType x);//查找特定值
//返回顺序表ps中的值为x的下标

void SLDelete(SL* ps, SLDataType x);//删除特定值


//实现
//初始化
void SLInit(SL* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
//销毁
void SLDestroy(SL* ps)
{
	assert(ps);

	free(ps->arr);
	ps->size = 0;
	ps->capacity = 0;
}
//打印
void SLprint(SL* ps)
{
	assert(ps);

	for (int i = 0;i < ps->size;i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}
//检查容量
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	//扩容
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
		//realloc在arr为空的时候,作用相当于malloc
		//realloc可能会扩容失败(失败是返回NULL),为了防止原数据丢失,
		//需要用一个临时变量来接收扩容后的地址
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);//0为正常终止,-1为异常终止
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
}
//尾插,尾删
void SLPushBack(SL* ps, int x)
{
	/*assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size] = x;
	ps->size++;*/
	SLInsert(ps,ps->size,x);
}
void SLPopBack(SL* ps)
{
	//assert(ps->size);//断言,如果size<0,终止程序
	//ps->size--;
	SLEarse(ps, ps->size);
}
//头插,头删
void SLPushFront(SL* ps, int x)
{
	//assert(ps);
	//SLCheckCapacity(ps);
	挪动数据
	//int end = ps->size-1;
	//while (end >= 0)
	//{
	//	ps->arr[end + 1] = ps->arr[end];
	//	end--;
	//}
	//ps->arr[0] = x;
	//ps->size++;
	SLInsert(ps, 0, x);
}
void SLPopFront(SL* ps)
{
	//assert(ps);
	//assert(ps->size);
	挪动数据
	//int begin = 1;
	//while (begin < ps->size)
	//{
	//	ps->arr[begin-1] = ps->arr[begin];
	//	begin++;
	//}
	//ps->size--;

	SLEarse(ps, 0);
}
//中间插,中间删
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	//先要检查一下pos,防止pos选择越界
	// 
	//if (pos < 0 || pos > ps->size)
	//{
	//	printf(“输入的pos不对”);
	//	exit(-1);
	//	//不想直接结束可以换return;
	//}
	//或者这样写:
	assert(pos >= 0);
	assert(pos <= ps->size);
	//先查看是否扩容
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->arr[end + 1] = ps->arr[end];
		end--;
	}
	ps->arr[pos] = x;
	ps->size++;
}
void SLEarse(SL* ps, int pos)
{
	assert(ps);
	//先要检查一下pos,防止pos选择越界。和中间插一样
	assert(pos >= 0);
	assert(pos <= ps->size );
	
	//挪动数据
	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->arr[begin - 1] = ps->arr[begin];
		begin++;
	}

	ps->size--;
}
//查找特定值
int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0;i < ps->size;i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}
//删除特定值
void SLDelete(SL* ps, SLDataType x)
{
	assert(ps);
	if (SLFind(ps, x) == -1)
	{
		printf("%d在顺序表中不存在\n", x);
	}
	else
	{
		int pos = SLFind(ps, x);
		while (pos != -1)
		{
			SLEarse(ps, pos);
			pos = SLFind(ps, x);
		}
	}

}

//主函数
//测试函数
void text1()
{
	SL s1;
	SLInit(&s1);
	printf("尾插测试:\n");
	SLPushBack(&s1, 1);
	SLPushBack(&s1, 5);
	SLPushBack(&s1, 1);
	SLprint(&s1);

	printf("尾删测试:\n");
	SLPopBack(&s1);
	SLprint(&s1);

	printf("头插测试:\n");
	SLPushFront(&s1, 4);
	SLPushFront(&s1, 5);
	SLPushFront(&s1, 1);
	SLprint(&s1);

	printf("头删测试:\n");
	SLPopFront(&s1);
	SLprint(&s1);


	printf("中间插入测试:\n");
	SLInsert(&s1, 2, 20);
	SLInsert(&s1, 4, 10);
	SLprint(&s1);

	printf("中间删除测试:\n");
	SLEarse(&s1, 3);
	SLprint(&s1);

	printf("查找测试:\n");
	printf("查找元素10并返回下标:%d\n", SLFind(&s1, 10));

	printf("删除元素为1的值,测试:\n");
	SLDelete(&s1, 1);
	SLprint(&s1);

	printf("删除元素为5的值,测试:\n");
	SLDelete(&s1, 5);
	SLprint(&s1);

	SLDestroy(&s1);
}

void mune()
{
	printf("1.尾插\n");
	printf("2.尾删\n");
	printf("3.头插\n");
	printf("4.头删\n");
	printf("5.中间插\n");
	printf("6.中间删\n");
	printf("7.查找特定值\n");
	printf("8.删除特定值\n");
	printf("9.打印数据\n");
	printf("0.退出\n");
}
void text2()
{
	int option = 1;
	SL s2;
	SLInit(&s2);

	SLPushBack(&s2, 1);
	SLPushBack(&s2, 5);
	SLPushBack(&s2, 1);
	SLPushBack(&s2, 2);
	SLPushFront(&s2, 4);
	SLPushFront(&s2, 5);
	SLPushFront(&s2, 1);

	while (option)
	{
		mune();
		printf("请输入:");
		scanf("%d", &option);
		SLDataType a = 0;
		SLDataType b = 0;

		switch(option)
		{
		case 1:
			printf("请依次输入你要尾插的数据,以-1结束:");
			scanf("%d", &a);
			while (a != -1)
			{
				SLPushBack(&s2, a);
				scanf("%d", &a);
			}
			system("cls");

			printf("插入成功!\n");
			break;
		case 2:
			SLPopBack(&s2);
			system("cls");

			printf("尾删成功!\n");
			break;
		case 3:
			printf("请输入你要头插的数据:");
			scanf("%d", &a);
			SLPushFront(&s2, a);
			system("cls");

			printf("头插成功!\n");
			break;
		case 4:
			SLPopFront(&s2);

			system("cls");
			printf("头删成功!\n");
			break;
		case 5:
			printf("请输入要插入的位置下标:");
			scanf("%d", &b);
			printf("请输入要插入的数:");
			scanf("%d", &a);
			SLInsert(&s2, b, a);

			system("cls");
			printf("中间插入成功!\n");
			break;
		case 6:
			printf("请输入要删除的位置下标:");
			scanf("%d", &b);
			SLEarse(&s2, b);

			system("cls");
			printf("中间删除成功!\n");
			break;
		case 7:
		{
			SL s;
			SLInit(&s);
			for (int i = 0;i < s2.size; i++)
			{
				SLCheckCapacity(&s);
				s.arr[i] = s2.arr[i];
				s.size++;
			}
			system("cls");
			printf("请输入要查找的数:");
			scanf("%d", &a);
			printf("查找元素:%d\n返回下标:", a);
			while (SLFind(&s, a) != -1)
			{
				printf("%d  ", SLFind(&s, a));
				SLEarse(&s, SLFind(&s, a));
				if (SLFind(&s, a) != -1)
				{
					SLInsert(&s, SLFind(&s, a), 0);
				}
			}
			printf("\n");
			SLDestroy(&s);
		}
			break;
		case 8:
			printf("请输入要删除的数:");
			scanf("%d", &a);
			SLDelete(&s2, a);

			system("cls");
			printf("删除成功!\n");
			break;
		case 9:
			system("cls");

			SLprint(&s2);
			printf("\n");

			break;
		case 0:
			SLDestroy(&s2);
			break;
		default:
			system("cls");
			printf("选择错误!请重选:\n");
			break;
		}
	}
}
int main()
{
	//text1();
	//菜单函数,建议先用text1调试成功后,再写菜单
	text2();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值