#顺序表静态分配,数据元素类型为结构体,大学基础数据结构

顺序表静态分配,数据元素类型为结构体

大学基础数据结构

//查了没问题






#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define Maxsize 10
//typedef struct student ElemType;

typedef struct student {
	int stu_num;
	char stu_name[20];
}stu,ElemType;

typedef struct SqList_static {//定义并重命名结构体----->顺序表
	ElemType data[Maxsize];//成员为结构体数组,数组大小为Maxsize
	int len;//顺序表的当前长度
}SqList_static;


void InitSqList_static(SqList_static& L) {//初始化顺序表,顺序表类型的引用
	for (int i = 0; i < Maxsize; i++) {
		L.data[i].stu_num = 0;//初始化顺序表元素全为0,避免残留在数组中的脏数据
		strcpy(L.data[i].stu_name, "0");
	}
	L.len = 0;//初始化当前顺序表表长
}


void Print_SqList_static(SqList_static L) {//打印顺序表
	printf("\n");
	for (int i = 0; i < L.len; i++) {
		printf("%d ", L.data[i].stu_num);
		printf("%s ", L.data[i].stu_name);
	}
	printf("\n");
}


bool SqList_static_Search_by_loc(SqList_static L, int i, ElemType& e) {//按位置查找顺序表元素
	if (i<1 || i>L.len)	return false;//寻找位置不合法
	e.stu_num = L.data[i - 1].stu_num;//如果合法返回e
	strcpy(e.stu_name, L.data[i - 1].stu_name);
	return true;
}


bool is_Elem_equal(ElemType a, ElemType b) {//判断两个ElemType变量是否相等,这里是结构体变量
	if (a.stu_num == b.stu_num && 0 == strcmp(a.stu_name, b.stu_name))return true;
	else return false;

}





void SqList_static_Search_by_val(SqList_static L, ElemType e, int a[]) {//按值查找顺序表元素
	for (int i = 0, j = 0; i < L.len; i++) {//整个顺序表扫描一遍
		if (is_Elem_equal(e, L.data[i])) {//如果发现值相等
			a[j] = i + 1;//数组记录下来位序
			j++;//记录数组向后移动一位
		}
	}
}




void a_value_to_b(ElemType &a, ElemType &b) {//将a的值给b
	b.stu_num = a.stu_num;
	strcpy(b.stu_name, a.stu_name);
}




bool SqList_static_Insert(SqList_static& L, int i, ElemType& e) {//按值插入
	if (i<1 || i>L.len + 1)return false;//位置不合法
	if (L.len >= Maxsize)return false;//表满了
	for (int j = L.len; j >= i; j--) {
		a_value_to_b(L.data[j - 1], L.data[j]);
	}
	a_value_to_b(e, L.data[i - 1]);
	L.len++;
}

bool auto_SqList_static_Insert(SqList_static& L, ElemType e[], int num) {//实现在表尾依次插入数组中的多个数
	if (num + L.len <= Maxsize) {
		for (int k = 0; k < num; k++) {
			SqList_static_Insert(L, L.len + 1, e[k]);
		}
		return true;
	}
	else return false;
}

bool SqList_delet(SqList_static& L, int i, ElemType& e) {//按照位置删除元素
	//if (L.len == 0)return false;//空表没法删除元素
	if (i<1 || i>L.len)return false;//删除位置i非法
	a_value_to_b(L.data[i - 1], e);
	for (int j = i; j < L.len; j++) {
		a_value_to_b(L.data[j], L.data[j - 1]);
	}
	L.len--;
}

//bool auto_SqList_dynamic_delet(SqList_static& L, int* arr, int num) {//升序数组中装的是要删除的位序
//	if (num > L.len)return false;
//	for (; num > 0; num--) {
//		SqList_delet(L, arr[num - 1]);
//	}
//	return true;
//}

int main() {
	SqList_static L;//定义顺序表
	InitSqList_static(L);//初始化顺序表
	ElemType e[5] = { 1,"aaa",2,"bbb",3,"ccc",4,"ddd",5,"eee" };//存放添加元素的数组
	if (auto_SqList_static_Insert(L, e, 5)) {
		printf("添加成功,顺序表为:\n");
		Print_SqList_static(L);//打印数组		
	}//自动在表尾添加数组中的元素
	if (auto_SqList_static_Insert(L, e, 5)) {
		printf("添加成功,顺序表为:\n");
		Print_SqList_static(L);//打印数组		
	}//自动在表尾添加数组中的元素



	ElemType ret;//用来得到按照位置查找的返回值
	if(SqList_static_Search_by_loc(L, 3, ret)){
		printf("按照位置查找成功");
		printf("%d %s\n", ret.stu_num,ret.stu_name);//打印返回值
}




	int a[Maxsize] = { -1 };//用来存放按照值查找的位序
	ElemType val_for_search = { 2,"bbb" };
	SqList_static_Search_by_val(L, val_for_search, a);
	printf("查找到的位序数组为:");
	for (int i = 0; i < Maxsize; i++) {
		printf("%d", a[i]);
	}//打印数组


	


	ElemType delet[Maxsize] = {-1};
	if (SqList_delet(L, 1, delet[0])) {
		printf("删除成功,删除后顺序表为:");
		Print_SqList_static(L);
	}else printf("删除失败");
	
	/*SqList_delet(L, 1, delet[1]);
	SqList_delet(L, 1, delet[2]);
	SqList_delet(L, 1, delet[3]);
	SqList_delet(L, 1, delet[4]);*/

	/*SqList_static_Insert(L, 1, e[0])
	SqList_static_Insert(L, 2, e[1]);
	SqList_static_Insert(L, 3, e[2]);
	SqList_static_Insert(L, 4, e[3]);
	SqList_static_Insert(L, 5, e[4]);*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值