数据结构c语言1:顺序表

数据结构c语言1:顺序表,第1张

函数实现
typedef struct SequentialList;//创建顺序表
void outputList(SequentialListPtr paraList);//打印顺序表 
void outputMemory(SequentialListPtr paraListPtr);//打印数据地址
SequentialListPtr sequentialListInit(int paraData[], int paraLength);//顺序表的初始化
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue);//顺序表的插入
void sequentialInsertTest();//测试插入功能
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition);//顺序表的删除
void sequentialDeleteTest();//测试删除功能
int locateElement(SequentialListPtr paraListPtr, int paraValue);//顺序表的查找
int getElement(SequentialListPtr paraListPtr, int paraPosition);//顺序表的取值
void clearList(SequentialListPtr paraListPtr);//清空顺序表
顺序表的查找
int locateElement(SequentialListPtr paraListPtr, int paraValue){
	for (int i = 0; i < paraListPtr->actualLength; i++) {
		if (paraListPtr->data[i] == paraValue) {
			return i;
		}
	}
	return -1;
}
顺序表的取值
int getElement(SequentialListPtr paraListPtr, int paraPosition) {
    if (paraPosition < 0) {
        printf("错误位置: %d.\r\n", paraPosition);
        return -1;
    }
    
    if (paraPosition >= paraListPtr->actualLength) {
        printf("不能删除: 位置 %d 超过线性表长度 %d.\r\n", paraPosition, paraListPtr->actualLength);
        return -1;
    }
	return paraListPtr->data[paraPosition];
}
顺序表的清空
void clearList(SequentialListPtr paraListPtr){
	paraListPtr->actualLength = 0;
}
代码
#include 
#include 

#define LIST_MAX_LENGTH 10

//创建顺序表
typedef struct SequentialList{
	int actualLength;

    int data[LIST_MAX_LENGTH];
}*SequentialListPtr;

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

//打印数据地址
void outputMemory(SequentialListPtr paraListPtr){
	printf("The address of the structure: %ld\r\n", paraListPtr);
	printf("The address of actualLength: %ld\r\n", &paraListPtr->actualLength);
	printf("The address of data: %ld\r\n", &paraListPtr->data);
    printf("The address of actual data: %ld\r\n", &paraListPtr->data[0]);
    printf("The address of second data: %ld\r\n", &paraListPtr->data[1]);
}

//顺序表的初始化
SequentialListPtr sequentialListInit(int paraData[], int paraLength){
	SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct SequentialList));
	for (int i = 0; i < paraLength; i ++) {
		resultPtr->data[i] = paraData[i];
	}
	resultPtr->actualLength = paraLength;

	return resultPtr;
}

//顺序表的插入
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue){
	//1、检查空间
	if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
        printf("Cannot insert element: list full.\r\n");
        return;
    }
	
	//2、检查位置
	if (paraPosition < 0) {
        printf("Cannot insert element: negative position unsupported.");
        return;
    }
    if (paraPosition > paraListPtr->actualLength) {
        printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
        return;
    }

    //3、移动数据
    for (int i = paraListPtr->actualLength; i > paraPosition; i --) {
        paraListPtr->data[i] = paraListPtr->data[i - 1];
    }

	//4、插入
	paraListPtr->data[paraPosition] = paraValue;

	//5、更新长度
	paraListPtr->actualLength ++;
}

//测试插入功能
void sequentialInsertTest() {
	int i;
	int tempArray[5] = {3, 5, 2, 7, 4};

	printf("---- 进行顺序插入测试 ----\r\n");

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

	// 首位插入 
    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);

	// 尾部之外插入 
    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("---- 顺序插入测试结束 ----\r\n\n");
}

//顺序表的删除
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
    // 1、检查位置 
    if (paraPosition < 0) {
        printf("错误的位置: %d.\r\n", paraPosition);
        return -1;
    }
	if (paraPosition >= paraListPtr->actualLength) {
		printf("不能删除:位置 %d 超出线性表长度 %d\r\n", paraPosition, paraListPtr->actualLength);
    	return -1;
	}

	// 2.移动数据 
	int resultValue = paraListPtr->data[paraPosition];
	for (int i = paraPosition; i < paraListPtr->actualLength; i ++) {
        paraListPtr->data[i] = paraListPtr->data[i + 1];
    }

	// 3.更新长度 
    paraListPtr->actualLength --;

	// 4.返回值 
	return resultValue;
}

//测试删除功能
void sequentialDeleteTest() {
	int tempArray[5] = {3, 5, 2, 7, 4};

	printf("---- 进行数据删除测试 ----\r\n");

	// 初始化的线性表 
    SequentialListPtr tempList = sequentialListInit(tempArray, 5);
    printf("初始化的线性表为: ");
	outputList(tempList);

	// 删除首位数据
    printf("删除首位数据: ");
	sequentialListDelete(tempList, 0);
	outputList(tempList);

	// 删除尾部数据
    printf("删除尾部数据: ");
	sequentialListDelete(tempList, 3);
	outputList(tempList);

	// 删除第二个数据
    printf("删除第二位数据: ");
	sequentialListDelete(tempList, 1);
	outputList(tempList);

	// 删除第-6位数据
    printf("删除第-6位数据: ");
	sequentialListDelete(tempList, -6);
	outputList(tempList);
	
	printf("---- 删除数据测试结束 ----\r\n");
 
	outputMemory(tempList);

}

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/756764.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-30
下一篇 2022-04-30

发表评论

登录后才能评论

评论列表(0条)

保存