数据结构——顺序表

1.1 顺序表

1.1.1 定长顺序表
(1)结构体定义
/*===========结构体定义==========*/
#define ElemType int
#define MaxSize 10

typedef struct List{
	ElemType data[MaxSize]; // 数组
	int len; // 长度
}List;
(2)初始化
/*===========初始化===============*/
List* List_Init(void){
	/*为List分配堆内存*/
	List *L = malloc(sizeof(List));
	/*动态分配失败处理*/
	if(L == NULL){
		fprintf(stderr,"Init malloc is error\n");
		return NULL;
	}
	/*数组长度指向0*/
	L->len = 0;
	
	/*测试*/
	puts("Init_List is success");
	return L;
}
(3)清空
/*==============清空==============*/
void List_Clear(List *L){
	/*清空只需要将长度置0*/
	L->len = 0;
	/*测试*/
	puts("Clear_List is success");
}
(4)销毁
/*===========销毁============*/
void List_Destory(List *L){
	/*已销毁判断*/
	if(L == NULL){
		fprintf(stderr,"Destory List is error, List can not be destory\n");
		exit(1);
	}
	/*释放L,并置为空指针*/
	free(L);
	L = NULL;
	
	/*测试*/
	puts("Destory List is success");
}
(5)获取长度
/*=========打印长度============*/
int List_GetLen(const List *L){
	return L->len;
}
(6)判断是否为空
/*===========判断是否为NULL===========*/
bool List_IsEmpty(const List *L){
	if(L->len == 0){
		return true;
	}
	return false;
}
(7)判断是否已满
/*==========判断是否已满=============*/
bool List_IsFull(const List *L){
	if(L->len == MaxSize){
		return true;
	}
	return false;
}
(8)头插式创建
/*==========头插式创建============*/
bool List_CreatHead(List *L, ElemType arr[], int length){
	int i = 0;
	
	/*长度判断*/
	if(L->len + length > MaxSize){
		fprintf(stderr,"Length of List is not enough\n");
		return false;
	}
	
	/*元素全部向后移动length个位置,空出前length位置*/
	for(i = L->len - 1;i >= 0;i--){
		L->data[i + length] = L->data[i];
	}
	
	/*将length个元素逆序复制到头部前length空间*/
	for(i = 0;i < length;i++){
		L->data[i] = arr[length - i - 1];
	}
	/*长度加length*/
	L->len += length;
	
	return true;
}
(9)尾插式创建
/*=============尾插式创建==============*/
bool List_CreatTail(List *L, ElemType arr[], int length){
	int i= 0;
	/*长度判断*/
	if(L->len + length > MaxSize){
		fprintf(stderr,"Length of List is not enough\n");
		return false;
	}
	/*遍历元素赋值*/
	for(i = 0;i < length;i++){
		L->data[i + L->len] = arr[i]; 
		
	}
	
	/*长度加length*/
	L->len += length;
	
	return true;
}

(10)查找元素
/*==============查找元素,返回下标==========*/
int List_IndexOf(const List *L, ElemType e){
	int i = 0;
	for(i = 0;i < L->len;i++){
		if(L->data[i] == e){
			/*查找成功*/
			return i;
		}
	}
	/*查找失败*/
	return -1;
}
(11)获取第loc个元素
/*===========获取第loc个元素===========*/
ElemType List_GetElem(const List *L, int loc){
	/*判断是否越界*/
	if(loc < 1 || loc > L->len){
		fprintf(stderr,"the index is not suitable\n");
		exit(1);
	}
	/*返回loc - 1位置元素*/
	return L->data[loc - 1];
}
(12)在loc位置插入元素
/*=======在loc位置插入元素=====*/
bool List_Insert(List *L, int loc, ElemType e){
	int i = 0;
	/*判断是否越界*/
	if(loc < 1 || loc > L->len + 1){
		fprintf(stderr,"the index is not suitable\n");
		return false;
	}
	/*将loc - 1起的元素全部向后搬运一个位置*/
	for(i = L->len;i >= loc;i --){
		L->data[i] = L->data[i - 1];
	}
	/*插入元素*/
	L->data[loc - 1] = e;
	/*长度加1*/
	L->len ++;
	return true;
}
(13)删除loc位置元素
/*===========删除第loc个元素============*/
bool List_Delete(List *L, int loc, ElemType *e){
	int i = 0;
	/*判断是否越界*/
	if(loc < 1 || loc > L->len){
		fprintf(stderr,"the index is not suitable\n");
		return false;
	}
	/*e保留删除值*/
	*e = L->data[loc - 1];
	
	/*将loc位置元素前前搬运一个位置*/
	for(i = loc; i < L->len; i ++){
		L->data[i - 1] = L->data[i];
	}
	/*长度减一*/
	L->len --;
	return true;
}
(14)修改第loc位置元素
/*============修改第loc个元素============*/
bool List_Modify(List *L, int loc, ElemType e){
	/*判断是否越界*/
	if(loc < 1 || loc > L->len){
		fprintf(stderr,"the index is not suitable\n");
		return false;
	}
	L->data[loc - 1] = e;
	return true;
}
(15)打印顺序表
/*==============打印================*/
void List_Show(const List *L){
	int i = 0;
	for(i = 0;i < L->len;i++){
		printf("%d\t",L->data[i]);
	}
	putchar('\n');
}
1.1.2 变长顺序表
(1)结构体定义
/*===========结构体定义==========*/
#define ElemType int
#define InitSize 10  // 初始长度

typedef struct List{
	ElemType *data; // 数组
	int len; // 目前元素所占长度
	int size; // 当前最大长度
}List;

(2)初始化
/*===========初始化===============*/
void List_Init(List* L){
	/*为data分配堆内存,长度为初始长度*/
	L->data = malloc(sizeof(ElemType) * InitSize);
	/*动态分配失败处理*/
	if(L->data == NULL){
		fprintf(stderr,"Init malloc is error\n");
		exit(1);
	}
	/*数组长度指向0*/
	L->len = 0;
	/*最大长度为初始长度*/
	L->size = InitSize;
	
	/*测试*/
	puts("Init_List is success");
}
(3)清空
/*==============清空==============*/
void List_Clear(List *L){
	/*已销毁判断*/
	if(L->data == NULL){
		fprintf(stderr,"Destory List is error, List can not be destory\n");
		exit(1);
	}
	/*清空只需要将长度置0*/
	L->len = 0;
	/*测试*/
	puts("Clear_List is success");
}
(4)销毁
/*===========销毁============*/
void List_Destory(List *L){
	/*已销毁判断*/
	if(L->data == NULL){
		fprintf(stderr,"Destory List is error, List can not be destory\n");
		exit(1);
	}
	/*释放L->data,并置为空指针*/
	free(L->data);
	L->data = NULL;
	
	/*表的最大容量设置为-1*/
	L->size = -1;
	L->len = -1;
	
	/*测试*/
	puts("Destory List is success");
}
(5)获取长度
/*=========获取长度============*/
int List_GetLen(const List *L){
	/*已销毁判断*/
	if(L->data == NULL){
		fprintf(stderr,"Destory List is error, List can not be destory\n");
		exit(1);
	}
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
	}
	return L->len;
}
(6)判断是否为空
/*===========判断是否为NULL===========*/
bool List_IsEmpty(const List *L){
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	if(L->len == 0){
		return true;
	}
	return false;
}
(7)数组扩容
/*=============数组扩容===========*/
void List_Malloc(List *L, int length){
	int len = L->len + length;
	ElemType *new_data = NULL;
	int i = 0;
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	/*判断是否需要扩容*/
	while(L->size < len){
		/*两倍扩容*/
		L->size *= 2;
	}
	/*分配新数组用来存扩容的内容*/
	new_data = malloc(sizeof(ElemType) * L->size);
	/*将旧数组的内容拷贝过来*/
	for(i = 0;i < L->len;i++){
		new_data[i] = L->data[i];
	}
	/*释放旧的data*/
	free(L->data);
	/*L->data修改位new_data*/
	L->data = new_data;
}
(8)头插式创建
/*==========头插式创建============*/
bool List_CreatHead(List *L, ElemType arr[], int length){
	int i = 0;
	
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	
	/*长度判断*/
	if(L->len + length > L->size){
		/*扩容*/
		List_Malloc(L,length);
	}
	
	/*元素全部向后移动length个位置,空出前length位置*/
	for(i = L->len - 1;i >= 0;i--){
		L->data[i + length] = L->data[i];
	}
	
	/*将length个元素逆序复制到头部前length空间*/
	for(i = 0;i < length;i++){
		L->data[i] = arr[length - i - 1];
	}
	/*长度加length*/
	L->len += length;
	
	return true;
}
(9)尾插式创建
/*=============尾插式创建==============*/
bool List_CreatTail(List *L, ElemType arr[], int length){
	int i= 0;
	
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	
	/*长度判断*/
	if(L->len + length > L->size){
		/*扩容*/
		List_Malloc(L,length);
	}
	
	/*遍历元素赋值*/
	for(i = 0;i < length;i++){
		L->data[i + L->len] = arr[i]; 
		
	}
	
	/*长度加length*/
	L->len += length;
	
	return true;
}
(10)查找元素
/*==============查找元素,返回下标==========*/
int List_IndexOf(const List *L, ElemType e){
	int i = 0;
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	for(i = 0;i < L->len;i++){
		if(L->data[i] == e){
			/*查找成功*/
			return i;
		}
	}
	/*查找失败*/
	return -1;
}
(11)获取第loc个元素
/*===========获取第loc个元素===========*/
ElemType List_GetElem(const List *L, int loc){
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	/*判断是否越界*/
	if(loc < 1 || loc > L->len){
		fprintf(stderr,"the index is not suitable\n");
		exit(1);
	}
	/*返回loc - 1位置元素*/
	return L->data[loc - 1];
}
(12)在loc位置插入元素
/*=======在loc位置插入元素=====*/
bool List_Insert(List *L, int loc, ElemType e){
	int i = 0;
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	
	/*判断是否越界*/
	if(loc < 1){
		fprintf(stderr,"the index is not suitable\n");
		return false;
	}
	/*扩容*/
	List_Malloc(L,1);
	
	/*将loc - 1起的元素全部向后搬运一个位置*/
	for(i = L->len;i >= loc;i --){
		L->data[i] = L->data[i - 1];
	}
	/*插入元素*/
	L->data[loc - 1] = e;
	/*长度加1*/
	L->len ++;
	return true;
}
(13)删除loc位置元素
/*===========删除第loc个元素============*/
bool List_Delete(List *L, int loc, ElemType *e){
	int i = 0;
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	/*判断是否越界*/
	if(loc < 1 || loc > L->len){
		fprintf(stderr,"the index is not suitable\n");
		return false;
	}
	/*e保留删除值*/
	*e = L->data[loc - 1];
	
	/*将loc位置元素前前搬运一个位置*/
	for(i = loc; i < L->len; i ++){
		L->data[i - 1] = L->data[i];
	}
	/*长度减一*/
	L->len --;
	return true;
}

(14)修改第loc位置元素
/*============修改第loc个元素============*/
bool List_Modify(List *L, int loc, ElemType e){
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	/*判断是否越界*/
	if(loc < 1 || loc > L->len){
		fprintf(stderr,"the index is not suitable\n");
		return false;
	}
	L->data[loc - 1] = e;
	return true;
}
(15)打印顺序表
/*==============打印================*/
void List_Show(const List *L){
	int i = 0;
	/*链表已销毁*/
	if(L->size == -1){
		fprintf(stderr,"list is not exist\n");
		exit(1);
	}
	for(i = 0;i < L->len;i++){
		printf("%d\t",L->data[i]);
	}
	putchar('\n');
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值