目录
线性表的定义
线性表是最基本、最简单、也是最常用的一种数据结构。线性表(linear list)是数据结构的一种,一个线性表是n个具有相同特性的数据元素的有限序列。
线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的(注意,这句话只适用大部分线性表,而不是全部。比如,循环列表逻辑层次上也是一种线性表(存储层次上属于链式存储,但是把最后一个数据元素的尾指针指向了首位结点)。
分类
我们说“线性”和“非线性”,只在逻辑层次上讨论,而不考虑存储层次,所以双向链表和循环链表依旧是线性表。
在数据结构逻辑层次上细分,线性表可分为一般线性表和受限线性表。一般线性表也就是我们通常所说的“线性表”,可以自由的删除或添加结点。受限线性表主要包括栈和队列,受限表示对结点的操作受限制。
优点
线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。
临摹老师的代码
一,定义头文件与结构体
#include <stdio.h>
#include <malloc.h>
#define LIST_MAX_LENGTH 10
/**
* 整数的顺序表,最重要的是存储数据
*/
typedef struct SequentialList {
int actualLength;
int data[LIST_MAX_LENGTH];//此处固定了存储数据的最大的长度,为LIST_MAX_LENGTH个int类型的数
} *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", ¶ListPtr->actualLength);
printf("The address of data: %ld\r\n", ¶ListPtr->data);
printf("The address of actual data: %ld\r\n", ¶ListPtr->data[0]);
printf("The address of second data: %ld\r\n", ¶ListPtr->data[1]);
}//用循环来输出顺序表各类数据的地址
四,初始化顺序表
/**
* 初始化顺序表。且此功能没有错误检查
* @param paraListPtr 是一个指针。而且它必须是一个用于更改顺序表的指针
* @param paraValues 是存储所有数据的一个int数组
*/
SequentialListPtr sequentialListInit(int paraData[], int paraLength) {
SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(SequentialListPtr));
for (int i = 0; i < paraLength; i++) {
resultPtr->data[i] = paraData[i];
}
resultPtr->actualLength = paraLength;
return resultPtr;
}//用循环来初始化顺序表
五,顺序表的插入
/**
* 将数据插入到顺序表中
* @param paraListPtr 是一个指针。而且它必须是一个用于更改顺序表的指针
* @param paraPosition 是一个地址, e.g., 0代表着插入到顺序表的第一个地址
* @param paraValue 是要插入的
*/
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
// Step 1. 检查顺序表空间大小是否够用
if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
printf("Cannot insert element: list full.\r\n");
return;
}
// Step 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;
}
// Step 3. 移动要插入的地方后面的其余数据
for (int i = paraListPtr->actualLength; i > paraPosition; i--) {
paraListPtr->data[i] = paraListPtr->data[i - 1];
}
// Step 4. 插入数据
paraListPtr->data[paraPosition] = paraValue;
// Step 5. 更新顺序表的长度
paraListPtr->actualLength ++;
}//用循环在顺序表中间插入数据
六,测试插入功能
/**
* 测试插入的功能是否正常
*/
void sequentialInsertTest() {
int i;
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialInsertTest begins. ----\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);
//将5个数据插入第三个位置
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");
}//测试插入的功能是否正常
七,顺序表的删除
/**
* 在顺序表中删除一个数据
* @param paraListPtr 是一个指针。而且它必须是一个用于更改顺序表的指针
* @param paraPosition 是一个地址, e.g., 0代表着插入到顺序表的第一个地址
* @return 返回删除的值
*/
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
// Step 1. 检查地址是否正确
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;
}
// Step 2. 移动要删除的地方后面的其余数据
int resultValue = paraListPtr->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->actualLength; i++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
// Step 3. 更新顺序表的长度
paraListPtr->actualLength --;
// Step 4. 返回被删除的数据
return resultValue;
}//用循环在顺序表中间删除数据
八,测试删除功能
/**
*测试删除功能是否正常
*/
void sequentialDeleteTest() {
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialDeleteTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 删除顺序表的首位
printf("Now delete the first, the list is: ");
sequentialListDelete(tempList, 0);
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);
}//测试删除的功能是否正常
九,程序入口
/**
* 程序入口
*/
void main() {
sequentialInsertTest();
sequentialDeleteTest();
}
十,运行结果
---- 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.
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
---- sequentialInsertTest 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: 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
---- sequentialDeleteTest ends. ----
The address of the structure: 6757056
The address of actualLength: 6757056
The address of data: 6757060
The address of actual data: 6757060
The address of second data: 6757064
特殊功能函数编写
一,找到顺序表中特定的数据的位置
/**
* 找到顺序表中特定的数据的位置
*/
int locateElement(SequentialListPtr paraListPtr, int paraValue){
for(int i = 0; i < paraListPtr->actualLength; i ++){
if(paraListPtr->data[i] == paraValue){
return i;
}
}
return -1;
}
/**
* 测试locateElement功能是否正常
*/
void sequentiallocateElementTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentiallocateElementTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 寻找数据
for(int i = 2; i < 8; i++){
printf("Now find the paraValue's position: ");
int temp = locateElement(tempList, i);
if(temp != -1){
printf("The paraValue %d's position is : %d\r\n", i, temp);
}else{
printf("The paraValue %d is invalid.\r\n", i);
}
}
}
二,找到顺序表中特定的位置的数据
/**
* 找到顺序表中特定的位置的数据
*/
int getElement(SequentialListPtr paraListPtr, int paraPosition){
if(paraPosition < 0 || paraPosition >= paraListPtr->actualLength){
printf("Invalid position: %d.\r\n", paraPosition);
return -1;
}else{
return paraListPtr->data[paraPosition];
}
}
/**
* 测试getElement功能是否正常
*/
void sequentialgetElementTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialgetElementTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 寻找特定位置的数据
for(int i = -1; i < 7; i++){
printf("Now find the paraPosition's value: ");
int temp = getElement(tempList, i);
if(temp != -1){
printf("The paraPosition %d's value is : %d\r\n", i, temp);
}
}
}
三,清除顺序表中所有的数据
/**
* 清除顺序表中所有的数据
*/
void clearList(SequentialListPtr paraListPtr){
paraListPtr->actualLength = 0;
}
/**
* 测试clearlist功能是否正常
*/
void sequentialclearListTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialclearListTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 清除顺序表
printf("Now clear the list: ");
clearList(tempList);
outputList(tempList);
}
四,找到顺序表中特定数据的前驱
/**
* 找到顺序表中特定数据的前驱
*/
int priorElement(SequentialListPtr paraListPtr, int paraPosition){
paraPosition --;
if(paraPosition < 0 || paraPosition >= paraListPtr->actualLength){
printf("The position %d have invalid prior position: %d.\r\n", paraPosition+1, paraPosition);
return -1;
}else{
return paraListPtr->data[paraPosition];
}
}
/**
* 测试priorElement功能是否正常
*/
void sequentialpriorElementTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialpriorElementTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 寻找前驱数据
for(int i = 0; i < 7; i++){
printf("Now find the prior value: ");
int temp = priorElement(tempList, i);
if(temp != -1){
printf("The paraPosition %d's prior value is : %d\r\n", i, temp);
}
}
}
五,找到顺序表中特定数据的后驱
/**
* 找到顺序表中特定数据的后驱
*/
int nextElement(SequentialListPtr paraListPtr, int paraPosition){
paraPosition ++;
if(paraPosition < 0 || paraPosition >= paraListPtr->actualLength){
printf("The position %d have invalid next position: %d.\r\n", paraPosition-1, paraPosition);
return -1;
}else{
return paraListPtr->data[paraPosition];
}
}
/**
* 测试nextElement功能是否正常
*/
void sequentialnextElementTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialnextElementTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 寻找后驱数据
for(int i = -1; i < 6; i++){
printf("Now find the next value: ");
int temp = nextElement(tempList, i);
if(temp != -1){
printf("The paraPosition %d's next value is : %d\r\n", i, temp);
}
}
}
六,修改程序入口
/**
* 程序入口
*/
void main() {
sequentialInsertTest();
sequentialDeleteTest();
sequentiallocateElementTest();
sequentialgetElementTest();
sequentialpriorElementTest();
sequentialnextElementTest();
sequentialclearListTest();
system("Pause");
}
七,运行结果
---- 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.
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
---- sequentialInsertTest 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: 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
---- sequentialDeleteTest ends. ----
The address of the structure: 7609024
The address of actualLength: 7609024
The address of data: 7609028
The address of actual data: 7609028
The address of second data: 7609032
---- sequentiallocateElementTest begins. ----
After initialization, the list is: 3 5 2 7 4
Now find the paraValue's position: The paraValue 2's position is : 2
Now find the paraValue's position: The paraValue 3's position is : 0
Now find the paraValue's position: The paraValue 4's position is : 4
Now find the paraValue's position: The paraValue 5's position is : 1
Now find the paraValue's position: The paraValue 6 is invalid.
Now find the paraValue's position: The paraValue 7's position is : 3
---- sequentialgetElementTest begins. ----
After initialization, the list is: 3 5 2 7 4
Now find the paraPosition's value: Invalid position: -1.
Now find the paraPosition's value: The paraPosition 0's value is : 3
Now find the paraPosition's value: The paraPosition 1's value is : 5
Now find the paraPosition's value: The paraPosition 2's value is : 2
Now find the paraPosition's value: The paraPosition 3's value is : 7
Now find the paraPosition's value: The paraPosition 4's value is : 4
Now find the paraPosition's value: Invalid position: 5.
Now find the paraPosition's value: Invalid position: 6.
---- sequentialpriorElementTest begins. ----
After initialization, the list is: 3 5 2 7 4
Now find the prior value: The position 0 have invalid prior position: -1.
Now find the prior value: The paraPosition 1's prior value is : 3
Now find the prior value: The paraPosition 2's prior value is : 5
Now find the prior value: The paraPosition 3's prior value is : 2
Now find the prior value: The paraPosition 4's prior value is : 7
Now find the prior value: The paraPosition 5's prior value is : 4
Now find the prior value: The position 6 have invalid prior position: 5.
---- sequentialnextElementTest begins. ----
After initialization, the list is: 3 5 2 7 4
Now find the next value: The paraPosition -1's next value is : 3
Now find the next value: The paraPosition 0's next value is : 5
Now find the next value: The paraPosition 1's next value is : 2
Now find the next value: The paraPosition 2's next value is : 7
Now find the next value: The paraPosition 3's next value is : 4
Now find the next value: The position 4 have invalid next position: 5.
Now find the next value: The position 5 have invalid next position: 6.
---- sequentialclearListTest begins. ----
After initialization, the list is: 3 5 2 7 4
Now clear the list: The list is empty.
请按任意键继续. . .
八,修改程序后的优点
在修改后的程序中,我加入寻找特定数据,位置的操作等等,方便我们通过多个函数接口来实现对顺序表的多种操作来实现对顺序表的进一步操作。
九,可以修改的方向
1,可以加入更多的功能函数。
2,可以将地址的打印输出换为%p获得更准确的地址值。
对于本次作业的收获
在本次作业中,我学会了一种简单的数据结构种类--顺序表,同时在对老师代码的临摹中,我明白了程序编写过程中需要尽量减少主函数的内容而将功能通过函数来封装,并且在程序中加入测试功能可以极大的减少我们debug的时间;在对程序功能增加的过程中,我也对于线性表有了更深的理解,同时我也明白了规范化代码的重要性。
最终完整代码
#include <stdio.h>
#include <malloc.h>
#define LIST_MAX_LENGTH 10
/**
* 整数的顺序表,最重要的是存储数据
*/
typedef struct SequentialList {
int actualLength;
int data[LIST_MAX_LENGTH];//此处固定了存储数据的最大的长度,为LIST_MAX_LENGTH个int类型的数
} *SequentialListPtr;
/**
* 输出顺序表
*/
void outputList(SequentialListPtr paraList) {
if(paraList->actualLength == 0){
printf("The list is empty.\r\n");
}else{
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", ¶ListPtr->actualLength);
printf("The address of data: %ld\r\n", ¶ListPtr->data);
printf("The address of actual data: %ld\r\n", ¶ListPtr->data[0]);
printf("The address of second data: %ld\r\n", ¶ListPtr->data[1]);
}//用循环来输出顺序表各类数据的地址
/**
* 初始化顺序表。且此功能没有错误检查
* @param paraListPtr 是一个指针。而且它必须是一个用于更改顺序表的指针
* @param paraValues 是存储所有数据的一个int数组
*/
SequentialListPtr sequentialListInit(int paraData[], int paraLength) {
SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(SequentialListPtr));
for (int i = 0; i < paraLength; i++) {
resultPtr->data[i] = paraData[i];
}
resultPtr->actualLength = paraLength;
return resultPtr;
}//用循环来初始化顺序表
/**
* 将数据插入到顺序表中
* @param paraListPtr 是一个指针。而且它必须是一个用于更改顺序表的指针
* @param paraPosition 是一个地址, e.g., 0代表着插入到顺序表的第一个地址
* @param paraValue 是要插入的
*/
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
// Step 1. 检查顺序表空间大小是否够用
if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
printf("Cannot insert element: list full.\r\n");
return;
}
// Step 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;
}
// Step 3. 移动要插入的地方后面的其余数据
for (int i = paraListPtr->actualLength; i > paraPosition; i--) {
paraListPtr->data[i] = paraListPtr->data[i - 1];
}
// Step 4. 插入数据
paraListPtr->data[paraPosition] = paraValue;
// Step 5. 更新顺序表的长度
paraListPtr->actualLength ++;
}//用循环在顺序表中间插入数据
/**
* 测试插入的功能是否正常
*/
void sequentialInsertTest() {
int i;
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialInsertTest begins. ----\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);
//将5个数据插入第三个位置
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");
}//测试插入的功能是否正常
/**
* 在顺序表中删除一个数据
* @param paraListPtr 是一个指针。而且它必须是一个用于更改顺序表的指针
* @param paraPosition 是一个地址, e.g., 0代表着插入到顺序表的第一个地址
* @return 返回删除的值
*/
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
// Step 1. 检查地址是否正确
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;
}
// Step 2. 移动要删除的地方后面的其余数据
int resultValue = paraListPtr->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->actualLength; i++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}
// Step 3. 更新顺序表的长度
paraListPtr->actualLength --;
// Step 4. 返回被删除的数据
return resultValue;
}//用循环在顺序表中间删除数据
/**
*测试删除功能是否正常
*/
void sequentialDeleteTest() {
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialDeleteTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 删除顺序表的首位
printf("Now delete the first, the list is: ");
sequentialListDelete(tempList, 0);
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;
}
}
return -1;
}
/**
* 测试locateElement功能是否正常
*/
void sequentiallocateElementTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentiallocateElementTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 寻找数据
for(int i = 2; i < 8; i++){
printf("Now find the paraValue's position: ");
int temp = locateElement(tempList, i);
if(temp != -1){
printf("The paraValue %d's position is : %d\r\n", i, temp);
}else{
printf("The paraValue %d is invalid.\r\n", i);
}
}
}
/**
* 找到顺序表中特定的位置的数据
*/
int getElement(SequentialListPtr paraListPtr, int paraPosition){
if(paraPosition < 0 || paraPosition >= paraListPtr->actualLength){
printf("Invalid position: %d.\r\n", paraPosition);
return -1;
}else{
return paraListPtr->data[paraPosition];
}
}
/**
* 测试getElement功能是否正常
*/
void sequentialgetElementTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialgetElementTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 寻找特定位置的数据
for(int i = -1; i < 7; i++){
printf("Now find the paraPosition's value: ");
int temp = getElement(tempList, i);
if(temp != -1){
printf("The paraPosition %d's value is : %d\r\n", i, temp);
}
}
}
/**
* 清除顺序表中所有的数据
*/
void clearList(SequentialListPtr paraListPtr){
paraListPtr->actualLength = 0;
}
/**
* 测试clearlist功能是否正常
*/
void sequentialclearListTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialclearListTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 清除顺序表
printf("Now clear the list: ");
clearList(tempList);
outputList(tempList);
}
/**
* 找到顺序表中特定数据的前驱
*/
int priorElement(SequentialListPtr paraListPtr, int paraPosition){
paraPosition --;
if(paraPosition < 0 || paraPosition >= paraListPtr->actualLength){
printf("The position %d have invalid prior position: %d.\r\n", paraPosition+1, paraPosition);
return -1;
}else{
return paraListPtr->data[paraPosition];
}
}
/**
* 测试priorElement功能是否正常
*/
void sequentialpriorElementTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialpriorElementTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 寻找前驱数据
for(int i = 0; i < 7; i++){
printf("Now find the prior value: ");
int temp = priorElement(tempList, i);
if(temp != -1){
printf("The paraPosition %d's prior value is : %d\r\n", i, temp);
}
}
}
/**
* 找到顺序表中特定数据的后驱
*/
int nextElement(SequentialListPtr paraListPtr, int paraPosition){
paraPosition ++;
if(paraPosition < 0 || paraPosition >= paraListPtr->actualLength){
printf("The position %d have invalid next position: %d.\r\n", paraPosition-1, paraPosition);
return -1;
}else{
return paraListPtr->data[paraPosition];
}
}
/**
* 测试nextElement功能是否正常
*/
void sequentialnextElementTest(){
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialnextElementTest begins. ----\r\n");
// 初始化
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// 寻找后驱数据
for(int i = -1; i < 6; i++){
printf("Now find the next value: ");
int temp = nextElement(tempList, i);
if(temp != -1){
printf("The paraPosition %d's next value is : %d\r\n", i, temp);
}
}
}
/**
* 程序入口
*/
void main() {
sequentialInsertTest();
sequentialDeleteTest();
sequentiallocateElementTest();
sequentialgetElementTest();
sequentialpriorElementTest();
sequentialnextElementTest();
sequentialclearListTest();
system("Pause");
}