数据结构初阶 顺序表的实现与oj题 个人随堂笔记

线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

顺序表

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

在这里实现动态顺序表

首先实现顺序表的开辟与初始化

void SLInit(SL* ps)
{
	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps == NULL)
	{
		perror("malloc error");
		exit(-1);
	}
	ps->size = 0;
	ps->capacity = 4;
};

对应实现顺序表的删除

void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a);
	ps->a=NULL;
	ps->capacity = ps->size = 0;
};

实现顺序表的容量检查与动态内存开辟

void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		SLDataType* temp = (SLDataType*)realloc(ps->a,ps->capacity*sizeof(ps) * 2);

		if (temp == NULL)
		{
			perror("realloc error");
			exit(-1);
		}
		ps->a= temp;
		ps->capacity *= 2;
	}
};

实现顺序表的展示

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

有了初始容量的顺序表后实现数据任意位置的数据增加,也便于后面头插与尾插的实现

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++;
};

实现顺序表任意位置的删除,便于后面尾删头删

void SLErase(SL* ps, int pos)
{
	assert(ps);
	if (ps->size == 0)
		printf("顺序表为空");
	else
	{
		int end = ps->size - 1;
		while (pos < end)
		{
			ps->a[pos] = ps->a[pos + 1];
			pos++;
		}
	}
	ps->size--;
};

实现任意数据地址的查找

int  SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	int i = 0;
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			printf("%d", i);
			return i;
		}
	
	}
	if (i = ps->size)
	{
		printf("查无此数");
		return -1;
	}
};

最后调用void SLInsert(SL* ps, int pos, SLDataType x); void SLErase(SL* ps, int pos);实现头插头删与尾插尾删

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLInsert(ps,ps->size,x);
};


void SLPopBack(SL* ps)
{
	assert(ps);
	SLErase(ps, ps->size);
};

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLInsert(ps, 0, x);

};

void SLPopFront(SL* ps)
{
	assert(ps);
	SLErase(ps,0);
};

随后实现顺序表任意位置数据的修改

void SLModify(SL* ps, int pos, SLDataType x)
{
	ps->a[pos] = x;
};

测试用例:

void test1()
{
	SL SLT;
	SLInit(&SLT);
	SLInsert(&SLT, 0, 0);
	SLInsert(&SLT,1,1);
	SLInsert(&SLT, 2, 2);
	SLInsert(&SLT, 3, 3);
	SLInsert(&SLT, 4, 4);
	SLInsert(&SLT, 5, 5);
	SLInsert(&SLT, 6, 6);
	SLInsert(&SLT, 7, 7);

	SLPrint(&SLT);

	SLErase(&SLT, 6);
	SLErase(&SLT, 2);

	SLPrint(&SLT);

	SLPushBack(&SLT,8);
	SLPushFront(&SLT, -1);

	SLPrint(&SLT);


	SLPopFront(&SLT);
	SLPopBack(&SLT);

	SLPrint(&SLT);

	SLFind(&SLT, -1);
	SLModify(&SLT, 2, 999);

	SLPrint(&SLT);

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

}

在这里插入图片描述

在任意位置删除与头删尾删时,直接挪动数据覆盖,数据不用置0/-1,避免数据原本是0/-1
顺序表只能一次性对所有进行内存释放(删除),想要删除数据只能挪动覆盖,不能对数据小空间单独free或者说局部释放

超出顺序表数据个数的删除,会出现内存的越界访问(越界是不一定报错的,编译器一般检查检查位(内存数组头前与尾后),检查位预设值发生改变时出现报错) ,后popint时发生错误,修改野指针指向的内存。

顺序表的oj
移除元素:给你一个数组nums和一个值val,你需要移除所有数值等于val的元素,并返回移除之后的数组新长度。
不要使用额外的空间,空间复杂度为O(1)并修改输入的数组,元素的顺序可以改变,你不需要考虑数组中超出新长度后面元素

int Remove_the_element2(int * arr,int removenumber,int size)
{
	int left = 0;
	int right = 0;
	while (right < size)
	{
		if (arr[right] != removenumber)
		{
			arr[left] = arr[right];
			left++;
			right++;
		}
		else
		{
			right++;
		}
	}
	return left;
}

void printarr(int* arr,int size)
{
	for (int i = 0; i < size; i++)
		printf("%d	\n", arr[i]);
}
int main()
{
	int arr1[] = { 1,22,6,9,4,6,1,359,6,12,358,6,6,5,61,6 };
	int arr2[] = { 1,22,6,9,4,6,1,359,6,12,358,6,6,5,61,6 };
	int ret=Remove_the_element2(arr2, 6, sizeof(arr2) / sizeof(arr2[0]));
	printarr(arr2, ret);

	return 0;
}

合并两个有序数组oj
两个非递减顺序的整数数组num1与num2,另外有两个整数分别表示num1余num2中的元素个数。合并num2到num1中,时合并后的数组同样按照非递减顺序排列。

注意:最终

方法:倒着比较,取最大的依次往前插入

void merge(int* num1, int* num2, int m, int n)
{
	int end1 = m-1;
	int end2 = n-1;
	int end = m + n-1;
	while (end1 >= 0 && end2 >= 0)
	{
		if (num1[end1] < num2[end2])
		{
			num1[end]=num2[end2];
			end2--;
			end--;
		}
		else
		{
			num1[end] = num1[end1];
			end1--;
			end--;
		}
	}
	if (end2 != 0)
	{
		for (int i = 0; i <= end2; i++)
		{
			num1[0] = num2[i];
		}
	}
}

int main()
{
	int num1[] = {2,3,4,10,98,256,666,0,0,0,0};
	int num2[] = {56,87,102,301};
	merge(num1, num2, 7, 4);
	for (int i = 0; i < sizeof(num1) / sizeof(num1[0]); i++)
	{
		printf("%d\n", num1[i]);
	}
	return 0;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值