顺序表的基本创建及增删改查修和销毁

Posted 别碰我的宏定义

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顺序表的基本创建及增删改查修和销毁相关的知识,希望对你有一定的参考价值。

顺序表的基本创建及增删改查修和销毁
在头文件中定义需要的函数及数据结构以及需要引入的头文件,顺序表是动态创建的,那与其对应的删除也应该存在

#ifndef _SQELIST_H_
#define _SQELIST_H_
typedef int DateType;
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<assert.h>
#pragma warning(disable:4996)
typedef struct SqeList{
	DateType *a;
	int size;
	int capcity;
}SqeList;
//扩容
void SqeListReserve(SqeList *List, int capacity);
//顺序表初始化
void InitSqeList(SqeList *List, int i);
//销毁顺序表
void DestroySqeList(SqeList *List);
//尾插添加元素
void AddSqeList(SqeList *List,DateType date);
//任意位置插入元素
void SqeListInsert(SqeList *List, DateType date, int pos);
//尾删
void EarseSqeList(SqeList *List);
//删除任意位置的元素
void DelSqeList(SqeList *List,DateType date);
//查询某个元素
DateType FindSqeList(SqeList *List, DateType date);
//显示所有元素
void ShowSqeList(SqeList *List);
//修改顺序表数据
void ModSqeList(SqeList *List,DateType date,DateType temp);
//测量顺序表大小
int SqeListcapacity(SqeList *List);
//判断该顺序表是否为空
int IsEmpty(SqeList *List);
//统计顺序表元素个数
int SqeListSize(SqeList *List);
#endif

放入主函数

#include"SqeList.h"
int main()
{
	SqeList List;
	InitSqeList(&List,10);
	AddSqeList(&List,1);
	AddSqeList(&List, 2);
	ShowSqeList(&List);
	DelSqeList(&List, 1);
	DateType date= FindSqeList(&List, 3);
	if (date!=-1)
		printf("Find it ,%d\\n", date);
	else
		printf("Not Find\\n");
	ShowSqeList(&List);
	ModSqeList(&List,2,10);
	ShowSqeList(&List);
	AddSqeList(&List, 2);
	AddSqeList(&List, 3);
	SqeListInsert(&List,8,2);
	SqeListInsert(&List, 7, 2);
	ShowSqeList(&List);
	system("pause");
	return 0;
}
#include"SqeList.h"
void InitSqeList(SqeList *List,int i)
{
	assert(List != NULL);
	List->a= (DateType*)malloc(sizeof(DateType)*i);
	List->capcity = i;
	List->size = 0;

}
void AddSqeList(SqeList *List,DateType date)
{
	if(List->size == List->capcity)
		SqeListReserve(List,SqeListcapacity(List)*2);
	List->a[List->size++] = date;
}
int SqeListcapacity(SqeList *List)
{
	assert(List != NULL);
	return List->capcity;
}
void ShowSqeList(SqeList *List)
{
	assert(List->size!= 0);
	if (IsEmpty(List) == 0)
		printf("没有元素输出\\n");
	int i;
	for (i = 0; i < List->size; i++)
	{
		printf("%d ", List->a[i]);
	}
	printf("\\n");
}
void SqeListReserve(SqeList *List, int capacity)
{
	assert(List != NULL);
	List->a = (DateType*)realloc(List->a, capacity*sizeof(DateType));
	assert(List != NULL);
	List->capcity = 2 * List->capcity;
}
void DelSqeList(SqeList *List,DateType date)
{
	assert(List != NULL);
	int i;
	for (i = 0; i < List->size; i++)
	{
		if (List->a[i] == date)
			break;
	}
	int j;
	for (j = i; j < List->size; j++)
	{
		List->a[j] = List->a[j + 1]; 
	}
	List->size--;
}
DateType FindSqeList(SqeList *List, DateType date)
{
	assert(List != NULL);
	int i;
	for (i = 0; i < List->size; i++)
	{
		if (List->a[i] == date)
			break;
	}
	return i == List->size ? -1: List->a[i];
}
void ModSqeList(SqeList *List, DateType date, DateType temp)
{
	assert(List != NULL);
	for (int i = 0; i < List->size; i++)
	{
		if (List->a[i] == date)
			List->a[i] = temp;
	}
}
int IsEmpty(SqeList *List)
{
	assert(List != NULL);
	return List->size == 0 ? 0 : List->size;
}
int SqeListSize(SqeList *List)
{
	assert(List != NULL);
	return List->size;
}
void EarseSqeList(SqeList *List)
{
	assert(List != NULL);
	List->size--;
}
void SqeListInsert(SqeList *List, DateType date, int pos)
{
	assert(List != NULL);
	if (pos > List->size || pos < 0)
		printf("插入元素位置不正确\\n");
	for (int i = List->size - 1; i >= pos; i--)
	{
		List->a[i + 1] = List->a[i];
	}
	List->a[pos] = date;
	List->size++;
}
void DestroySqeList(SqeList *List)
{
	assert(List != NULL);
	if (List->a)  //如果顺序表没释放,则将其动态开辟的空间free掉,将其指针置为NULL,其空间和数据都置为0
	{
		free(List->a);
		List->a = NULL;
		List->capcity = 0;
		List->size = 0;
	}
}

以上是关于顺序表的基本创建及增删改查修和销毁的主要内容,如果未能解决你的问题,请参考以下文章

数据库与表的创建及增删改查

创建数据库及增删改查基本操作

java DMO及增删改查代码的自动生成

语句创建数据库表及增删改查

数据结构C语言版 —— 顺序表增删改查实现

mysql系列-安装及增删改查