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

数据结构与算法:顺序表综合练习1(C++实现)

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

数据结构与算法:顺序表综合练习1(C++实现)

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#define MaxSize 50
#define ElemType int
typedef struct {
	ElemType data[MaxSize];
	int Length;
}SqList;
//基本接口
bool ListInsert(SqList &L,int i,ElemType e) {
	if (i<1 || i>L.Length) { return false; }
	if (L.Length > MaxSize) { return false; }
	for (int j = L.Length; j >= i; j--) {
		L.data[j] = L.data[j - 1];
	}
	L.data[i - 1] = e;
	L.Length++;
	return true;
}
bool ListDelete(SqList& L, int i, ElemType& e) {
	if (i<1 || i>L.Length) {
		return false;
	}
	e = L.data[i - 1];
	for (int j = i; j < L.Length; j++) {
		L.data[j - 1] = L.data[j];
	}
	L.Length--;
	return true;
}
int LocateElem(SqList L, ElemType e) {
	int i;
	for (i = 0; i < L.Length; i++) {
		if (L.data[i] == e) {
			return i + 1; //返回位序
		}
	}
	return 0;
}
void PrintList(SqList L) {
	for (int i = 0; i < L.Length; i++) {
		printf("%3d", L.data[i]);
	}
}
void swap(int &a,int &b) {
	int temp;
	temp = b;
	b = a;
	a = temp;
}
//基本接口
//输出最小值元素
int printMin(SqList L) {
	int temp = L.data[0];
	int MaxElem;
	for (int i = 0; i < L.Length; i++) {
		if (temp < L.data[i]) {}
		else {
			temp = L.data[i];
		}
	}
	MaxElem = temp;
	return MaxElem;
}
//元素逆置
void reverseList(SqList &L) {
	for (int i = L.Length; i >= L.Length / 2; i--) {
		swap(L.data[L.Length - i], L.data[i-1]);
	}
}
//删除所有值为x的元素
void deleteElemX(SqList &L,ElemType x) {
	int i = 0;
	for (int j = 0; j < L.Length; j++) {
		if (L.data[j] == x)
			i++;
		L.data[j] = L.data[j + i];
	}
	L.Length = L.Length - i;
}
//删除给定值之间的所有元素
bool delete_Range_Elements(SqList &L,ElemType sta,ElemType end) {
	if (sta<1 || sta>L.Length) { return false; }
	if (end<1 || end>L.Length) { return false; }
	if (sta > end) { return false; }
	if (L.Length == 0) { return false; }
	for (int i = sta; i <= end; i++) {
		L.data[i-1] = L.data[(i-1)+(end-sta+1)];
	}
	L.Length = L.Length - (end - sta + 1);
	return true;
}
//从有序顺序表中删除所有值重复的元素
bool Remove_duplicate_elements(SqList& L) {
	int i = 0;
	if (L.Length == 0) { return false; }
	for (int j = 0; j < L.Length; j++) {
		if (L.data[j] == L.data[j + 1]) {
			i++;
		}
		L.data[j] = L.data[j + i];
	}
	L.Length = L.Length - i;
	return true;
}
//将两个有序顺序表合并为一个有序顺序表
SqList union_List1(SqList& L1, SqList& L2) 
{
	SqList rnew;
	rnew.Length = L1.Length + L2.Length;
	int i=0, j=0, k = 0;
	while (i < L1.Length && j < L2.Length) {
		if (L1.data[i] < L2.data[j]) {
			rnew.data[k++] = L1.data[i++];
		}
		else {
			rnew.data[k++] = L2.data[j++];
		}
	}
	while (i < L1.Length) {
		rnew.data[k++] = L1.data[i++];
	}
	while (i < L2.Length) {
		rnew.data[k++] = L2.data[j++];
	}
	return rnew;
}

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

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

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