数据结构 c++ 顺序表/线性表的基本操作

顺序表的基本操作
顺序表的初始化

空间分配/表长与容量初始化

Status InitList_Sq(SqList& L) {
	L.elems = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
	if (!L.elems) exit(OVERFLOW);//若分配空间失败
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	return OK;
}

释放顺序表

释放L.elem/表长与容量赋0

void DestroyList_Sq(SqList& L) {
	free(L.elems);
	L.length = 0;
	L.listsize = 0;
}

插入操作

向表L中第i个位置插入元素e

Status ListInsert_Sq(SqList& L, int i, ElemType e) {
	if (i<1 || i>L.length + 1) return ERROR;//判断插入位置不合法
	if (L.length == L.listsize) {//判断表是否填满
		ElemType* nnew;
		nnew = (ElemType*)malloc(sizeof(ElemType) * (L.listsize + LISTINCREMENT));
		if (!nnew) exit(OVERFLOW);//分配空间不成功
		L.elems = nnew;
		L.listsize += LISTINCREMENT;
	}
	//第i个位置 标号是i-1
	ElemType* p = L.elems + i - 1;//第i个位置
	ElemType* q = L.elems + L.length - 1;//最后一个元素的位置
	while (q >= p) { *(q + 1) = *q; q--; }
	*p = e;
	L.length++;
	return OK;
}

存取操作

直接取表L中第i个元素 并存在e中带回

void GetElem_Sq(SqList L, int i, ElemType& e) {//传e的引用
	e = *(L.elems + i - 1);
}

返回顺序表长度

直接将成员变量L.length返回即可

int ListLength_Sq(SqList L) {
	return L.length;
}

删除操作

删除表L中第i个元素 用e返回其值

int ListDelete_Sq(SqList& L, int i,ElemType &e) {
	if (i<1 || i>L.length) return ERROR;
	//删除第i个元素 下标i-1
	ElemType* p = L.elems + i - 1;//*p=&L.elems[i-1];
	ElemType* q = L.elems + L.length - 1;//最后一个元素
	e = *p;
	while (p < q) { *p  = * (p + 1); p++; }
	L.length--;
	return OK;
}

合并两个有序顺序表

设两个指针分别遍历有序La Lb,将较小者插入到Lc的末尾

Status MergeList_Sq(SqList& La, SqList& Lb, SqList& Lc) {
	if (Lc.listsize < (Lc.length + La.length + Lb.length)) {
		ElemType* nnew;
		nnew = (ElemType*)malloc(sizeof(ElemType) * (Lc.length + La.length + Lb.length));
		if (!nnew) exit(OVERFLOW);
		Lc.elems = nnew;
		Lc.listsize = Lc.length + La.length + Lb.length;
	}
	ElemType* p, * q, * i;
	i = Lc.elems + Lc.length;//最后一个元素的下一个
	p = La.elems;
	q = Lb.elems;
	while (p < (La.elems + La.length) && q < (Lb.elems + Lb.length)) {
		if (*p < *q) *i = *p, p++, i++;
		else *i = *q, q++, i++;
	}
	while (p < (La.elems + La.length)) {
		*i = *p;
		p++; i++;
	}
	while (q < (Lb.elems + Lb.length)) {
		*i = *q;
		q++; i++;
	}
	Lc.length = Lc.length + La.length + Lb.length;;
	return OK;
}

所有代码

#include <iostream>

using namespace std;
//顺序表存储结构定义
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR -1
typedef int ElemType;
typedef int Status;
//顺序表能访问随机 插入删除需大量移动数据 与数组类似 
//结构体
typedef struct {
	ElemType* elems;//存储空间基地址 顺序表要先开辟空间
	int length;     //表中元素个数
	int listsize;   //表容量大小
}SqList;			
//空间分配/表长与容量初始化
Status InitList_Sq(SqList& L) {
	L.elems = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
	if (!L.elems) exit(OVERFLOW);//若分配空间失败
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	return OK;
}
//释放L.elem/表长与容量赋0
void DestroyList_Sq(SqList& L) {
	free(L.elems);
	L.length = 0;
	L.listsize = 0;
}
//向表L中第i个位置插入元素e
Status ListInsert_Sq(SqList& L, int i, ElemType e) {
	if (i<1 || i>L.length + 1) return ERROR;//判断插入位置不合法
	if (L.length == L.listsize) {//判断表是否填满
		ElemType* nnew;
		nnew = (ElemType*)malloc(sizeof(ElemType) * (L.listsize + LISTINCREMENT));
		if (!nnew) exit(OVERFLOW);//分配空间不成功
		L.elems = nnew;
		L.listsize += LISTINCREMENT;
	}
	//第i个位置 标号是i-1
	ElemType* p = L.elems + i - 1;//第i个位置
	ElemType* q = L.elems + L.length - 1;//最后一个元素的位置
	while (q >= p) { *(q + 1) = *q; q--; }
	*p = e;
	L.length++;
	return OK;
}
//直接取表L中第i个元素 并存在e中带回
void GetElem_Sq(SqList L, int i, ElemType& e) {//传e的引用
	e = *(L.elems + i - 1);
}
//直接将成员变量L.length返回即可
int ListLength_Sq(SqList L) {
	return L.length;
}
//删除表L中第i个元素 用e返回其值
int ListDelete_Sq(SqList& L, int i,ElemType &e) {
	if (i<1 || i>L.length) return ERROR;
	//删除第i个元素 下标i-1
	ElemType* p = L.elems + i - 1;//*p=&L.elems[i-1];
	ElemType* q = L.elems + L.length - 1;//最后一个元素
	e = *p;
	while (p < q) { *p  = * (p + 1); p++; }
	L.length--;
	return OK;
}
//设两个指针分别遍历有序La Lb,将较小者插入到Lc的末尾
Status MergeList_Sq(SqList& La, SqList& Lb, SqList& Lc) {
	if (Lc.listsize < (Lc.length + La.length + Lb.length)) {
		ElemType* nnew;
		nnew = (ElemType*)malloc(sizeof(ElemType) * (Lc.length + La.length + Lb.length));
		if (!nnew) exit(OVERFLOW);
		Lc.elems = nnew;
		Lc.listsize = Lc.length + La.length + Lb.length;
	}
	ElemType* p, * q, * i;
	i = Lc.elems + Lc.length;//最后一个元素的下一个
	p = La.elems;
	q = Lb.elems;
	while (p < (La.elems + La.length) && q < (Lb.elems + Lb.length)) {
		if (*p < *q) *i = *p, p++, i++;
		else *i = *q, q++, i++;
	}
	while (p < (La.elems + La.length)) {
		*i = *p;
		p++; i++;
	}
	while (q < (Lb.elems + Lb.length)) {
		*i = *q;
		q++; i++;
	}
	Lc.length = Lc.length + La.length + Lb.length;;
	return OK;
}
int main()
{
	SqList a, b, c;
	InitList_Sq(a);
	InitList_Sq(b);
	InitList_Sq(c);
	int t;
	for (int i = 0; i < 5; i++) cin >> t, ListInsert_Sq(a, i+1, t);
	for (int i = 0; i < 5; i++) cin >> t, ListInsert_Sq(b, i+1, t);
	if (MergeList_Sq(a, b, c)) {
		ElemType* p;
		for (int i = 0; i < c.length; i++) cout << " " << c.elems[i];
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值