静态顺序表

文章展示了如何使用C++编程实现一个顺序表结构,包括初始化顺序表、在指定位置插入元素、删除元素的功能。代码中定义了结构体SequentialListPtr,包含了数组和实际长度,并提供了对应的函数接口来操作顺序表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <cstdlib>
using namespace std;

#define max_length 10

/*
* 创建一个结构体,结构体内包含一个整形数组和其实际使用的长度
*/

typedef struct Sequentiallist {
	int actualLength;

	int data[max_length];
}SequentialListPtr;

//打印顺序表
void outputList(SequentialListPtr *paralist)
{
	if (paralist->actualLength == 0) return;
	for (int i = 0; i < paralist->actualLength; i++)
		printf("%d ",paralist->data[i]);
	printf("\r\n");
}

//打印顺序表相关元素的地址
void  outputMemory(SequentialListPtr *paraListPtr)
{
	cout<<"The address of the structure:"<<paraListPtr<<endl;
	cout<<"The address of actualLength:"<< &paraListPtr->actualLength<<endl;
	cout<<"The address of data:"<<paraListPtr->data<<endl;
	cout<<"The address of actual data:"<< &paraListPtr->data[0]<<endl;
	cout << "The address of second data:" << &paraListPtr->data[1] << endl;
}

//初始化顺序表,函数的返回类型是SequentialListPtr
SequentialListPtr *sequentialListInit(int paraData[], int paraLength)
{
	SequentialListPtr *resultPtr = new SequentialListPtr;
	for (int i = 0; i < paraLength; i++)
	{
		resultPtr->data[i] = paraData[i];
		cout << paraData[i];
	}
	resultPtr->actualLength = paraLength;

	return resultPtr;
}

//顺序表的插入,传入的第一个参数必须是指针类型,才能对该顺序表进行修改。
//paraPosition是需要插入的位置,如0,1,2···。
//paravValue是需要插入元素的值
void sequentialListInsert(SequentialListPtr* paraListPtr, int paraPosition, int paraValue)
{
	if (paraListPtr->actualLength < 0)
	{
		printf("Cannot insert element:negative position unsupported.");
		return;
	}

	if (paraListPtr->actualLength >= max_length)
	{
		printf("Cannot insert element: list full.\r\n");
		return;
	}

	if (paraPosition > paraListPtr->actualLength)
	{
		printf("Cannot insert element:the position %d is bigger than the list length %d.\n", paraPosition, paraListPtr->actualLength);
		return;
	}

	for (int i = paraListPtr->actualLength; i > paraPosition; i--)
	{
		paraListPtr->data[i] = paraListPtr->data[i - 1];
	}

	paraListPtr->data[paraPosition] = paraValue;

	paraListPtr->actualLength++;
}

//插入函数的测试函数
void sequentialInsertTest()
{
	int i = 0;
	int tempArray[5] = { 5,7,6,2,3 };
	
	printf("---- sequentialInsertTest begins. ----\r\n");

	//初始化顺序表
	SequentialListPtr* tempList = sequentialListInit(tempArray, 5);
	printf("After initialization,the list is: ");
	outputList(tempList);

	//把8插入到第一个位置
	printf("now insert to the first,the list is: ");
	sequentialListInsert(tempList, 0, 8);
	outputList(tempList);

	printf("Now insert to the last, the list is: ");
	sequentialListInsert(tempList, 6, 9);
	outputList(tempList);

	//把9 插入到末尾
	printf("Now insert beyond the tail. \r\n");
	sequentialListInsert(tempList, 8, 9);
	printf("The list is:");
	outputList(tempList);

	// 溢出测试
	for (i = 0; i < 5; i++) {
		printf("Inserting %d.\r\n", (i + 10));
		sequentialListInsert(tempList, 0, (i + 10));
		outputList(tempList);
	}

	printf("---- sequentialInsertTest ends. ----\r\n");
}

//从顺序表中删除元素,函数返回类型是整数类型,返回被删除元素的值
//第一个参数必须是指针类型,以使能对顺序表进行修改
//paraPosition是要删除元素的地址
int sequentialListDelete(SequentialListPtr* paraListPtr, int paraPosition)
{
	//检查位置是否合法
	if (paraPosition < 0)
	{
		printf("Invalid position:%d.\r\n", paraPosition);
		return-1;
	}

	if (paraPosition >= paraListPtr->actualLength)
	{
		printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
		return -1;
	}
	
	//保存结果并开始移动需要改变位置的元素
	int resultValue = paraListPtr->data[paraPosition];
	for (int i = paraPosition; i < paraListPtr->actualLength; i++)
	{
		paraListPtr->data[i] = paraListPtr->data[i + 1];
	}

	paraListPtr->actualLength--;

	return resultValue;
}

//删除函数的测试函数
void sequentialDeleteTest()
{
	int tempArray[5] = { 5,8,6,4,3 };
	printf("--- sequentiaDeleteTest begins. ---\r\n");

	SequentialListPtr* tempList = sequentialListInit(tempArray, 5);
	printf("After initialization,the list is: ");
	outputList(tempList);

	printf("Now delete the last, the list is: ");
	sequentialListDelete(tempList, 3);
	outputList(tempList);

	// 删除第二个元素
	printf("Now delete the second, the list is: ");
	sequentialListDelete(tempList, 1);
	outputList(tempList);

	// 删除第五个元素
	printf("Now delete the 5th, the list is: ");
	sequentialListDelete(tempList, 5);
	outputList(tempList);

	// 不合理位置的检测
	printf("Now delete the (-6)th, the list is: ");
	sequentialListDelete(tempList, -6);
	outputList(tempList);

	printf("---- sequentialDeleteTest ends. ----\r\n");

	outputMemory(tempList);
}

//查找元素的位置,函数类型是整形,返回该元素在顺序表中的位置
int locateElement(SequentialListPtr *paraListPtr, int paraValue)
{
	for (int i = 0; i < paraListPtr->actualLength; i++)
	{
		if (paraListPtr->data[i] == paraValue)
			return i;
	}
}

//给定顺序表中的位置,返回该位置储存的值,返回类型为整形
int getElement(SequentialListPtr* paraListPtr, int paraPosition)
{
	if (paraPosition < 0)
	{
		printf("Invalid position : % d.\r\n", paraPosition);
		return -1;
	}

	if (paraPosition >= paraListPtr->actualLength)
	{
		printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
		return -1;
	}
	
	return paraListPtr->data[paraPosition];
}

void main()
{
	sequentialInsertTest();
	sequentialDeleteTest();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值