顺序表是最常用的数据结构
1. 代码 (20223版)
代码如下
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<malloc.h>
#define LIST_MAX_LENGTH 10typedef struct Sequentialist {
int actualLength;
int date[LIST_MAX_LENGTH];
}*SequentiaListPtr;
//Output a linked list
void outputlist(*SequentiaListPtr paraList) {
int i = 0;
for (i = 0; i < paraList->actualLength; i++) {
printf("%d", paraList->date[i]);
}
printf("\r\n");
}
//Output address
void outputMemory(SequentialistPtr 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]);
}
/*Initialize the linked list
A pointer to a list. It must be a pointer to the change list
An int array that stores all elements
*/
//Initialize the linked list
SequentiaListPtr SequntiaListInit(int paraDate[], int paralength) {
SequentialistPtr resultPtr = (SequentiaListPtr)malloc(sizeof(struct Sequentialist));
for (i = 0; i < paraLength; i++) {
resultPtr->date[i] = paraDate[i];
}
resultPtr->actualLength = paralength;
reuturn resultresultPtr;
}
/*An element in a sequential linear list
A pointer to a list. It must be a pointer to the change list
The value to insert*/
void sequentiaListPtrInsert(SequentiaListPtr paraListPtr, int paraPosition, int paraValue) {
// Space check
if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
printf("Cannot insert element: list full.\r\n");
return;
}
//Position check
if (paraPosition < 0) {
printf("Cannot insert element: negative position unsupported.");
return;
}
if (paraPosition->actualListPtr->actualLength) {
printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return;
}
// Move the remaining part
for (int i = paraListPtr->actualLength : i > paraPosition; i--) {
paraListPtr->date[i] = paraListPtr->date[i - 1];
}
//Insert
paraListPtr->date[paraPosition] = paraValue;
//Update the length
paraListPtr->actualLength i++;
}
//Test the insert function
void sequentiaListPtrInsertTest() {
int i;
int tempArray[5] = { 3,5,2,7,4 };
printf("---- sequentialInsertTest begins. ----\r\n");
// Initialize
SequentiaListPtr temList = SequentialistInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
//Inser to the first
printf("Now insert to the first, the list is: ");
sequentiaListPtrInsert(tempList, 0, 8);
outputList(tempList);
//Insert to the last
printf("Now insert to the last, the list is: ");
sequentiaListPtrInsert(tempList, 0, 8);
outputList(tempList);
//Insert beyond the tail
printf("Now insert beyond the tail. \r\n");
sequentiaListPtrInsert(tempList, 8, 9);
printf("The list is:");
outputList(tempList);
//Insert to position 3
for (i = 0; i < 5; i++) {
printf("Inserting %d.\r\n", (i + 10));
sequentiaListPtrInsert(tempList, 0, (i + 10));
outputList(tempList);
}
printf("---- sequentialInsertTest ends. ----\r\n");
}
/*Delete an element from a sequential linear list.
paraListPtr The pointer to the list. It must be a pointer to change the list.
paraPosition The position, e.g., 0 stands for inserting at the first position.
return The deleted value*/
int sequentiaListDelete(SequentiaListPtr paraListPtr, int paraPosiotion) {
//Position check
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;
}
//Move the remaining part
int resultValue = paraListPtr->date[paraPosition];
for (int i = paraPosition; i < paraList->actualLength; i++) {
paraListPtr->date[i] = paraListPtr->date[i + 1];
}
//Update the length
paraListPtr->actualLength--;
//Return the value
return resultValue;
}
//Test the delete function
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);printf("---- sequentialDeleteTest ends. ----\r\n");
outputMemory(tempList);
}
/*Locate an element in the list.
paraListPtr The pointer to the list.
paraValue the indicated value.
return The position of the value, or -1 indicating not exists*/
int locateElement(SequentialListPtr paraListPtr, int paraValue) {
for (int i = 0; i < paraListPtr->actualLength; i++) {
if (paraListPtr->data[i] == paraValue) {
return i;
}
}return -1;
}
/*Get an element in the list.
paraListPtr The pointer to the list.
paraPosition The given position.
return The position of the value, or -1 indicating not exists*/
int getElement(SequentialListPtr paraListPtr, int paraPosition) {
// Position check
if (paraPosition < 0) {
printf("Invalid position: %d.\r\n", paraPosition);
return -1;
}if (paraPosition >= paraListPtr->actualLength) {
printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}return paraListPtr->data[paraPosition];
}
/* Clear elements in the list.
paraListPtr The pointer to the list.
return The position of the value, or -1 indicating not exists*/
void clearList(SequentialListPtr paraListPtr) {
paraListPtr->actualLength = 0;
}
/*The entrance*/
void main() {
sequentialInsertTest();
sequentialDeleteTest();
}
2. 运行结果