One Day - 顺序表

目录

顺序表处理:

1.定义顺序表

2.按顺序输出顺序表

3.初始化顺序表

4.元素插入

5.元素删除

6.查找(用位置找元素,用元素找位置)

7.顺序表清空

8.求前驱

9.求后继

10.销毁连续线性表

11.判断是否为空

12.总代码:

 


顺序表处理:

顺序表定义:

        顺序表,即线性表的顺序存储,用一组地址连续的存储单元依次存储线性表中的各个元素,使得线性表中在逻辑结构上相邻的两个元素在物理位置上也相邻。

1.定义顺序表

代码实现:

typedef struct SequentialList{
	int actualLength;
	
	int data[LIST_MAX_LENGTH];//The maximum length is fixed.
} *SequentialListPtr;

2.按顺序输出顺序表

代码实现:

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

3.初始化顺序表

代码实现:

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

4.元素插入

思路:

将插入位置及以后的元素往后挪位置,插入该位置 如图。

图解:

代码实现:

void sequentailListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
	// Step1. Space check.
	if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
		printf("Cannot insert element: list full.\n");
		
		return;
	}
	
	//Step2. Position check.
	if (paraPosition > paraListPtr->actualLength) {
		printf("Cannot insert element: the position %d is bigger than the list length %d.\n", paraPosition, paraListPtr->actualLength);
		return;
	} 
	
	//Step3. Move the remaining part.
	for (int i = paraListPtr->actualLength; i > paraPosition; i--) {
		paraListPtr->data[i] = paraListPtr->data[i - 1];
	}//Of for i
	
	//Step4. Insert.
	paraListPtr->data[paraPosition] = paraValue;
	
	//Updata the length.
	paraListPtr->actualLength ++;
}

测试代码:

void sequentialInsertTest(){
	int i;
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	printf("---- sequentialInsertTest begins. ----\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: ");
	sequentailListInsert(tempList, 0, 8);
	outputList(tempList);
	
	//Insert to the last.
	printf("Now insert to the last, the list is: ");
	sequentailListInsert(tempList, 6, 9);
	outputList(tempList);
	
	// Insert beyond the tail.
	printf("Now insert beyond the tail.");
	sequentailListInsert(tempList, 8, 9);
	printf("The list is: ");
	outputList(tempList);
	// Insert to position 3.
	for(i = 0; i < 5; i++) {
		printf("Inserting %d.\n", (i + 10));
		sequentailListInsert(tempList, 0, (i+10));
		printf("After inserting, the list is: ");
		outputList(tempList);
	} 
	printf("---- sequenttialInsertTest ends. ----\n");// Of sequentialInsertTest
}

5.元素删除

思路:把删除位置以后的元素往前移动 如图。

图解:

 代码实现:

int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
	//Step 1.Position check.
	if(paraPosition < 0){
		printf("Invalid positiion : %d.\n", paraPosition);
		return -1;
	}

	if(paraPosition >= paraListPtr->actualLength) {
		printf("Cannot dalete element: the position %d is beyond the list length %d.\n", paraPosition, paraListPtr->actualLength);
		return -1;
	}
	
	//Step2. Move the remaining part.
	int resultValue = paraListPtr->data[paraPosition];
	for (int i = paraPosition; i < paraListPtr->actualLength; i++){
		paraListPtr->data[i] = paraListPtr->data[i + 1];
	}

	//Step3. Updata the length.
	paraListPtr->actualLength --;
	
	//Step4. Return the value.
	return resultValue;
	
}

测试代码:

void sequentialDeleteTest() {
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	printf("---- sequentialDeleteTest begins. ----\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 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, 5);
	outputList(tempList);
	
	//Delete the beyond
	printf("Now delete the(-6)th, the list is: ");
	sequentialListDelete(tempList, -6);
	outputList(tempList);
	
	printf("---- sequentialDeleteTest ends. ----\n");
	
	outputMemory(tempList);
	 
}

6.查找(用位置找元素,用元素找位置)

代码实现:

int MyLocateElement(SequentialListPtr paraListPtr, int paraValue) {
 	int target = paraValue;
 	for(int i = 0; i < paraListPtr->actualLength; i++){
 		if (paraListPtr->data[i] == target) {
 			return i;
		 }
	}
	return -1;
	 
 } 
int MyGetElement(SequentialListPtr paraListPtr, int paraPosition) {
	int target = paraPosition;
	if( target >= paraListPtr->actualLength || target < 0){
		printf("no the position\n");
		return -1;
	}
	for(int i = 0; i < paraListPtr->actualLength; i++){
		if(i == target) {
			return paraListPtr->data[i];
		}
	}
}

测试代码:

void MyLocateElementTest(){
	printf("---- MyLocateElementTest begin ----\n");
	
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	//Initialize.
	SequentialListPtr tempList = sequentialListInit(tempArray, 5);
	printf("After initialization, the list is: ");
	outputList(tempList);
	
	//value in the SequentialList
	printf("Now value in SequentialList, the position of the value: %d \n", MyLocateElement(tempList, 5));
	
	//value not in the SequentialList
	printf("Now value not in SequentialList, the position of the value: %d \n", MyLocateElement(tempList, 9));
	
	printf("---- MyLocateElementTest end ----\n");
}
void MyGetElementTest(){
	printf("---- MyGetElementTest begin ----\n");
	
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	//Initialize.
	SequentialListPtr tempList = sequentialListInit(tempArray, 5);
	printf("After initialization, the list is: \n");
	outputList(tempList);
	int tempvalue;
	
	//position out of SequentialList
	tempvalue = MyGetElement(tempList, 9);
	printf("Now position not in SequentialList, the value of the position is : %d\n", tempvalue);
	
	//position in the SequentialList
	tempvalue = MyGetElement(tempList, 1);
	printf("Now position in SequentialList, the value of the position is : %d\n", tempvalue);
	
	printf("---- MyGetElementTest end ----\n");
}

7.顺序表清空

思路:

现在长度设置为0。

代码实现:

void MyClearList(SequentialListPtr paraListPtr) {
	paraListPtr->actualLength = 0;
	return;
}

测试代码:

void MyLocateElementTest(){
	printf("---- MyLocateElementTest begin ----\n");
	
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	//Initialize.
	SequentialListPtr tempList = sequentialListInit(tempArray, 5);
	printf("After initialization, the list is: ");
	outputList(tempList);
	
	//value in the SequentialList
	printf("Now value in SequentialList, the position of the value: %d \n", MyLocateElement(tempList, 5));
	
	//value not in the SequentialList
	printf("Now value not in SequentialList, the position of the value: %d \n", MyLocateElement(tempList, 9));
	
	printf("---- MyLocateElementTest end ----\n");
}

8.求前驱

代码:

int PriorElem(SequentialListPtr paraListPtr, int paraValue){
	int tempKey;
	int tempI;
	for(tempI = 0; tempI < paraListPtr->actualLength; tempI++){
		if(paraValue == paraListPtr->data[tempI] ){
			break;
		}
	}
	if(tempI == 0 || tempI == paraListPtr->actualLength){
		printf("No priorElem, the '-1' not the elem\n");
		return -1;
	}
	else{
		return paraListPtr->data[tempI - 1];
	}
}

测试:

void PriorElemTest(){
	printf("---- PriorElemTest begin ----\n");
	
	//Initialize.
	int tempArray[] = {1, 2, 3, 4};
	SequentialListPtr tempList = sequentialListInit(tempArray, 4);
	outputList(tempList);
	
	//prior
	int priorElem;
	//the prior of 2
	priorElem = PriorElem(tempList, 2);
	printf("the prior of 2:%d\n", priorElem);
	//the prior of 4
	priorElem = PriorElem(tempList, 4);
	printf("the prior of 4:%d\n", priorElem);
	//the prior of 1
	priorElem = PriorElem(tempList, 1);
	printf("the prior of 1:%d\n", priorElem);
	//the prior of 6
	priorElem = PriorElem(tempList, 6);
	printf("the prior of 6:%d\n", priorElem);
	printf("---- PriorElemTest end ----\n");
}

9.求后继

代码:

int NextElem(SequentialListPtr paraListPtr, int paraValue){
	int tempKey;
	int tempI;
	
	for(tempI = 0; tempI < paraListPtr->actualLength; tempI ++){
		if( paraValue == paraListPtr->data[tempI] ){
			break;
		}
	}
	
	if(tempI >= (paraListPtr->actualLength - 1) ){
		printf("N0 nextElem, the '-1' not the elem\n");
		return -1;
	}
	return paraListPtr->data[tempI + 1];
} 

测试:

void NextElemTest(){
	printf("---- NextElemTest begin ----\n");
	
	//Initialize.
	int tempArray[] = {1, 2, 3, 4};
	SequentialListPtr tempList = sequentialListInit(tempArray, 4);
	outputList(tempList);
	
	//prior
	int nextElem;
	//the next of 1
	nextElem = NextElem(tempList, 1);
	printf("the next of 1:%d\n", nextElem);
	//the next of 4
	nextElem = NextElem(tempList, 4);
	printf("the next of 1:%d\n", nextElem);
	//the next of 2
	nextElem = NextElem(tempList, 2);
	printf("the next of 1:%d\n", nextElem);
	//the next of 6
	nextElem = NextElem(tempList, 6);
	printf("the next of 1:%d\n", nextElem);
	printf("---- PriorElemTest end ----\n");
}

10.销毁连续线性表

代码:

void DestroyList(SequentialListPtr &paraList){
	free(paraList);
	paraList = NULL;
}

测试:

void DestroyListTest(){
	printf("---- DestroyListTest begin ----\n");
	
	int tempArray[] = {1, 2, 3, 4};
	
	//Initialize.
	SequentialListPtr tempList = sequentialListInit(tempArray, 4);
	printf("After initialization, The list is: \n");
	outputList(tempList);
	
	//Destroy
	printf("Before destruction, the ptr of list: %d\n", tempList);
	DestroyList(tempList);
	printf("After destruction, the ptr: %d\n", tempList);
	printf("---- DestroyListTest end ----\n");
}

11.判断是否为空

代码:

int ListEmpty( SequentialListPtr &paraList ){
	if( paraList == NULL || paraList->actualLength == 0 ){
		return 0;
	}
	return 1;
}

测试:

void ListEmptyTest(){
	
	printf("---- ListEmptyTest begin ----\n");
	
	//Initialize.
	int tempArray[] = {1, 2, 3, 4};
	SequentialListPtr tempList = sequentialListInit(tempArray, 4);
	outputList(tempList);
	
	//if Empty
	int isEmpty = ListEmpty(tempList);
	
	if(isEmpty){
		printf("After Initialization, the list is no empty\n");
	}
	else{
		printf("After Initialization, the list is empty\n");
	}
	
	MyClearList(tempList);
	isEmpty = ListEmpty(tempList);
	if(isEmpty){
		printf("After Clear, the list is no empty\n");
	}
	else{
		printf("After Clear, the list is empty\n");
	}
	
	tempList->actualLength = 1;// the variate is one
	DestroyList( tempList );
	
	isEmpty = ListEmpty(tempList);
	if(isEmpty){
		printf("After Destrument , the list is no empty\n");
	}
	else{
		printf("After Destrument , the list is empty\n");
	}
	
	printf("---- ListEmptyTest end ----\n");
}

12.总代码:

#include <stdio.h>
#include <malloc.h>
 
#define LIST_MAX_LENGTH 10

/**
 * Linear list of integers. The key to is data.
 */
typedef struct SequentialList{
	int actualLength;
	
	int data[LIST_MAX_LENGTH];//The maximum length is fixed.
} *SequentialListPtr;

/**
 * Output the lsit.
 */
void outputList(SequentialListPtr paraList){
	if(paraList->actualLength == 0){
		printf("no value");
	}
	else {
		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 \n", paraListPtr);
	printf("The address of actualLength: %ld \n", &paraListPtr->actualLength);
	printf("The address of the data: %ld \n", &paraListPtr->data);
	printf("The address of the actual data: %ld \n", &paraListPtr->data[0]);
	printf("The address of the second data: %ld \n", &paraListPtr->data[1]);
}//Of outputMemory

/**
 *Initialize a sequential list. No error checking for this function.
 *@param paraListPtr The pointer to the list. It must be a pointer to change the list.
 *@param paraValues An int array storing all elements;
 */
SequentialListPtr sequentialListInit(int paraData[], int paraLength){
	SequentialListPtr resultPtr = (SequentialList*)malloc(sizeof(SequentialList));
	for(int i = 0; i < paraLength; i++){
		resultPtr->data[i] = paraData[i];
	} //Of for i
	resultPtr->actualLength = paraLength;
	
	return resultPtr;
}//Of sequentialListInit

/**
 * Insert an element into a sequential linear list.
 * @param paraListPtr The pointer to the list. It must be a pointer to change the list.
 * @param paraPosition The position, e.g., 0 stands for inserting at first position.
 * @param paraValue The value to be inserted.
 */
void sequentailListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
	// Step1. Space check.
	if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
		printf("Cannot insert element: list full.\n");
		
		return;
	}//Of if
	
	//Step2. Position check.
	if (paraPosition > paraListPtr->actualLength) {
		printf("Cannot insert element: the position %d is bigger than the list length %d.\n", paraPosition, paraListPtr->actualLength);
		return;
	} //Of if
	
	//Step3. Move the remaining part.
	for (int i = paraListPtr->actualLength; i > paraPosition; i--) {
		paraListPtr->data[i] = paraListPtr->data[i - 1];
	}//Of for i
	
	//Step4. Insert.
	paraListPtr->data[paraPosition] = paraValue;
	
	//Updata the length.
	paraListPtr->actualLength ++;
}//Of sequentialListInsert

/**
 *Test the insert function.
 */
void sequentialInsertTest(){
	int i;
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	printf("---- sequentialInsertTest begins. ----\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: ");
	sequentailListInsert(tempList, 0, 8);
	outputList(tempList);
	
	//Insert to the last.
	printf("Now insert to the last, the list is: ");
	sequentailListInsert(tempList, 6, 9);
	outputList(tempList);
	
	// Insert beyond the tail.
	printf("Now insert beyond the tail.");
	sequentailListInsert(tempList, 8, 9);
	printf("The list is: ");
	outputList(tempList);
	// Insert to position 3.
	for(i = 0; i < 5; i++) {
		printf("Inserting %d.\n", (i + 10));
		sequentailListInsert(tempList, 0, (i+10));
		printf("After inserting, the list is: ");
		outputList(tempList);
	} //Of for i
	printf("---- sequenttialInsertTest ends. ----\n");// Of sequentialInsertTest
}
	/**
	* Delete an element from a sequential linear list.
	* @param paraListPtr The pointer to the list. It must be a pointer to change the list.
	* @param paraPosition The position, e.g., 0 stands for intering at the first position.
	* @return The deleted value.
	*/
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
	//Step 1.Position check.
	if(paraPosition < 0){
		printf("Invalid positiion : %d.\n", paraPosition);
		return -1;
	}//Of if
	
	if(paraPosition >= paraListPtr->actualLength) {
		printf("Cannot dalete element: the position %d is beyond the list length %d.\n", paraPosition, paraListPtr->actualLength);
		return -1;
	}
	
	//Step2. Move the remaining part.
	int resultValue = paraListPtr->data[paraPosition];
	for (int i = paraPosition; i < paraListPtr->actualLength; i++){
		paraListPtr->data[i] = paraListPtr->data[i + 1];
	}//Of for i
	
	//Step3. Updata the length.
	paraListPtr->actualLength --;
	
	//Step4. Return the value.
	return resultValue;
	
}// Of sequentialListDelete

/**
 *Test the delete function.
 */
void sequentialDeleteTest() {
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	printf("---- sequentialDeleteTest begins. ----\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 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, 5);
	outputList(tempList);
	
	//Delete the beyond
	printf("Now delete the(-6)th, the list is: ");
	sequentialListDelete(tempList, -6);
	outputList(tempList);
	
	printf("---- sequentialDeleteTest ends. ----\n");
	
	outputMemory(tempList);
	 
}// Of sequentialDeleteTest

//Mycode
/**
 * Locate an element in the list.
 * @param paraListPtr The pointer to the list.
 * @param paraValue The position of value you want to kown.
 * @return -1 , or the position of value
 */
 int MyLocateElement(SequentialListPtr paraListPtr, int paraValue) {
 	int target = paraValue;
 	for(int i = 0; i < paraListPtr->actualLength; i++){
 		if (paraListPtr->data[i] == target) {
 			return i;
		 }
	}
	return -1;
	 
 } 
 /**
 * Get an element in the list.
 * @param paraListPtr The pointer to the list.
 * @param paraValue The position of value you want to kown.
 * @return -1 and no position , or the value of the position
 */
 
int MyGetElement(SequentialListPtr paraListPtr, int paraPosition) {
	int target = paraPosition;
	if( target >= paraListPtr->actualLength || target < 0){
		printf("no the position\n");
		return -1;
	}
	for(int i = 0; i < paraListPtr->actualLength; i++){
		if(i == target) {
			return paraListPtr->data[i];
		}
	}
}

 /**
 * Clear element in the list.
 * @param paraListPtr The pointer to the list.
 */

void MyClearList(SequentialListPtr paraListPtr) {
	paraListPtr->actualLength = 0;
	return;
}

int PriorElem(SequentialListPtr paraListPtr, int paraValue){
	int tempKey;
	int tempI;
	for(tempI = 0; tempI < paraListPtr->actualLength; tempI++){
		if(paraValue == paraListPtr->data[tempI] ){
			break;
		}
	}
	if(tempI == 0 || tempI == paraListPtr->actualLength){
		printf("No priorElem, the '-1' not the elem\n");
		return -1;
	}
	else{
		return paraListPtr->data[tempI - 1];
	}
}

int NextElem(SequentialListPtr paraListPtr, int paraValue){
	int tempKey;
	int tempI;
	
	for(tempI = 0; tempI < paraListPtr->actualLength; tempI ++){
		if( paraValue == paraListPtr->data[tempI] ){
			break;
		}
	}
	
	if(tempI >= (paraListPtr->actualLength - 1) ){
		printf("N0 nextElem, the '-1' not the elem\n");
		return -1;
	}
	return paraListPtr->data[tempI + 1];
} 


void MyLocateElementTest(){
	printf("---- MyLocateElementTest begin ----\n");
	
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	//Initialize.
	SequentialListPtr tempList = sequentialListInit(tempArray, 5);
	printf("After initialization, the list is: ");
	outputList(tempList);
	
	//value in the SequentialList
	printf("Now value in SequentialList, the position of the value: %d \n", MyLocateElement(tempList, 5));
	
	//value not in the SequentialList
	printf("Now value not in SequentialList, the position of the value: %d \n", MyLocateElement(tempList, 9));
	
	printf("---- MyLocateElementTest end ----\n");
}
void DestroyList(SequentialListPtr &paraList){
	free(paraList);
	paraList = NULL;
}
int ListEmpty( SequentialListPtr &paraList ){
	if( paraList == NULL || paraList->actualLength == 0 ){
		return 0;
	}
	return 1;
}
void MyGetElementTest(){
	printf("---- MyGetElementTest begin ----\n");
	
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	//Initialize.
	SequentialListPtr tempList = sequentialListInit(tempArray, 5);
	printf("After initialization, the list is: \n");
	outputList(tempList);
	int tempvalue;
	
	//position out of SequentialList
	tempvalue = MyGetElement(tempList, 9);
	printf("Now position not in SequentialList, the value of the position is : %d\n", tempvalue);
	
	//position in the SequentialList
	tempvalue = MyGetElement(tempList, 1);
	printf("Now position in SequentialList, the value of the position is : %d\n", tempvalue);
	
	printf("---- MyGetElementTest end ----\n");
}
void MyClearElementTest(){
	printf("---- MyClearElementTest begin ----\n");
	
	int tempArray[5] = {3, 5, 2, 7, 4};
	
	//Initialize.
	SequentialListPtr tempList = sequentialListInit(tempArray, 5);
	printf("After initialization, the list is: ");
	outputList(tempList);
	
	//clear
	MyClearList(tempList);
	
	printf("After clear, the list is: ");
	outputList(tempList);
	
	outputList(tempList);
	printf("---- MyClearElementTest end ----\n");
}

void DestroyListTest(){
	printf("---- DestroyListTest begin ----\n");
	
	int tempArray[] = {1, 2, 3, 4};
	
	//Initialize.
	SequentialListPtr tempList = sequentialListInit(tempArray, 4);
	printf("After initialization, The list is: \n");
	outputList(tempList);
	
	//Destroy
	printf("Before destruction, the ptr of list: %d\n", tempList);
	DestroyList(tempList);
	printf("After destruction, the ptr: %d\n", tempList);
	printf("---- DestroyListTest end ----\n");
}

void ListEmptyTest(){
	
	printf("---- ListEmptyTest begin ----\n");
	
	//Initialize.
	int tempArray[] = {1, 2, 3, 4};
	SequentialListPtr tempList = sequentialListInit(tempArray, 4);
	outputList(tempList);
	
	//if Empty
	int isEmpty = ListEmpty(tempList);
	
	if(isEmpty){
		printf("After Initialization, the list is no empty\n");
	}
	else{
		printf("After Initialization, the list is empty\n");
	}
	
	MyClearList(tempList);
	isEmpty = ListEmpty(tempList);
	if(isEmpty){
		printf("After Clear, the list is no empty\n");
	}
	else{
		printf("After Clear, the list is empty\n");
	}
	
	tempList->actualLength = 1;// the variate is one
	DestroyList( tempList );
	
	isEmpty = ListEmpty(tempList);
	if(isEmpty){
		printf("After Destrument , the list is no empty\n");
	}
	else{
		printf("After Destrument , the list is empty\n");
	}
	
	printf("---- ListEmptyTest end ----\n");
}
void PriorElemTest(){
	printf("---- PriorElemTest begin ----\n");
	
	//Initialize.
	int tempArray[] = {1, 2, 3, 4};
	SequentialListPtr tempList = sequentialListInit(tempArray, 4);
	outputList(tempList);
	
	//prior
	int priorElem;
	//the prior of 2
	priorElem = PriorElem(tempList, 2);
	printf("the prior of 2:%d\n", priorElem);
	//the prior of 4
	priorElem = PriorElem(tempList, 4);
	printf("the prior of 4:%d\n", priorElem);
	//the prior of 1
	priorElem = PriorElem(tempList, 1);
	printf("the prior of 1:%d\n", priorElem);
	//the prior of 6
	priorElem = PriorElem(tempList, 6);
	printf("the prior of 6:%d\n", priorElem);
	printf("---- PriorElemTest end ----\n");
}

void NextElemTest(){
	printf("---- NextElemTest begin ----\n");
	
	//Initialize.
	int tempArray[] = {1, 2, 3, 4};
	SequentialListPtr tempList = sequentialListInit(tempArray, 4);
	outputList(tempList);
	
	//prior
	int nextElem;
	//the next of 1
	nextElem = NextElem(tempList, 1);
	printf("the next of 1:%d\n", nextElem);
	//the next of 4
	nextElem = NextElem(tempList, 4);
	printf("the next of 1:%d\n", nextElem);
	//the next of 2
	nextElem = NextElem(tempList, 2);
	printf("the next of 1:%d\n", nextElem);
	//the next of 6
	nextElem = NextElem(tempList, 6);
	printf("the next of 1:%d\n", nextElem);
	printf("---- PriorElemTest end ----\n");
}


int main() {
	sequentialInsertTest();
	sequentialDeleteTest();
	MyLocateElementTest();
	MyGetElementTest();
	MyClearElementTest();
	DestroyListTest();
	ListEmptyTest();
	PriorElemTest();
	NextElemTest();
	return 0;
}

测试结果:

---- sequentialInsertTest begins. ----
After initialization, the list is:3 5 2 7 4
Now insert to the first, the list is: 8 3 5 2 7 4
Now insert to the last, 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.
After inserting, the list is: 10 8 3 5 2 7 4 9
Inserting 11.
After inserting, the list is: 11 10 8 3 5 2 7 4 9
Inserting 12.
After inserting, the list is: 12 11 10 8 3 5 2 7 4 9
Inserting 13.
Cannot insert element: list full.
After inserting, the list is: 12 11 10 8 3 5 2 7 4 9
Inserting 14.
Cannot insert element: list full.
After inserting, the list is: 12 11 10 8 3 5 2 7 4 9
---- sequenttialInsertTest ends. ----
---- sequentialDeleteTest 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: Cannot dalete element: the position 5 is beyond the list length 3.
5 2 7
Now delete the(-6)th, the list is: Invalid positiion : -6.
5 2 7
---- sequentialDeleteTest ends. ----
The address of the structure: 7367488
The address of actualLength: 7367488
The address of the data: 7367492
The address of the actual data: 7367492
The address of the second data: 7367496
---- MyLocateElementTest begin ----
After initialization, the list is: 3 5 2 7 4
Now value in SequentialList, the position of the value: 1
Now value not in SequentialList, the position of the value: -1
---- MyLocateElementTest end ----
---- MyGetElementTest begin ----
After initialization, the list is:
3 5 2 7 4
no the position
Now position not in SequentialList, the value of the position is : -1
Now position in SequentialList, the value of the position is : 5
---- MyGetElementTest end ----
---- MyClearElementTest begin ----
After initialization, the list is: 3 5 2 7 4
After clear, the list is: no value
no value
---- MyClearElementTest end ----
---- DestroyListTest begin ----
After initialization, The list is:
1 2 3 4
Before destruction, the ptr of list: 7367744
After destruction, the ptr: 0
---- DestroyListTest end ----
---- ListEmptyTest begin ----
1 2 3 4
After Initialization, the list is no empty
After Clear, the list is empty
After Destrument , the list is empty
---- ListEmptyTest end ----
---- PriorElemTest begin ----
1 2 3 4
the prior of 2:1
the prior of 4:3
No priorElem, the '-1' not the elem
the prior of 1:-1
No priorElem, the '-1' not the elem
the prior of 6:-1
---- PriorElemTest end ----
---- NextElemTest begin ----
1 2 3 4
the next of 1:2
N0 nextElem, the '-1' not the elem
the next of 1:-1
the next of 1:3
N0 nextElem, the '-1' not the elem
the next of 1:-1
---- PriorElemTest end ----

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值