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

9 C++ 顺序结构 Sequential structure

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

9 C++ 顺序结构 Sequential structure

1 修改变量值 Modifying variable values
#include 
#include 
#include 
//void modify_num(int& a) {
//	++a;
//}
//
//void modify_pointer(int*& p) {
//	p = (int*)malloc(20);
//	p[0] = 5;
//}
//
//int main() {
//	int a = 10;
//	modify_num(a);
//	printf("a=%dn", a);
//	int* p = NULL;
//	modify_pointer(p);
//	printf("p[0]=%dn", p[0]);
//	return 0;
//}
2 顺序结构 Sequential structure
#define  MaxSize 50 //定义常量不需要末尾加; Defining constants doesn't require the end add;
typedef int ElemType;

typedef struct {
	ElemType data[MaxSize];
	int length;
}SqList;

bool ListInsert(SqList& L, int i, ElemType e) {
	if (i<1 || i>L.length + 1) {
		return false;
	}
	else if(L.length>=MaxSize){
		return false;
	}
	else {
		for (int j = L.length; j >= i; j--) { //顺序表数据向后移动 Sequential table data moves backward
			L.data[j] = L.data[j - 1];
		}
		L.data[i - 1] = e;
		L.length++;
		return true;
	}
}

bool ListDelect(SqList&L,int i,ElemType &e ) {
	if (i<1 || i>L.length) {
		return false;
	}
	else if (L.length == 0) {
		return false;
	}else {
		e = L.data[i - 1];
		for (int j = i; j < L.length; j++) {//从i的位置依次把元素覆盖 The position of the element is covered from the position of I
			L.data[j - 1] = L.data[j];
		}
		L.length--;
		return true;
	}
}

int locate_element(SqList L, ElemType e) {
	for (int 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]);
	}
	printf("n");
}

int main() {
	SqList L;
	bool ret;
	ElemType del=NULL;
	L.data[0] = 1;
	L.data[1] = 2;
	L.data[2] = 3;
	L.length = 3;
	ret = ListInsert(L, 2, 60);
	if (ret) {
		printf("sccessfuln");
		PrintList(L);//打印成功顺序表 Print successful sequence table
	}
	else {
		printf("failn");
	}

	ret = ListDelect(L, 1, del);
	if (ret) {
		printf("%dn", del);
		PrintList(L);
	}
	else {
		printf("fail");
	}
	ret = locate_element(L, 60);
	if(ret){
		printf("successfuln");
		printf("locate=%dn",ret);
	}else{
		printf("false");
	}
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942823.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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