顺序表:
指的是用一组地址连续的存储单元依次存储线性表的数据元素,这种表示也称作线性表的顺序存储结构或顺序映像。通常,称这种存储结构的线性表为顺序表(Seguential List)。
它与数组类似,可用一维数组表示。它们都用变量表示顺序表长度属性,但数组长度不可动态定义。
特点:
1)逻辑上相邻的数据元素,其物理位置也是相邻的。
2)顺序存储结构是占用一片连续的存储空间。
* 地址连续,依次存放,随机存取,类型相同 *
参数传递:
函数调用时传送给形参表的实参必须与形参三个一致(类型,个数,顺序)。
方式:①传值(参数为整型、实型、字符型)②传地址
参数为指针变量、引用类型、数组名。
代码
#include <stdio.h> #include <malloc.h> /* malloc动态内存分配函数, 开辟m字节长度的地址空间,并返回这段空间的首地址 */ #define LIST_MAX_LENGTH 10//线性表储存空间的初始分配量 //Linear list of integers. The key is data.整数的线性列表。关键是数据。 typedef struct SequentialList { int actualLength; int date[LIST_MAX_LENGTH];//The maximum length is fixed.最大长度是固定的。 } *SequentialListPtr; //Output the list. void outputList(SequentialListPtr paraList) { for(int i = 0; i< paraList->actualLength; i++) /*定义一个结构体,然后申明一个指针指向这个结构体, 就要用指针取出结构体中的数据,就要用到“->”.*/ { printf("%d ", paraList->date[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\r\n", paraListPtr); printf("The address of actualLength: %ld\r\n", ¶ListPtr->actualLength); printf("The address of date: %ld\r\n", ¶ListPtr->date); printf("The address of actual date: %ld\r\n", ¶ListPtr->date[0]); printf("The address of second date: %ld\r\n", ¶ListPtr->date[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 paraListPtr指向列表的指针。它必须是一个指针才能更改列表。 @param paraValues An int array storing all elements. param paraValues存储所有元素的int数组。 */ SequentialListPtr sequentialListInit(int paraData[], int paraLength) { SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct SequentialList)); //(sequentialListPtr)强制类型转换 sizeof(x)运算,计算变量x的长度 for (int i = 0; i < paraLength; i++) { resultPtr->date[i] = paraData[i]; }//Of for i resultPtr->actualLength = paraLength; return resultPtr; }//Of sequentialListIni /* 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 paraListPtr指向列表的指针。它必须是一个指针才能更改列表。 @param paraPosition The position, e.g., 0 stands for inserting at the first position. param paraPosition位置,例如,0代表在第一个位置插入 @param paraValue The value to be inserted. param paraValue要插入的值。 */ void sequentialListInsert(SequentialListPtr paraListPtr, int paraposition, int paraValue) { if (paraListPtr->actualLength >=LIST_MAX_LENGTH) { // Step 1. Space check. printf("Cannot insert element: list full.\r\n"); return; }//Of if //Step 2. Position check. if (paraposition < 0) { printf("Cannot insert element: negative position unsupported."); return; }//Of if if (paraposition > paraListPtr->actualLength) { printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n",paraposition,paraListPtr->actualLength); return; }//Of if //Step 3. Move the remaining part. for (int i = paraListPtr->actualLength; i > paraposition; i --) { paraListPtr->date[i] = paraListPtr->date[i - 1]; }//Of for i //Step 4. Insert. paraListPtr->date[paraposition] = paraValue; //Step 5. Update the length. paraListPtr->actualLength ++; }//Of sequentialListInsert /* Test the insert funciton. */ void sequentialInsertTest() { int i; int tempArray[5] = {3, 5, 2, 7, 4}; printf("---- sequentialInsertTest begins. ----\r\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: "); sequentialListInsert(tempList, 0, 8); outputList(tempList); //Insert to the last. printf("Now insert to the last,the list is: "); sequentialListInsert(tempList, 6, 9); outputList(tempList); //Insert beyond the tail. printf("Now insert beyong the tail. \r\n"); sequentialListInsert(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)); sequentialListInsert(tempList, 0 , (i + 10)); outputList(tempList); }//Of for i printf("---- sequentialInserTest ends. ----\r\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 paraListPtr指向列表的指针。它必须是一个指针才能更改列表。 * @param paraPosition The position, e.g., 0 stands for inserting at the first position. param paraPosition位置,例如,0代表在第一个位置插入。 * @return The deleted value.返回删除的值 */ int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) { // Step 1. Position check. if (paraPosition < 0) { printf("Invalid position: %d.\r\n", paraPosition); return -1; }//Of if if (paraPosition >= paraListPtr->actualLength) { printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength); return -1; }//Of if // Step 2. Move the remaining part. int resultValue = paraListPtr->date[paraPosition]; for (int i = paraPosition; i < paraListPtr->actualLength; i ++) { paraListPtr->date[i] = paraListPtr->date[i + 1]; }//Of for i // Step 3. Update the length. paraListPtr->actualLength --; // Step 4. Return the value. return resultValue; }// Of sequentialListDelete /** * 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); // Delete the second. printf("Now delete the (-6)th, the list is: "); sequentialListDelete(tempList, -6); outputList(tempList); printf("---- sequentialDeleteTest ends. ----\r\n"); outputMemory(tempList); }// Of sequentialDeleteTest /** * Locate an element in the list.在列表中找到一个元素。 * @param paraListPtr The pointer to the list.param paraListPtr指向列表的指针。 * @param paraValue the indicated value.param paraValue指示值。 * @return The position of the value, or -1 indicating not existsreturn 返回值的位置,或-1表示不存在 */ int locateElement(SequentialListPtr paraListPtr, int paraValue) { for (int i = 0; i < paraListPtr->actualLength; i ++) { if (paraListPtr->date[i] == paraValue) { return i; }// Of if }//Of for i return -1; }// Of locateElement /** * Get an element in the list.*在列表中获取一个元素。 * @param paraListPtr The pointer to the list. param paraListPtr指向列表的指针 * @param paraPosition The given position. param paraPosition给定的位置。 * @return The position of the value, or -1 indicating not exists 返回值的位置,或-1表示不存在 */ int getElement(SequentialListPtr paraListPtr, int paraPosition) { // Step 1. Position check. if (paraPosition < 0) { printf("Invalid position: %d.\r\n", paraPosition); return -1; }//Of if if (paraPosition >= paraListPtr->actualLength) { printf("Cannot get element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength); return -1; }//Of if return paraListPtr->date[paraPosition]; }// Of locateElement /** * Clear elements in the list. 清除列表中的元素。 * @param paraListPtr The pointer to the list. param paraListPtr指向列表的指针。 * @return The position of the value, or -1 indicating not exists 返回值的位置,或-1表示不存在 */ void clearList(SequentialListPtr paraListPtr) { paraListPtr->actualLength = 0; }// Of clearList /** The entrance. */ int main() { sequentialInsertTest(); sequentialDeleteTest(); }// Of main
运行结果
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
---- sequentialInserTest 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: 12784704
The address of actualLength: 12784704
The address of date: 12784708
The address of actual date: 12784708
The address of second date: 12784712
--------------------------------
Process exited after 0.109 seconds with return value 0
请按任意键继续. . .