【数据结构】线性表的基本操作

过程

在这里插入图片描述

在第pos个位置插入

/*
	@brief 在第pos个位置插入元素elem
	@param L 线性表
	@param pos 位序
	@param elem 待插入元素 
	@return  short int
*/
short int ListInsert(SeqList &L, int pos, int elem) {
	//合法判断
	if (pos < 1 || pos > L.length + 1) {
		return (short int) 1;
	} else if (L.length >= InitSize) {
		return (short int) 2;
	}
	
	for (int i = L.length; i >= pos; i--) {
		L.data[i] = L.data[i - 1];
	}
	L.data[pos - 1] = elem;
	L.length++;
	return (short int) 3;
}

删除第pos个元素

short int ListDelete(SeqList &L,int pos, int &e) {
	//合法判断
	if (pos < 1 || pos > L.length) {
		return (short int) 1;
	} else if (L.length == 0) {
		return (short int) 2;
	}
	
	//pos合理
	
	e = L.data[pos - 1];
	
	for (int i = pos; i < L.length; i++) {
		L.data[i - 1] = L.data[i];
	}
	//避免脏数据 
	L.data[L.length - 1] = 0;
	L.length--;
	return (short int) 3;
}

查找

按值查找

int LocateElem(SeqList L, int elem) {
	int pos = -1;
	
	for (int i = 0; i < L.length; i++) {
		if (elem == L.data[i - 1]) {
			pos = i;
			break;
		}
	}
	
	return pos;
}

按照位序查找

int GetElem(SeqList L, int pos) {
	if (pos < 1 || pos > L.length) return INT_MIN;
	
	return L.data[pos - 1];
} 

整体代码

/**
@brief 此用例用来测试对顺序表的插入,删除操作,若有疑问,可联系QQ482722982
@date 2022/3/28 20:37 
*/
#include <iostream>
#include <limits>

using namespace std;

#define InitSize 10

typedef struct {
	int data[InitSize];
	int length;
}SeqList;

/*
	@brief 在第pos个位置插入元素elem
	@param L 顺序表
	@param pos 位序
	@param elem 待插入元素 
	@return  short int
*/
short int ListInsert(SeqList &L, int pos, int elem);

void initList(SeqList &L);
//删除第pos个元素,并将值传给e 
short int ListDelete(SeqList &L,int pos, int &e); 
//输出元素 
void print(SeqList L); 

/*
	@Brief 按照位序查找
	@param L 顺序表 
	@param pos 位序 
*/
int GetElem(SeqList L, int pos); 

int LocateElem(SeqList L, int elem);


int main() {
	SeqList L;
	
	initList(L);
	
	L.data[0] = 1;
	L.data[1] = 2;
	L.data[2] = 3;
	L.length = 3;
	
	print(L);
	short int flag;
	flag = ListInsert(L, 4, 4);
	if (flag == 1) {
		printf("插入位置不合法......\n");
	} else if (flag == 2) {
		printf("当前无位置可插入......\n");
	} else if (flag == 3) {
		printf("插入成功,当前表内内容:");
		print(L);
		system("pause");
	}
	int e;
	flag = ListDelete(L,3,e);
	if (flag == 1) {
		printf("删除位置不合法!\n");
	} else if (flag == 2) {
		printf("当前表中已经没有元素可以删除!");
	} else if (flag == 3) {
		printf("删除成功,删除的元素是:%d\n",e);
		printf("当前表中元素:\a");
		print(L); 
	}
	
	int res = GetElem(L, 9);
	if (res == INT_MIN) {
		cout << "查找的位置不合法!" << endl;
	} else {
		cout << "第9个数是" << res << endl;
	}
	
	res = LocateElem(L, 1);
	if (res == -1) {
		cout << "元素未找到!" << endl;
	} else {
		cout << "元素所在位置为" << res << endl;
	}
	return 0;
} 

short int ListInsert(SeqList &L, int pos, int elem) {
	//合法判断
	if (pos < 1 || pos > L.length + 1) {
		return (short int) 1;
	} else if (L.length >= InitSize) {
		return (short int) 2;
	}
	
	for (int i = L.length; i >= pos; i--) {
		L.data[i] = L.data[i - 1];
	}
	L.data[pos - 1] = elem;
	L.length++;
	return (short int) 3;
}

void initList(SeqList &L) {
	L.length = 0;
}

void print(SeqList L) {
	for (int i = 0; i < L.length; i++) {
		printf("%d ", L.data[i]);
	}
	printf("\n");
}

short int ListDelete(SeqList &L,int pos, int &e) {
	//合法判断
	if (pos < 1 || pos > L.length) {
		return (short int) 1;
	} else if (L.length == 0) {
		return (short int) 2;
	}
	
	//pos合理
	
	e = L.data[pos - 1];
	
	for (int i = pos; i < L.length; i++) {
		L.data[i - 1] = L.data[i];
	}
	//避免脏数据 
	L.data[L.length - 1] = 0;
	L.length--;
	return (short int) 3;
}

int GetElem(SeqList L, int pos) {
	if (pos < 1 || pos > L.length) return INT_MIN;
	
	return L.data[pos - 1];
} 

int LocateElem(SeqList L, int elem) {
	int pos = -1;
	
	for (int i = 0; i < L.length; i++) {
		if (elem == L.data[i - 1]) {
			pos = i;
			break;
		}
	}
	
	return pos;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值