线性表和顺序表

目录

1.线性表

2.顺序表

2.1概念及结构

2.2 接口实现

2.3 数组相关面试题

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

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

2.3.3. 合并两个有序数组。


1.线性表

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

线性表是一种在实际中广泛使用的数据结构

常见的线性表:顺序表、链表、栈、队列、字符串...

线性表在逻辑上是线性结构,也就说是连续的一条直线。

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

2.顺序表

2.1概念及结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。(要求:数据必须从第一个位置开始,连续存储在数组上完成数据的增删查改。

顺序表一般可以分为:

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

SeqList.h

#pragma once

#define N 200
typedef int SLDataType;

//静态顺序表
//问题:N太小不够用,N太大,浪费空间
struct SeqList
{
	SLDataType a[N];
	int size;
};

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

SeqList.h

#pragma once

#define N 200
typedef int SLDataType


//动态顺序表   空间不够则增容
typedef struct SeqList
{
	SLDataType* a;//指向动态数组的指针
	int size;//数据个数
	int capacity;//容量——空间大小
}SL;

2.2 接口实现

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

静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,所以下面我们实现动态顺序表。

数据结构就是在内存中管理数据,管理数据也就是完成数据的增删查改

越界检查,第一下越界不会报错,free

越界不一定报错

系统对越界的检查,是设岗抽查

malloc空间一般会查得严一点

防御式代码写法

assert(ps !=NULL);

SeqList.h

函数的声明

#pragma once//防止头文件被重复包含

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

//#define N 200
//typedef int SLDataType;
//
 静态顺序表 -- N太小,可能不够用,N太大,可能浪费空间
//struct SeqList
//{
//	SLDataType a[N];
//	int size;
//};

typedef int SLDataType;

//动态顺序表
typedef struct SeqList
{
	SLDataType* a; // 指向动态数组指针
	int size;      // 有效数据个数
	int capacity;  // 容量-空间大小
}SL;

//打印
void SLPrint(SL* ps);

// 增删查改

//void SeqListInit(SL s);

//顺序表初始化
void SLInit(SL* ps);

//检查
void SLCheckCapacity(SL* ps);

//销毁
void SLDestory(SL* ps);

// 查找
int SLFind(SL* ps, SLDataType x);

//修改
void SLModify(SL* ps, int pos, SLDataType x);

//O(1)
// 尾插
void SLPushBack(SL* ps, SLDataType x);
//尾删
void SLPopBack(SL* ps);

//O(N)
//头插
void SLPushFront(SL* ps, SLDataType x);
//头删
void SLPopFront(SL* ps);

// 顺序表在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x);
// 顺序表删除pos位置的值
void SLErase(SL* ps, int pos);

SeqList.c

顺序表的定义

#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

//打印
void SLPrint(SL* ps)
{
	assert(ps != NULL);
	//assert(ps);

	for (int i = 0; i < ps->size; ++i)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

//初始化
void SLInit(SL* ps)
{
	assert(ps != NULL);

	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

//销毁
void SLDestory(SL* ps)
{
	assert(ps != NULL);

	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
		ps->capacity = ps->size = 0;
	}
}

// 检查容量空间,满了扩容
void SLCheckCapacity(SL* ps)
{
	assert(ps != NULL);

	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity*sizeof(SLDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			//exit(-1);
			return;
		}

		ps->a = tmp;
		ps->capacity = newCapacity;
	}
}

// 尾插
void SLPushBack(SL* ps, SLDataType x)
{
	//assert(ps != NULL);
	//SLCheckCapacity(ps);

	//ps->a[ps->size] = x;
	//ps->size++;

	SLInsert(ps, ps->size, x);
}

//头插
void SLPushFront(SL* ps, SLDataType x)
{
	//assert(ps != NULL);

	//SLCheckCapacity(ps);

	 挪动数据
	//int end = ps->size - 1;
	//while (end >= 0)
	//{
	//	ps->a[end + 1] = ps->a[end];
	//	--end;
	//}

	//ps->a[0] = x;
	//ps->size++;

	SLInsert(ps, 0, x);
}

//尾删
void SLPopBack(SL* ps)
{
	size是有效个数,size--就是直接把这个数据删除,顺序表是按顺序排列的
	ps->a[ps->size - 1] = 0;//可写可不写
	//

	//法一(温柔检查)
	if (ps->size == 0)
	{
		printf("SeqList is empty\n");
		return;
	}

	法二 (暴力检查,直接反馈出错误)
	//assert(ps != NULL);
	//assert(ps->size > 0);//断言 要求:为真没事,为假就报错 

	//ps->size--;

	SLErase(ps, ps->size - 1);

}


//头删
void SLPopFront(SL* ps)
{
	//assert(ps != NULL);
	//assert(ps->size > 0);

	挪动数据进行覆盖
	//int begin = 1;
	//while (begin < ps->size)
	//{
	//	ps->a[begin - 1] = ps->a[begin];
	//	++begin;
	//}

	//ps->size--;

	SLErase(ps, 0);
}


// 顺序表在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);

	SLCheckCapacity(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 SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

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

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

	ps->size--;
}

//查找
int SLFind(SL* ps, SLDataType x)
{
	assert(ps);

	for (int i = 0; i < ps->size; ++i)
	{
		if (ps->a[i] == x)
			return i;
	}

	return -1;
}

//修改
void SLModify(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

	ps->a[pos] = x;
}

Test.c

对顺序表的测试

#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

void TestSeqList1()
{
	SL sl;
	//初始化
	SLInit(&sl);
	//头插
	SLPushBack(&sl, 1);
	SLPushBack(&sl, 2);
	SLPushBack(&sl, 3);
	SLPushBack(&sl, 4);
	//打印
	SLPrint(&sl);

	SLPushBack(&sl, 5);
	SLPrint(&sl);

	SLPushBack(&sl, 5);
	SLPushBack(&sl, 5);
	SLPushBack(&sl, 5);
	SLPushBack(&sl, 5);
	SLPrint(&sl);
}

void TestSeqList2()
{
	SL sl;
	SLInit(&sl);

	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPushFront(&sl, 4);
	SLPushFront(&sl, 5);
	SLPrint(&sl);

	SLPushBack(&sl, 5);
	SLPushBack(&sl, 5);
	SLPrint(&sl);
}

void TestSeqList3()
{
	SL sl;
	SLInit(&sl);
	
	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPushFront(&sl, 4);
	SLPushFront(&sl, 5);
	SLPrint(&sl);

	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPopBack(&sl);
	//SLPopBack(&sl);
	SLPrint(&sl);

	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPrint(&sl);

	SLDestory(&sl);
}

void TestSeqList4()
{
	SL sl;
	SLInit(&sl);

	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPushFront(&sl, 4);
	SLPushFront(&sl, 5);
	SLPrint(&sl);

	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPopFront(&sl);

	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);


	SLPrint(&sl);
}

void TestSeqList5()
{
	SL sl;
	SLInit(&sl);
	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPushFront(&sl, 4);
	SLPushFront(&sl, 5);
	SLPrint(&sl);

	SLInsert(&sl, 3, 40);
	SLPrint(&sl);
	SLInsert(&sl, 0, 40);
	SLPrint(&sl);

	SLPushBack(&sl, 1000);
	SLPrint(&sl);

	SLDestory(&sl);
	
}

void TestSeqList6()
{
	SL sl;
	SLInit(&sl);
	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPushFront(&sl, 4);
	SLPushFront(&sl, 5);
	SLPrint(&sl);

	SLErase(&sl, 2);
	SLPrint(&sl);
	SLErase(&sl, 0);
	SLPrint(&sl);
	SLErase(&sl, 2);
	SLPrint(&sl);

	SLPopFront(&sl);
	SLPrint(&sl);
	SLPopBack(&sl);
	SLPrint(&sl);
}

void TestSeqList7()
{
	SL sl;
	SLInit(&sl);
	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 4);
	SLPushFront(&sl, 5);
	SLPrint(&sl);

	//int x = 0;
	//printf("请输入你要删除的值:");
	//scanf("%d", &x);
	//int pos = SLFind(&sl, x);
	//if (pos != -1)
	//{
	//	SLErase(&sl, pos);
	//}
	//else
	//{
	//	printf("没找到:%d\n", x);
	//}
	//SLPrint(&sl);

	int x = 0;
	printf("请输入你要删除的值:");
	scanf("%d", &x);
	int pos = SLFind(&sl, x);
	while (pos != -1)
	{
		SLErase(&sl, pos);

		pos = SLFind(&sl, x);
	}
	SLPrint(&sl);

	int y, z;
	printf("请输入你要修改的值和修改后的值:");
	scanf("%d%d", &y, &z);
	pos = SLFind(&sl, y);
	if (pos != -1)
	{
		SLModify(&sl, pos, z);
	}
	else
	{
		printf("没找到:%d\n", y);
	}
	SLPrint(&sl);
}



void menu()
{
	printf("****************************************\n");
	printf("1、尾插 2、头插\n");
	printf("3、尾删 4、头删\n");
	printf("5、查找 6、删除\n");
	printf("7、修改 8、打印\n");
	printf("-1、退出\n");
	printf("****************************************\n");
}

int main()
{
	SL sl;
	SLInit(&sl);

	int option = -1;
	do
	{
		menu();
		if (scanf("%d", &option) == EOF)
		{
			printf("scanf输入错误\n");
			break;
		}

		int val, pos;
		int y, z;
		switch (option)
		{
		case 1:
			printf("请连续输入你要尾插的数据,以0结束:>");
			scanf("%d", &val);
			while (val != 0)
			{
				SLPushBack(&sl, val);
				scanf("%d", &val);
			}
			break;
		case 2:
			printf("请连续输入你要头插的数据,以0结束:>");
			scanf("%d", &val);
			while (val != 0)
			{
				SLPushFront(&sl, val);
				scanf("%d", &val);
			}
			break;
		case 3:
			SLPopBack(&sl);
			break;
		case 4:
			SLPopFront(&sl);
			break;
		case 5:
			printf("请输入要查找的数据:>");
			scanf("%d", &val);
			int num = SLFind(&sl, val);
			printf("%d\n", num);
			break;
		case 6:
			printf("请输入你要删除的位置:>");
			scanf("%d", &pos);
			SLErase(&sl, pos);
			break;
		case 7:
			printf("请输入你要修改的值和修改后的值:");
			scanf("%d%d", &y, &z);
			pos = SLFind(&sl, y);
			if (pos != -1)
			{
				SLModify(&sl, pos, z);
			}
			else
			{
				printf("没找到:%d\n", y);
			}
			break;
		case 8:
			SLPrint(&sl);
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (option != -1);

	SLDestory(&sl);

	return 0;
}

2.3 数组相关面试题

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

  • 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
  • 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入

思路一:覆盖删除

时间复杂度O(N^2)

思路二:保留不是val的值,挪到新数组

时间复杂度O(N)

空间复杂度O(N)

思路三:双指针,保留不是val的值,覆盖前面的值

时间复杂度为O(N)

空间复杂度O(1)

src找不是val的值,然后往dst的位置上放

dst找val,dst指向的位置表示可以覆盖

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

}

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

去重算法

给你一个 升序排列的数组 nums ,请你原地删除重复出现的元素,使每个元素只出现一次,返回删除后数组的新长度。元素的相对顺序应该保持 一致 。

由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果。

将最终结果插入 nums 的前 k 个位置后返回 k 。

不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

思路:

相同src继续往前走,不相同src覆盖给dst

dst不动,src相同,dst++

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

2.3.3. 合并两个有序数组。

给你两个按 非递减顺序(可能会有相同的数字,但都是按顺序排列的) 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。

请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。

注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n 。

不是合并成新数组,nums1的大小就是s1+s2

要求时间复杂度为O(M+N)

思路一:开辟一个额外数组,依次从两个数组里小的放在新数组里

思路二:倒着走,取大,覆盖

思路三: 从nums1内的最后一个元素放起

如果end2先结束了,那就不用管,因为数据都进入nums1中了,正好符合题目要求

但如果end1先结束,还需要再把end2的数据放入nums1中

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
    int end1 = m-1;
    int end2 = n-1;
    int end = m+n-1;

    while(end1 >=0 && end2 >= 0)
    {
        if(nums1[end1] > nums2[end2])
        {
            nums1[end--] = nums1[end1--];

        }
        else
        {
            nums1[end--] = nums2[end2--];
        }
    }

    while(end2 >=0)
    {
        nums1[end--] = nums2[end2--];
    }

}

小提示:

       写代码的之前可以通过题目的例子进行画图理解,这样写代码的思路就会清晰很多,画图真的很重要!!!!

  • 32
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 26
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hey pear!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值