数据结构之顺序表(部分参考C博客—文逗,整理和参考严蔚敏版数据结构)

本文介绍了一个使用C++实现的顺序表的基本操作,包括初始化、创建、销毁、清空、判断是否为空、获取长度、反转、插入、删除等,并提供了完整的代码示例。

#include<iostream>
#include<malloc.h>
using namespace std;
#define LISTSIZE 100
#define SIZEADD 10
typedef int ElemType;
typedef struct{
	ElemType *pBase;
	int Length;
	int Listsize;
}SqList;
void InitList(SqList &L)
{
	L.pBase = new ElemType[LISTSIZE];
	L.Length = 0;
	L.Listsize = LISTSIZE;
}
void CreateList(SqList &L, int len)
{
	cout << "请输入" << len << "个元素的值:" << endl;
	for (int i = 0; i<len; i++)
		cin >> L.pBase[i];
	L.Length = len;
}
void DestroyList(SqList &L)
{
	delete[]L.pBase;
	L.pBase = NULL;
	L.Length = 0;
	L.Listsize = 0;
	return;
}
void ClearList(SqList &L)
{
	L.Length = 0;
	return;
}
bool ListEmpty(SqList L)
{
	if (L.Length)
		return false;
	else
		return true;
}
int ListLength(SqList L)
{
	return L.Length;
}
void ListReverse(SqList &L)
{
	ElemType tem;
	if (ListEmpty(L))
		return;
	for (int i = 0, j = L.Length - 1; i<j; ++i, --j)
	{
		tem = L.pBase[i]; L.pBase[i] = L.pBase[j]; L.pBase[j] = tem;
	}
	return;
}
bool ListInsert(SqList &L, int pos, ElemType e)
{
	if (pos<1 || pos>L.Length + 1) return false;
	if (L.Length >= L.Listsize)
	{
		ElemType *pNBase = (ElemType*)realloc(L.pBase, (L.Listsize + SIZEADD)*sizeof(ElemType));
		L.pBase = pNBase;
		L.Listsize += SIZEADD;
	}
	ElemType *q = &(L.pBase[pos - 1]);//指向当前
	ElemType *p = &(L.pBase[L.Length - 1]);//指向尾巴
	while (p >= q)//从尾巴至当前位置的值往后移动,当前插入位置不动
	{
		*(p + 1) = *(p);
		--p;
	}
	*q = e;
	++L.Length;
	return true;
}
bool ListDelete(SqList &L, int pos, ElemType &e)
{
	if (pos<1 || pos>L.Length)
		return false;
	ElemType *q = L.pBase + pos - 1;
	ElemType *p = L.pBase + L.Length - 1;
	e = *q;
	for (++q; q <= p; ++q)
		*(q - 1) = *q;
	--L.Length;
	return true;
}
bool DeletePart(SqList &L, int pos, int count)//删除从第i个元素起的count个元素
{
	int i;
	if (pos < 1 || count<1 || pos + count - 1>L.Length)
		return false;
	for (i = 0; pos + i - 1 < L.Length - count; i++)//循环条件?
		L.pBase[pos + i - 1] = L.pBase[pos + i + count - 1];
	L.Length -= count;
	return true;
}
int LocateElem(SqList L, ElemType e)//获取元素的位置
{
	int pos = 1;
	while (pos <= L.Length&&L.pBase[pos - 1] != e)
	{
		pos++;
	}
	return pos;//注意当pos>L.Length时则是获取失败
}
bool GetElem(SqList L, int pos, ElemType &e)
{
	if (pos<1 || pos>L.Length)
		return false;
	e = L.pBase[pos - 1];
	return true;
}
bool PriorElem(SqList L, ElemType cur, ElemType &pre)
{
	int pos;
	pos = LocateElem(L, cur);            //先查询是否存在cur
	if (pos > L.Length || pos == 1)
		return false;
	else                                 //存在
	{
		pre = L.pBase[pos - 2];
		return true;
	}
}
bool NextElem(SqList L, ElemType cur, ElemType &next)
{
	int pos;
	pos = LocateElem(L, cur);
	if (pos >= L.Length)
		return false;
	else
	{
		next = L.pBase[pos];
		return true;
	}
}
int ListCompare(SqList LA, SqList LB)
{
	int i = 0;
	while (i < LA.Length&&i < LB.Length)
	{
		if (LA.pBase[i] == LB.pBase[i])
			i++;
		else if (LA.pBase[i] < LB.pBase[i])
			return -1;
		else
			return 1;
	}
	if (i == LA.Length&&i == LB.Length)
		return 0;
	else if (i == LA.Length&&i < LB.Length)
		return -1;
	else
		return 1;

}
void ListMerge(SqList LA, SqList LB, SqList &LC)//归并两个值非递减的顺序表为一个值非递减的顺序表
{
	ElemType *pA, *pB, *pC;
	ElemType *pALast, *pBLast;
	pA = LA.pBase;
	pB = LB.pBase;
	pC = LC.pBase;
	LC.Listsize = LC.Length = LA.Length + LB.Length;
	pALast = LA.pBase + LA.Length - 1;
	pBLast = LB.pBase + LB.Length - 1;
	while (pA <= pALast&&pB <= pBLast)
	{
		if (*pA <= *pB)
			*pC++ = *pA++;
		else
			*pC++ = *pB++;
	}
	while (pA <= pALast)
		*pC++ = *pA++;
	while (pB<=pBLast)
		*pC++ = *pB++;
}
void ListTraverse(SqList L)
{
	for (int i = 0; i < L.Length; i++)
	{
		cout << L.pBase[i] << " ";
	}
	cout << endl;
	return;
}
int main(void)
{
	SqList LA, LB, LC;
	InitList(LA);
	InitList(LB);
	InitList(LC);
	CreateList(LA, 4);
	CreateList(LB, 5);
	ListMerge(LA, LB, LC);
	ListTraverse(LA);
	ListTraverse(LB);
	ListTraverse(LC);
	
	/*
	ListInsert(L, 7, 7);
	ListTraverse(L);
	ListDelete(L, 7, e);
	cout << e << endl;
	ListTraverse(L);
	GetElem(L, 2, e);
	cout << e << endl;
	i = LocateElem(L, 2);
	if (i > L.Length)
	cout << "元素位置获取失败!" << endl;
	else
	cout << i << endl;
	PriorElem(L, 2, e);
	cout << e << endl;
	NextElem(L, 2, e);
	cout << e << endl;
	DeletePart(L, 2, 3);
	ListTraverse(L);
	*/
	return(0);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值