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

C语言实现顺序表自由插入、自由删除

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

C语言实现顺序表自由插入、自由删除

今天在实现顺序表的时候,就想着实现以下自由个数的元素的插入、删除;不知道写的对不对,还请大家帮忙看看 (这些就是在插入,删除中添加了循环,我只是觉得像之前那样频繁调用函数来实现元素的删除、插入很麻烦)

(代码中所指插入的位置就是将这一位来存放该元素,还有就是输入的位置的数字是站在用户角度,所以会比实际存储的下标大1 )

//动态顺序表的实现
#include 
#include 
#define ElemType int
#define OK 1
#define ERROR -1
#define SIZE 10
typedef struct
{
	ElemType* arr;
	int length;//记录当前table的长度
	int real_size;//记录表中的实际元素个数
}SqL,*SqList;
//INIT
int SqL_init(SqList L)
{
	L->arr = (int*)malloc(sizeof(int) * SIZE);
	if (!(L)->arr)
	{
		return ERROR;
	}
	L->length = SIZE;
	L->real_size = 0;
	return OK;
}
//IsEmpty
int IsEmpty(SqL L)
{
	if (L.real_size == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//insert
int SqL_insert(SqList L)
{
	int num = 0;
	int i = 0;
	int value = 0;
	int loc = 0;
	if (L->real_size == L->length)//顺序表已满,就增加空间
	{
		int* pf = NULL;
		(int*)pf = (ElemType*)realloc(L->arr,sizeof(int) * SIZE);
		if (!pf)
		{
			perror("fail to create it>");
			return -9999;
		}
		L->arr = pf;
	}
	printf("please input the NUMBER of that you wanna write>");
	scanf("%d", &num);
	if (num < 1)
	{
		printf("the number is illegal");
		return ERROR;
	}
	printf("the length of table");
	printf("t%d", L->real_size);
	for (i; i < num; i++)
	{
		int j = 0;
		printf("nplease input the VALUE of elemment>");
		scanf("%d", &value);
		printf("the length of table");
		printf("t%d", L->real_size);
		printf("ninput the LOCATION of the elemment> ");
		scanf("%d", &loc);
		if (loc<1 || loc>L->length)
		{
			printf("the position is illeagle>");
			return -999;
		}
		for (j = L->real_size; j>= loc-1; j--)
		{
			*(L->arr + j + 1) = *(L->arr + j);
		}
		*(L->arr + loc-1) = value;
		L->real_size++;

	}
	return OK;
}
//delete
int SqL_del(SqList L)
{
	int num = 0;
	int i = 0;
	int loc = 0;//用户输入的位置,会比实际下标大1
	int ret = IsEmpty(*L);
	if (ret == 1)
	{
		return ERROR;
	}
	printf(" the  CURRENT length of table");
	printf("t%d", L->real_size);
	printf("ninput the nember of NUMBER you wana delete>");
	scanf("%d", &num);
	for (i; i < num; i++)
	{
		int j = 0;
		printf("the Length of table");
		printf("t%d", L->real_size);
		printf("ninput the LOCATION of the elemment> ");
		scanf("%d", &loc);
		if (loc<1 || loc>L->real_size)
		{
			printf("the position is illeagle>");
			return -333;
		}
		for (j = loc - 1; j < L->real_size-1; j++)
		{
			*(L->arr + j) = *(L->arr + j + 1);
		}
		L->real_size--;
	}
	return OK;
}
//traverse
void SqL_traverse(SqL L)
{
	int i = 0;
	int ret = IsEmpty(L);
	if (ret == 1)
	{
		return ERROR;
	}
	else
	{
		for (i; i < L.real_size; i++)
		{
			printf("%dt", *(L.arr + i));
		}
	}
}
int main()
{
	SqL S;
	SqL_init(&S);
	SqL_insert(&S);
	SqL_del(&S);
	printf("n");
	SqL_traverse(S);
	return 0;
}

运行结果:

实现尾插法

头插法:

自由插入:

实现自由删除:

第一个位置(下标为0)插入,数据尾巴删除:

尝试的次数有些少,希望大家看看,有问题记得给我说一下

 

 

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

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

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