数据结构我好爱:01顺序表的基本操作

本文通过实例详细讲解了数据结构中顺序表的概念,包括顺序表的定义、结构体实现及基本操作如创建、初始化、插入、删除、清空等。代码示例展示了如何使用C语言实现顺序表,并提供了完整的顺序表操作函数。顺序表是一种简单的数据结构,能有效实现人脑与计算机之间的信息沟通。
摘要由CSDN通过智能技术生成

我的凡老师:

                数据结构原来是如此简单!

                数据结构可以实现人脑与电脑的无缝沟。

                No code no bb,show me the code

顺序表:顾名思义,用一组地址连续的存储单元来存放数据元素,因此我们可以通过一个数据的地址来找出其他数据的地址。

在这里插入图片描述

有抽象公式:Loc(\alpha _{i}) = Loc(\alpha _{i-1}) +l

可具体概括为Loc(\alpha _{m}) = Loc(\alpha _{n})+(m-n)*sizeof(type(a))

其中最简单常见的线性表为:数组

通常情况下:我们描述一个物体的属性,往往不只从一个维度出发,比如描述一间教室:

描述它的位置坐标,上课人数,教师姓名...

typedef struct{
    int x,y;//表示教室坐标(x,y)
    int people;//表示学生人数
    string teacher;//教师姓名
}Aclassroom;

由于涉及的数据类型较多,我们会使用struct结构体类型。

经典入门典例:含数组结构体表的一系列基本操作(创建、初始化、插入、删除、清空...)

1.创建需要的结构体:

typedef struct {
	int data[MAX_LENGTH];
	int len;
}Sql,*SqlPtr;

2.初始化:

SqlPtr SqlInit(int *array,int len) {
	if (len > MAX_LENGTH) {
		printf("error for too long. \r\n");
	}
	SqlPtr L=(SqlPtr)malloc(sizeof(Sql));
	for (int i = 0; i < len; i++) {
		L->data[i] = array[i];
	}
	L->len = len;
	return L;
}

3.打印输出列表元素:

void OutputSql(SqlPtr L) {
	for (int i = 0; i < L->len; i++) {
		printf("%3d ", L->data[i]);
	}
	putchar('\n');
}

4.插入操作:优化到前的挪动空间才可以确保原数据不被破坏。

void SqlInsert(SqlPtr L,int p,int value) {
	if (p > L->len || p < 0) {
		printf("position error,No changed:");
		return;
	}
	for (int i = L->len; i > p; i--) {
		L->data[i] = L->data[i - 1];
	}
	L->data[p] = value;
	L->len++;
}

5.删除操作:

void SqlDelete(SqlPtr L, int p) {
	if (p >= L->len || p < 0) {
		printf("position error ,out of the area:0-%d\r\n", L->len);
		return;
	}
	for (int i = p; i < L->len; i++) {
		L->data[i] = L->data[i + 1];
	}
	L->len--;
}

5.列表清空:清空只需要将长度重置为0即可,不需要考虑原有数据free的问题,因为在下次使用时,地址没有变化,但是可以产生覆盖,故可理解为又创建了一个新表。

void clearSql(SqlPtr L) {
    L->len = 0;
}

6.列表元素的定位与查找:普普通通的数组遍历,可以考虑使用二分法优化代码。

int locateElement(SqlPtr L, int value) {
	for (int i = 0; i < L->len; i++) {
		if (L->data[i] == value) {
			return i;
		}
	}

	return -1;
}

int getElement(SqlPtr L, int p) {
	if (p < 0) {
		printf("Invalid position: %d.\r\n", p);
		return -1;
	}

	if (p >= L->len) {
		printf("Cannot delete: the position %d is beyond the length %d.\r\n", p, L->len);
		return -1;
	}

	return L->data[p];
}

运行截图:perfect!!!

 

文末为凡老师完整代码:https://blog.csdn.net/minfanphd/article/details/117277239

#include<stdio.h>
#include<malloc.h>

#define LIST_MAX_LENGTH 10

/**
*Linear list of integers. The key is data.
*/
typedef struct SequentialList {
	int actualLength;

	int data[LIST_MAX_LENGTH];//the maxium length is fixed.
}SequentialList, * SequentialListPtr;


//Output the list

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

//Output the memory for the list.

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

SequentialListPtr sequentialListInit(int* paraData, int paraLength) {
	SequentialListPtr resultPtr = (SequentialList*)malloc(sizeof(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) {
	if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
		printf("can not insert:list is full.\r\n");
		return;
	}
	if (paraPosition > paraListPtr->actualLength || paraPosition < 0) {
		printf("can not insert:position error.\r\n");
		return;
	}

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

}

void sequentialInsertTest() {
	int i;
	int tempArray[5] = { 3, 5, 2, 7, 4 };

	printf("---- sequentialInsertTest begins. ----\r\n");

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

	// Insert to the first.
	printf("Now insert to the first, the list is: ");
	sequentialListInsert(tempList, 0, 8);
	outputList(tempList);

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

	// Insert beyond the tail.
	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);
	}//Of for i

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


int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
	if (paraPosition < 0) {
		printf("Invalid position: %d.\r\n", paraPosition);
		return -1;
	}//Of if
	if (paraPosition >= paraListPtr->actualLength) {
		printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
		return -1;
	}//Of if

	int resultValue = paraListPtr->data[paraPosition];
	for (int i = paraPosition; i < paraListPtr->actualLength; i++) {
		paraListPtr->data[i] = paraListPtr->data[i + 1];
	}//Of for i

	// Step 3. Update the length.
	paraListPtr->actualLength--;

	// Step 4. Return the value.
	return resultValue;
}

void sequentialDeleteTest() {
	int tempArray[5] = { 3, 5, 2, 7, 4 };

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

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

	// Delete the first.
	printf("Now delete the first, the list is: ");
	sequentialListDelete(tempList, 0);
	outputList(tempList);

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

	// Delete the second.
	printf("Now delete the second, the list is: ");
	sequentialListDelete(tempList, 1);
	outputList(tempList);

	// Delete the second.
	printf("Now delete the 5th, the list is: ");
	sequentialListDelete(tempList, 5);
	outputList(tempList);

	// Delete the second.
	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;
		}// Of if
	}//Of for i

	return -1;
}// Of locateElement

/**
 * Get an element in the list.
 * @param paraListPtr The pointer to the list.
 * @param paraPosition The given position.
 * @return The position of the value, or  -1 indicating not exists
 */
int getElement(SequentialListPtr paraListPtr, int paraPosition) {
	// Step 1. Position check.
	if (paraPosition < 0) {
		printf("Invalid position: %d.\r\n", paraPosition);
		return -1;
	}//Of if

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

	return paraListPtr->data[paraPosition];
}// Of locateElement

/**
 * Clear elements in the list.
 * @param paraListPtr The pointer to the list.
 * @return The position of the value, or  -1 indicating not exists
 */
void clearList(SequentialListPtr paraListPtr) {
	paraListPtr->actualLength = 0;
}// Of clearList

/**
 The entrance.
 */
void main() {
	sequentialInsertTest();
	sequentialDeleteTest();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值