数据结构 2.1:顺序表

文章展示了如何使用C语言实现顺序列表的数据结构,包括初始化、插入元素、删除元素等操作。在插入和删除过程中,作者注意到了边界条件检查和内存管理,并通过测试函数进行了功能验证。此外,作者强调了代码规范和注释的重要性,以及在编程中避免数组越界和及时释放内存的问题。
摘要由CSDN通过智能技术生成

1.代码

先上代码:

#define _CRT_SECURE_NO_WARNINGS 1

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

#define LIST_MAX_LENGTH 10

typedef struct SequentialList {
	int actualLength;

	int data[LIST_MAX_LENGTH];
}*SequentialListPtr;

int i;
//Output SequentialList

void OutputSequentialList(SequentialListPtr paraListPtr) {
	for (i = 0; i < paraListPtr->actualLength; i++) {
		printf("%d ", paraListPtr->data[i]);
	}
	printf("\r\n");
}

//Output the memeory for the list.

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

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]);
}

//*Initialize the SequentialList

SequentialListPtr sequentiallistInit(int paraData[], int paraLength) {
	SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct SequentialList));
	for (i = 0; i < paraLength; i++) {
		resultPtr->data[i] = paraData[i];
	}
	resultPtr->actualLength = paraLength;
	return resultPtr;
}



//insert a element to the sequentialList
//@paraListPtr: the pointer to the list
//@paraPosition: the posiotion of the insert
//@paraValue: the value to be inserted

void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
	
	//step 1.Spece check
	
	if (paraListPtr->actualLength >= LIST_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.\r\n", paraPosition, paraListPtr->actualLength);
		return;
	}
	if (paraPosition < 0) {
		printf("Cannot insert element: negative position unsupported.");
		return;
	}

	//step 2.Move the remaining part
	
	for (i = paraListPtr->actualLength; i > paraPosition; i--) {
		paraListPtr->data[i] = paraListPtr->data[i - 1];
	}
	
	//step 3.Insert
	
	paraListPtr->data[paraPosition] = paraValue;

	//step 4.Update the actualLength
	
	paraListPtr->actualLength++;
}

//test the Insert function;

void sequentialListInsertTest() {

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

	//1.Initialize

	int tempArray[5] = { 3, 5, 2, 7, 4 };
	SequentialListPtr tempList = sequentiallistInit(tempArray, 5);

	//2.Insert to the first

	printf("Now insert to the first, the list is: ");
	sequentialListInsert(tempList, 0, 8);
	OutputSequentialList(tempList);


	//3.Insert to the last

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

	//4.Insert beyond the tail.
	printf("Now insert beyond the tail. \r\n");
	sequentialListInsert(tempList, 8, 9);
	printf("The list is:");
	OutputSequentialList(tempList);

	//5.Insert to position 3.
	int a;
	for (a = 0; a < 5; a++) {
		printf("Inserting %d.\r\n", (a + 10));
		sequentialListInsert(tempList, 0, (a + 10));
		OutputSequentialList(tempList);
	}
	printf("---- sequentialListInsertTest ends. ----\r\n");
}


   /* Delete an element from a sequential linear list.
   * @param paraListPtr The pointer to the list.
   * @param paraPosition The position, e.g., 0 stands for inserting at the first position.
   * @return The deleted value.
   */

int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {

	int resultValue=paraListPtr->data[paraPosition];

	// Step 1. Position check.

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

	// Step 2. Move the remaining part.
	for (i = paraPosition; i <=(paraListPtr->actualLength) ; i++) {
		paraListPtr->data[i] = paraListPtr->data[i + 1];
	}

	// Step 3. Update the length.

	paraListPtr->actualLength--;

	// Step 4. Return the value.

	return resultValue;
}

/**
 * Test the delete function.
 */

void sequentialListDeleteTest(){

int tempArray[5] = { 3, 5, 2, 7, 4 };
 printf("----sequentialListDeleteTest begins. ----\r\n");

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

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

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

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

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

  // Delete the Minus six.
  printf("Now delete the (-6)th, the list is: ");
  sequentialListDelete(tempList, -6);
  OutputSequentialList(tempList);

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

  outputMemory(tempList);}

/**
 * Locate an element in the list.
 * @param paraListPtr The pointer to the list.
 * @param paraValue the indicated value.
 * @return The position of the value, or  -1 indicating not exists
 */

int locateElement(SequentialListPtr paraListPtr, int paraValue) {

	for (i = 0; i < paraListPtr->actualLength; i++) {
		if (paraListPtr->data[i] == paraValue) {
			return i;
		}
	}           return -1;
}

/**
 * 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 >= paraListPtr->actualLength) {
		printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
		return -1;
	}
	if (paraPosition < 0) {
		printf("Invalid position: %d.\r\n", paraPosition);
		return -1;
	}
	return paraListPtr->data[paraPosition];
}

/**
 * Clear elements in the list.
 * @param paraListPtr The pointer to the list.
 * @return The position of the value, or  -1 indicating not exists
 */

//*teacher's version*

//void clearList(SequentialListPtr paraListPtr) {
//	paraListPtr->actualLength = 0;
//}// Of clearList


//*my version*
void MyclearList(SequentialListPtr paraListPtr) {
	free(paraListPtr);
	paraListPtr->actualLength = 0;
}


void main() {
	sequentialListInsertTest();
	sequentialListDeleteTest();
}

2.运行结果

---- sequentialListInsertTest begins. ----
Now insert to the first, the list is: 8 3 5 2 7 4
Now insert to the first, the list is: 8 3 5 2 7 4 9
Now insert beyond the tail.
Cannot insert element: the position 8 is bigger than the list length 7.
The list is:8 3 5 2 7 4 9
Inserting 10.
10 8 3 5 2 7 4 9
Inserting 11.
11 10 8 3 5 2 7 4 9
Inserting 12.
12 11 10 8 3 5 2 7 4 9
Inserting 13.
Cannot insert element: list full.
12 11 10 8 3 5 2 7 4 9
Inserting 14.
Cannot insert element: list full.
12 11 10 8 3 5 2 7 4 9
---- sequentialListInsertTest ends. ----
----sequentialListDeleteTest begins. ----
After initialization, the list is: 3 5 2 7 4
Now delete the first, the list is: 5 2 7 4
Now delete the last, the list is: 5 2 7
Now delete the second, the list is: 5 7
Now delete the 5th, the list is: Cannot delete element: the position 5 is beyond the list length 2.
5 7
Now delete the (-6)th, the list is: Invalid position: -6.
5 7
---- sequentialListDeleteTest ends. ----
The address of the structure: 1822989136
The address of actualLength: 1822989136
The address of data: 1822989140
The address of actual data: 1822989140
The address of second data: 1822989144

D:\visual studio\SequentialList.c\x64\Debug\SequentialList.c.exe (进程 7028)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。

3.一些收获

自己敲过一遍后,更加认识到了代码规范性的重要性,要养成写注释的好习惯,变量命名要规范,不然写着写着就忘记自己用的是缩写还是全称,是大写还是小写了,往往会浪费很多的时间,所以要有自己个人写代码的风格。

在这过程中我也发现了一些问题,比如我们规定的LIST_MAX_LENGTH=10,而在函数SequentialListInit (int paraData[ ] , paraLength) 中,我们并未对paraData[ ]的长度paraLength有限制,可能有数组越界的风险;其二就是我们在初始化函数时申请了一块动态内存,应该要在测试结束前释放,所以我就把这个功能加在了闵帆老师的clearList中

//*my version*
void MyclearList(SequentialListPtr paraListPtr) {
	free(paraListPtr);
	paraListPtr->actualLength = 0;
}

最后就是,在写for循环时,最好还是写成这样

 不然前面函数太多,很有可能会把 i 忘记了,导致循环跑不起来

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值