顺序表-严蔚敏教材-C语言实现

顺序表

用的是C语言不是C++,编译器vs2019,出现的 scanf_s() 在vs需要,如果复制到其他地方可以改成 scanf()

严蔚敏教材-算法2.1-2.5

线性表的存储结构

#define Status int
#define MAXSIZE 100
#define ElemType int
#define OK 1
#define ERROR -1

typedef struct DATA {
	char id[13];
	char name[50];
	char author[40];
	char press[25];
}Book;

typedef struct SQLIST {
	ElemType* elem;
	int length;
} SqList;

算法2.1:初始化顺序表

//算法2.1:初始化顺序表
Status InitList(SqList* L) {
	L->elem = (Book*)malloc(sizeof(Book) * MAXSIZE);	//分配最大 MAXSIZE 空间
	if (!L->elem) {
		printf("分配空间失败");
		return ERROR;
	}
	L->length = 0;

	return 0;

}

算法2.2:顺序表的取值

//算法2.2:顺序表的取值
Status GetElem(SqList L, int i, ElemType* e) {
	if (i < 1 || i > L.length) {						//判断读取的位置是否合法
		printf("位置不合法\n");
		return ERROR;			
	}
		*e = L.elem[i - 1];								//将对应位置的值赋值给 e 指向地址的值
	return OK;
}

算法2.3:顺序表的查找

//算法2.3:顺序表的查找
int LocateElem(SqList L, ElemType e) {
	for (int i = 0; i < L.length; i++) {
		if (L.elem[i] == e)	return i + 1;
	}
	return 0;
}

算法2.4:顺序表的插入

//算法2.4:顺序表的插入
Status ListInsert(SqList* L, int i, ElemType e) {
	if ((i < 1) || (i > L->length + 1)){				//判断读取的位置是否合法
			printf("位置不合法\n");
			return ERROR;
		}

	if (L->length == MAXSIZE) return ERROR;				//判断当前是否已经满
	
	for (int j = L->length - 1; j > i - 1; j--) {		
		L->elem[j + 1] = L->elem[j];					//插入位置及之后的元素位移
	}
	L->elem[i - 1] = e;									//将元素 e 放入到第 i 个位置
	++(L->length);							//表长加 1
	
	return OK;

}

算法2.5:顺序表的删除

//算法2.5:顺序表的删除
Status ListDelete(SqList* L, int i) {
	if (i < 1 || i > L->length) {				//判断读取的位置是否合法
		printf("位置不合法\n");
		return ERROR;
	}
	for (int j = i; j <= L->length - 1; j++) {
		L->elem[j - 1] = L->elem[j];
	}
	--(L->length);
	return OK;
}

自制-遍历顺序表并输出

//遍历顺序表并输出
Status TraverseList(SqList L) {
	printf("表内元素为:");
	for (int j = 0; j < L.length; j++) {				//从 0 到 MAXSIZE 遍历一次,并输出内容
		printf("%d ", L.elem[j]);
	}
	printf("\n");
	return OK;
}

测试线性表程序

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>

//宏定义
#define Status int
#define MAXSIZE 100
#define ElemType int
#define OK 1
#define ERROR -1

//结构体
typedef struct DATA {
	char id[13];
	char name[50];
	char author[40];
	char press[25];
}Book;

typedef struct SQLIST {
	ElemType* elem;
	int length;
} SqList;

Status InitList(SqList* L);						//2.1 初始化
Status GetElem(SqList L, int i, ElemType* e);	//2.2 取位置 i 的值
int LocateElem(SqList L, ElemType e);			//2.3 查找 e 的位置
Status ListInsert(SqList* L, int i, ElemType e);//2.4 插入指定位置的元素
Status TraverseList(SqList L);					//遍历并输出
Status ListDelete(SqList* L, int i);			//2.5 顺序表的删除


int main() {
	SqList L;
	int i = 0, E = 1, t = 0;
	int s = 0;
	int* e = &E;

	InitList(&L);								//初始化线性表
	
	for (int i = 1; i <= 10; i++,E+=10) {		//线性表初始赋值 1 - 10
		ListInsert(&L, i, E);
	}
	TraverseList(L);//遍历并输出							
	
	printf("1: 查找第 i 的值\n");
	printf("2: 插入e和位置i的\n");
	printf("3: 删除第 i 的值\n");
	printf("4: 查找位置 e 的值\n");
	while( 1 ){
		printf("\n请输入要执行的操作:");
	
		scanf_s("%d", &s);
		
		switch(s){
		case 1:
			printf("\n请输入需要查找值的位置:");		//取位置 i 的值并输出
			scanf_s("%d", &i);
			t = GetElem(L, i, e);
			if(t != ERROR)
				printf("位置第%d个的元素值为:%d\n", i, *e);
	
			break;
	
		case 2:
			printf("\n请输入需要插入的值e:");			//插入值为 e 位置为 i 的值
			scanf_s("%d", e);
			printf("再次输入需要插入值的位置i:");
			scanf_s("%d", &i);
			t = ListInsert(&L, i, *e);		
			if (t != ERROR);
				TraverseList(L);//遍历并输出	
	
			break;
	
		case 3:
			printf("\n请输入需要删除的位置i:");		//删除位置 i 的值
			scanf_s("%d", &i);
			t = ListDelete(&L, i);
			if (t != ERROR);
				TraverseList(L);//遍历并输出
	
			break;
	
		case 4:
			printf("\n请输入需要查找的元素e:");		//查找位置 e 的值
			scanf_s("%d", e);
			t = LocateElem(L, *e);
			if (t != 0)
				printf("元素%d的位置为:%d\n", *e, t);
			else
				printf("没找到%d的值", *e);
			break;
	
		default:
			return 0;
		}
	
	}
	
	return 0;

}

//算法2.1:初始化顺序表
Status InitList(SqList* L) {
	L->elem = (Book*)malloc(sizeof(Book) * MAXSIZE);	//分配最大 MAXSIZE 空间
	if (!L->elem) {
		printf("分配空间失败");
		return ERROR;
	}
	L->length = 0;

	return 0;;

}

//算法2.2:顺序表的取值
Status GetElem(SqList L, int i, ElemType* e) {
	if (i < 1 || i > L.length) {						//判断读取的位置是否合法
		printf("位置不合法\n");
		return ERROR;			
	}
		*e = L.elem[i - 1];								//将对应位置的值赋值给 e 指向地址的值
	return OK;
}

//算法2.3:顺序表的查找
int LocateElem(SqList L, ElemType e) {
	for (int i = 0; i < L.length; i++) {
		if (L.elem[i] == e)	return i + 1;
	}
	return 0;
}

//算法2.4:顺序表的插入
Status ListInsert(SqList* L, int i, ElemType e) {
	if ((i < 1) || (i > L->length + 1)){				//判断读取的位置是否合法
			printf("位置不合法\n");
			return ERROR;
		}

	if (L->length == MAXSIZE) return ERROR;				//判断当前是否已经满
	
	for (int j = L->length - 1; j > i - 1; j--) {		
		L->elem[j + 1] = L->elem[j];					//插入位置及之后的元素位移
	}
	L->elem[i - 1] = e;									//将元素 e 放入到第 i 个位置
	++(L->length);							//表长加 1
	
	return OK;

}

//算法2.5:顺序表的删除
Status ListDelete(SqList* L, int i) {
	if (i < 1 || i > L->length) {				//判断读取的位置是否合法
		printf("位置不合法\n");
		return ERROR;
	}
	for (int j = i; j <= L->length - 1; j++) {
		L->elem[j - 1] = L->elem[j];
	}
	--(L->length);
	return OK;
}

Status TraverseList(SqList L) {
	printf("表内元素为:");
	for (int j = 0; j < L.length; j++) {				//从 0 到 MAXSIZE 遍历一次,并输出内容
		printf("%d ", L.elem[j]);
	}
	printf("\n");
	return OK;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值