本文设计了最基本的顺序表代码,其主要功能如下:
-
sequentialListInit():顺序表初始化。
-
sequentialListInsert():插入数据。
-
sequentialListDelete():删除数据。
-
locateElement():已知数据求位置。
-
getElement():已知位置求数据。
-
outputList():打印顺序表数据。
-
outputMemory():打印顺序表地址。
在老师的基础上采用了c++进行编码,将LIST_MAX_LENGTH的宏定义改为了全局常量,能在运行时进行类型检查,在数据修改函数中新增局部变量position进行数值替换,在删除数据函数中新增空间判断,表空时进行空表提示,代码如下:
//
// Created by Caka on 2023/3/25.
//
#include <malloc.h>
#include <iostream>
using namespace std;
const int LIST_MAX_LENGTH = 10;
/**
* SequentialList data structure definition
*/
typedef struct SequentialList {
int actualLength;
int data[LIST_MAX_LENGTH]; //The maximum length is fixed.
} *SequentialListPtr;
/**
* Initialize a sequential list. No error checking for this function.
*/
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];
}// Of for i
resultPtr->actualLength = paraLength;
return resultPtr;
}//Of sequentialListInit
/**
* Insert an element into a sequential list.
*/
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
// Step 1. Space check.
if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
cout << "Cannot insert element: because the list is full." << endl;
return;
}//Of if
// Step 2. Position check.
if (paraPosition < 0) {
cout << "Cannot insert element: negative position unsupported." << endl;
return;
}//Of if
if (paraPosition > paraListPtr->actualLength) {
cout << "Cannot insert element: the position " << paraPosition << " is bigger than the list length "
<< paraListPtr->actualLength << endl;
return;
}//Of if
// Step 3. Move the remaining part.
for (int i = paraListPtr->actualLength; i > paraPosition; i--) {
paraListPtr->data[i] = paraListPtr->data[i - 1];
}//Of for i
// Step 4. Insert.
paraListPtr->data[paraPosition] = paraValue;
// Step 5. Update the length.
paraListPtr->actualLength++;
}// Of sequentialListInsert
/**
* Delete an element from a sequential linear list.
*/
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
// Step 1. Position check.
if (paraListPtr->actualLength == 0) {
cout << "Cannot delete element: because the list is empty." << endl;
return -1;
}
// Step 2. Position check.
if (paraPosition < 0) {
cout << "Invalid position: " << paraPosition << endl;
return -1;
}//Of if
if (paraPosition >= paraListPtr->actualLength) {
cout << "Cannot delete element: the position " << paraPosition << " is beyond the list length "
<< paraListPtr->actualLength << endl;
return -1;
}//Of if
// Step 3. 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
// Step 4. Update the length.
paraListPtr->actualLength--;
// Step 5. Return the value.
return resultValue;
}// Of sequentialListDelete
/**
* Output the list.
*/
void outputList(SequentialListPtr paraList) {
for (int i = 0; i < paraList->actualLength; i++) {
cout << paraList->data[i] << " ";
}// Of for i
cout << endl;
}// Of outputList
/**
* Output the memory for the list.
*/
void outputMemory(SequentialListPtr paraListPtr) {
cout << "The address of the structure: " << paraListPtr << endl;
cout << "The address of actualLength: " << ¶ListPtr->actualLength << endl;
cout << "The address of data: " << ¶ListPtr->data << endl;
cout << "The address of actual data: " << ¶ListPtr->data[0] << endl;
cout << "The address of second data: " << ¶ListPtr->data[1] << endl;
}// Of outputMemory
/**
* Locate an element in the list.
*/
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.
*/
int getElement(SequentialListPtr paraListPtr, int paraPosition) {
// Step 1. Position check.
if (paraPosition < 0) {
cout << "Invalid position: " << paraPosition << endl;
return -1;
}//Of if
if (paraPosition >= paraListPtr->actualLength) {
cout << "Cannot get element: the position " << paraPosition << "is beyond the list length "
<< paraListPtr->actualLength << endl;
return -1;
}//Of if
return paraListPtr->data[paraPosition];
}// Of locateElement
/**
* Clear elements in the list.
*/
void clearList(SequentialListPtr paraListPtr) {
paraListPtr->actualLength = 0;
cout << "This list has been cleared" << endl;
}// Of clearList
/**
The Main function.
*/
int main() {
int i;
int ArrayList[5] = {1, 2, 3, 4, 5};
cout << "---- sequentialListTest begins. ---- " << endl;
// Initialize.
SequentialListPtr tempList = sequentialListInit(ArrayList, 5);
cout << "After initialization, the list is: " << endl;
outputList(tempList);
// Insert to the first.
cout << "Now insert to the first, the list is: " << endl;
sequentialListInsert(tempList, 0, 0);
outputList(tempList);
// Insert to the last.
cout << "Now insert to the last, the list is: " << endl;
sequentialListInsert(tempList, 6, 6);
outputList(tempList);
// Insert beyond the tail.
cout << "Now insert beyond the tail." << endl;
sequentialListInsert(tempList, 8, 7);
cout << "The list is: " << endl;
outputList(tempList);
// Insert to position 3.
int position = tempList->actualLength;
for (i = 0; i < 5; i++) {
cout << "After inserting " << (i + 7) << " " << "The list is :" << endl;
sequentialListInsert(tempList, position, (i + 7));
position += 1;
if (position <= LIST_MAX_LENGTH) {
outputList(tempList);
}
}//Of for i
position = tempList->actualLength - 1;
cout << endl;
// Test the locateElement Function
cout << "The number 1' position is: " << locateElement(tempList, 1) << endl;
// Test the getElement Function
cout << "The position 1' number is: " << getElement(tempList, 1) << endl;
cout << endl;
// Test the outMemory Function
outputMemory(tempList);
cout << endl;
// Delete the first.
cout << "Now delete the first, the list is: " << endl;
if (sequentialListDelete(tempList, 0) != -1) {
position -= 1;
}
outputList(tempList);
// Delete to the last.
cout << "Now delete the last, the list is: " << endl;
if (sequentialListDelete(tempList, position) != -1) {
position -= 1;
}
outputList(tempList);
// Delete the second.
cout << "Now delete the second, the list is: " << endl;
if (sequentialListDelete(tempList, 1) != -1) {
position -= 1;
}
outputList(tempList);
// Delete the second.
cout << "Now delete the 5th, the list is: " << endl;
if (sequentialListDelete(tempList, 4) != -1) {
position -= 1;
}
outputList(tempList);
// Delete the second.
cout << "Now delete the (-6)th, the list is: " << endl;
if (sequentialListDelete(tempList, -6) != -1) {
position -= 1;
}
for (i = 0; i <= 6; i++) {
cout << "After deleting " << "the first number" << " The list is :" << endl;
sequentialListDelete(tempList, 0);
if (position <= LIST_MAX_LENGTH) {
outputList(tempList);
}
}//Of for i
// Test the clearList Function
clearList(tempList);
cout << "---- sequentialListTest ends. ---- " << endl;
return 0;
}// Of main
运行结果如下:
---- sequentialListTest begins. ----
After initialization, the list is:
1 2 3 4 5
Now insert to the first, the list is:
0 1 2 3 4 5
Now insert to the last, the list is:
0 1 2 3 4 5 6
Now insert beyond the tail.
Cannot insert element: the position 8 is bigger than the list length 7
The list is:
0 1 2 3 4 5 6
After inserting 7 The list is :
0 1 2 3 4 5 6 7
After inserting 8 The list is :
0 1 2 3 4 5 6 7 8
After inserting 9 The list is :
0 1 2 3 4 5 6 7 8 9
After inserting 10 The list is :
Cannot insert element: because the list is full.
After inserting 11 The list is :
Cannot insert element: because the list is full.
The number 1' position is: 1
The position 1' number is: 1
The address of the structure: 0xe61650
The address of actualLength: 0xe61650
The address of data: 0xe61654
The address of actual data: 0xe61654
The address of second data: 0xe61658
Now delete the first, the list is:
1 2 3 4 5 6 7 8 9
Now delete the last, the list is:
1 2 3 4 5 6 7 8
Now delete the second, the list is:
1 3 4 5 6 7 8
Now delete the 5th, the list is:
1 3 4 5 7 8
Now delete the (-6)th, the list is:
Invalid position: -6
After deleting the first number The list is :
3 4 5 7 8
After deleting the first number The list is :
4 5 7 8
After deleting the first number The list is :
5 7 8
After deleting the first number The list is :
7 8
After deleting the first number The list is :
8
After deleting the first number The list is :
After deleting the first number The list is :
Cannot delete element: because the list is empty.
This list has been cleared
---- sequentialListTest ends. ----
522

被折叠的 条评论
为什么被折叠?



