栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言实现顺序表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言实现顺序表

#include 
#include 

#define N 32

typedef int datatype_t;

typedef struct
{
	datatype_t data[N];
	int last;
} seqlist_t;

// 判断顺序表是否满了
int seqlist_full(seqlist_t *l) {
	return l->last == N - 1 ? 1 : 0;
}

// 创建一个线性表
seqlist_t* seqlist_create() {
	seqlist_t *s1 = (seqlist_t *)malloc(sizeof(seqlist_t));
	s1->last = -1;
	return s1;
}

// 不指定位置插入数据节点
int seqlist_insert(seqlist_t *l, int value) {
	if (seqlist_full(l))
	{
		printf("seqlist is fulln");
		return -1;
	}

	l->last++;
	l->data[l->last] = value;
	return 0;
}

// 在指定位置插入元素
int seqlist_pos_insert(seqlist_t *l, int pos, datatype_t value) {
	int i = 0;
	if (seqlist_full(l))
	{
		printf("seqlist is fulln");
		return -1;
	}

	// 判断要插入的位置是否合理
	if (pos < 0 || pos > l->last)
	{
		printf("pos value invalied inputn");
		return -1;
	}

	// 通过for循环 将表中大于pos位置的元素向后移动一个位置
	for (int i = l->last + 1; i > pos; --i)
	{
		l->data[i] = l->data[i - 1];
	}

	// 将要插入的值放到指定的位置
	l->data[pos] = value;
	l->last++;
	return 0;
}

// 打印线性表的内容
int seqlist_show(seqlist_t *l) {
	for (int i = 0; i <= l->last; ++i)
	{
		printf("%d ", l->data[i]);
	}
	printf("n");
	return 0;
}

int seqlist_empty(seqlist_t *l) {
	return l->last == -1 ? 1 : 0;
}

// 删除最后面的数据节点
int seqlist_delete(seqlist_t *l) {
	if (seqlist_empty(l))
	{
		printf("seqlist emptyn");
		return -1;
	}

	datatype_t value;
	value = l->data[l->last];
	l->last--;
	return value;
}

// 删除指定位置的数据节点
int seqlist_pos_delete(seqlist_t *l, int pos) {
	// 判断要删除的是否为空
	if (seqlist_empty(l))
	{
		printf("seqlist emptyn");
		return -1;
	}

	// 判断删除的位置是否合理
	if (pos > l->last || pos <= -1)
	{
		printf("pos value invalied inputn");
		return -1;
	}

	datatype_t value = l->data[pos];
	for (int i = pos; i < l->last; i++)
	{
		l->data[i] = l->data[i+1];
	}
	
	l->last--;
	return value;
}

// 修改数据节点的值
int seqlist_change(seqlist_t *l, datatype_t old_value, datatype_t new_value) {
	// 判断要删除的是否为空
	if (seqlist_empty(l))
	{
		printf("seqlist emptyn");
		return -1;
	}

	for (int i = 0; i <= l->last; ++i)
	{
		if (l->data[i] == old_value)
		{
			l->data[i] = new_value;
			return 0;
		}
	}

	printf("未找到数据n");
	return -1;
}

// 查找数据节点所在位置
int seqlist_search(seqlist_t *l, datatype_t value) {
	// 判断要删除的是否为空
	if (seqlist_empty(l))
	{
		printf("seqlist emptyn");
		return -1;
	}

	for (int i = 0; i <= l->last; ++i)
	{
		if (l->data[i] == value)
		{
			return i;
		}
	}
	return -1;
}

// 删除线性表中重复的数据节点
int seqlist_purge(seqlist_t *l) {
	for (int i = 0; i < l->last; i++)
	{
		for (int j = i + 1; j <= l->last; j++)
		{
			if (l->data[i] == l->data[j]) // 发现重复元素
			{
				seqlist_pos_delete(l, j);// 删除元素将会导致后面所有元素下标向前移动 所以要减一
				j--;
			}
		}
	}
	return 0;
}

// 合并顺序表
int seqlist_union(seqlist_t *l1, seqlist_t *l2) {
	for (int i = 0; i <= l2->last; i++)
	{
		if (seqlist_search(l1, l2->data[i]) == -1)
		{
			seqlist_insert(l1, l2->data[i]);
		}
	}
	
	return 0;
}

// int main() {
// 	seqlist_t *s1;
// 	s1 = seqlist_create();
// 	printf("%dn", seqlist_full(s1));
// 	seqlist_insert(s1, 10);
// 	seqlist_insert(s1, 20);
// 	// seqlist_insert(s1, 20);
// 	seqlist_insert(s1, 30);
// 	seqlist_insert(s1, 40);
// 	seqlist_show(s1);

// 	printf("%dn", seqlist_full(s1));

// 	seqlist_pos_insert(s1,0,5);
// 	seqlist_pos_insert(s1,2,15);
// 	seqlist_pos_insert(s1,4,25);
// 	seqlist_pos_insert(s1,6,35);

// 	seqlist_show(s1);

// 	seqlist_empty(s1);
// 	seqlist_delete(s1);

// 	seqlist_pos_delete(s1, 0);
// 	seqlist_show(s1);
// 	return 0;
// }

int main() {
	seqlist_t *sl1, *sl2;// 定义结构体指针
	sl1 = seqlist_create();// 创建空顺序表
	seqlist_insert(sl1, 10);
	seqlist_insert(sl1, 20);
	seqlist_insert(sl1, 30);
	seqlist_insert(sl1, 40);

	seqlist_show(sl1);

	seqlist_change(sl1, 30, 20);// 修改顺序表中的数据 将30修改为 20

	seqlist_show(sl1);

	seqlist_purge(sl1);

	seqlist_show(sl1);

	sl2 = seqlist_create();
	seqlist_insert(sl2, 30);
	seqlist_insert(sl2, 50);
	seqlist_insert(sl2, 70);

	seqlist_union(sl1, sl2);
	seqlist_show(sl1);
	return 0;
}

输出结果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1025703.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号