顺序表是最常用的数据结构.
1. 代码 (2022 版)
先上代码, 再说废话.
#include <stdio.h>
#include <malloc.h>
#define LIST_MAX_LENGTH 10
/**
* Linear list of integers. The key is data.
*/
typedef struct SequentialList {
int actualLength;
int data[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->data[i]);
}// Of for i
printf("\r\n");
}// Of outputList
/**
* Output the memeory 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 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]);
}// 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 = (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 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 the first position.
* @param paraValue The value to be inserted.
*/
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
// Step 1. Space check.
if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
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->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
/**
* Test the insert function.
*/
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 beyond 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("---- sequentialInsertTest 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 paraPosition The position, e.g., 0 stands for inserting at the first position.
* @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->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->actualLength; i ++) {
paraListPtr->data[i] = paraListPtr->data[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 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;
}// Of if
}//Of for i
return -1;
}// Of locateElement
/**
* Get an element in the list.
* @param paraListPtr The pointer to the list.
* @param paraPosition The given position.
* @return The position of the value, or -1 indicating not exists
*/
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->data[paraPosition];
}// Of locateElement
/**
* Clear elements in the list.
* @param paraListPtr The pointer to the list.
* @return The position of the value, or -1 indicating not exists
*/
void clearList(SequentialListPtr paraListPtr) {
paraListPtr->actualLength = 0;
}// Of clearList
/**
The entrance.
*/
void main() {
sequentialInsertTest();
sequentialDeleteTest();
}// Of main
2. 运行结果
---- 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: 10163840
The address of actualLength: 10163840
The address of data: 10163844
The address of actual data: 10163844
The address of second data: 10163848
Press any key to continue
3. 代码说明
- 用 typedef struct SequentialList 定义一个新的结构体, 即数据结构的数据部分. 这里使用了指向结构体的指针. 用 C 语言描述, 没办法避开指针, 还不如面对它! 简化起见, 这里的结构体具有固定的空间.
- outputList 用于打印该顺序表, 方便结果观察.
- outputMemory 可以观察数据在内存中的具体位置. 有了地址, 就知道了系统内部运行的机制. 这也是 C 语言的一个具大优点. 因此, C 语言被称为高级语言与汇编语言之间的“中级语言”. 要想成为高手, 就应该玩转内存. 要想成为东方不败, 就必须搞懂指针.
- sequentialListInit 用于初始化顺序表, 它具有很高的复用性.
- sequentialListInsert 是第一个重要的功能. 其中需要进行越界检查等.
- sequentialInsertTest 是 sequentialListInsert 的单元测试函数. 一个合格的程序员必须做完美的单元测试, 千万不要嫌麻烦. 这里多花 1 小时, 找 bug 的时间就会节约 10 小时.
- sequentialListDelete 是第二个重要的功能.
- sequentialDeleteTest 与 sequentialListDelete 配套.
- main() 里面的代码量越少越好.
4. 作业
- 抄代码并运行. 不包括 locateElement, getElement, clearList.
- 自己实现 locateElement, getElement, clearList 三个函数并与我的代码比较.
- 实现并测试书上其它相关函数如 PriorElement.
- 找出我代码的问题, 并在本贴下方留言.
- 将自己的工作写成贴子发布到 CSDN.
附 1: 2021 版代码
#include <stdio.h>
#include <malloc.h>
#define LIST_MAX_LENGTH 100
/**
* The list contains only int values to keep it as simple as possible.
*/
struct SequentialList
{
int maxLength;
int actualLength;
int data[LIST_MAX_LENGTH];
} SequentialList;
/**
* Insert an element into the linear list. The new list should be sorted in ascendant order.
*/
void SequentialListInsert(struct SequentialList* paraListPtr, int paraValue)
{
int i = 0;
int tempPosition = paraListPtr->actualLength;
//Space check.
if(paraListPtr->actualLength == paraListPtr->maxLength)
{
printf("List full, cannot SequentialListInsert element.");
return;
}//Of if
//Find the position.
for(i = 0; i < paraListPtr->actualLength; i ++)
{
if (paraListPtr->data[i] > paraValue)
{
tempPosition = i;
break;
}//Of if
}//Of for i
//Move the remaining part.
for(i = paraListPtr->actualLength; i > tempPosition; i --)
{
paraListPtr->data[i] = paraListPtr->data[i - 1];
}//Of for i
//Now SequentialListInsert.
paraListPtr->data[tempPosition] = paraValue;
//Update the length.
paraListPtr->actualLength ++;
}//Of SequentialListInsert
/**
* Output the list.
*/
void outputList(struct SequentialList *paraList)
{
for(int i = 0; i < paraList->actualLength; i ++)
{
printf("%d ", paraList->data[i]);
}//Of for i
printf("\r\n");
}//Of outputList
/**
* Output the memeory for the list.
*/
void outputMemory(struct SequentialList* paraListPtr)
{
printf("The address of the structure: %ld\r\n", paraListPtr);
printf("The address of maxLength: %ld\r\n", ¶ListPtr->maxLength);
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]);
}//Of output
/**
* Test functions.
*/
void main()
{
int tempArray[5] = {3, 5, 2, 7, 4};
printf("Hello, world!\r\n");
struct SequentialList tempList1;
struct SequentialList tempList2;
struct SequentialList tempList3;
tempList1.maxLength = 100;
tempList1.actualLength = 0;
for(int i = 0; i < 5; i ++)
{
printf("Inserting %d.\r\n", tempArray[i]);
SequentialListInsert(&tempList1, tempArray[i]);
outputList(&tempList1);
}//Of for i
outputMemory(&tempList1);
printf("Now for other two lists.\r\n");
outputMemory(&tempList2);
outputMemory(&tempList3);
}//Of main
附 2. 2021 年学生的程序
完整的抽象数据类型, 完整的函数.
选用迪达尔的程序, 未作修改.
#include "stdio.h"
#include "windows.h"
#include "stdlib.h"
#define MAXSIZE 25 //顺序表最大长度
/*定义顺序表*/
typedef struct
{
int data[MAXSIZE];
int length;
} SeqList;
/*初始化顺序表*/
void InitList(SeqList *l)
{
l->length = 0;
}
/*建立顺序表*/
int CreatList(SeqList *l, int a[], int n)
{
if (n > MAXSIZE)
{
printf("空间不够,无法建立顺序表。\n");
return 0;
}
for (int d = 0; d < n; d++)
{
l->data[d] = a[d];
}
l->length = n;
return 1;
}
/*判空操作*/
int Empty(SeqList *l)
{
if (l->length == 0)
return 1;
else
return 0;
}
/*求顺序表长度*/
int Length(SeqList *l)
{
return l->length;
}
/*遍历操作*/
void PrintList(SeqList *l)
{
for (int i = 0; i < l->length; i++)
printf("%d ", (l->data[i]));
}
/*按值查找*/
int Locate(SeqList *l,int x)
{
for (int i = 0; i < l->length; i++)
{
if (l->data[i] == x)
{
return i + 1;
}
return 0;
}
return 1;
}
/*按位查找*/
int Get(SeqList *l, int x,int *ptr)
{
//若查找成功,则通过指针参数ptr返回值
if ( x <1 || x>l->length)
{
printf("查找位置非法,查找错误\n");
return 0;
}
else
{
*ptr = l->data[x];
return 1;
}
}
/*插入操作*/
int Insert(SeqList *l, int i, int x)
{
if (l->length > MAXSIZE)
{
printf("上溢错误!");
return 0;
}
if (i<1 || i>l->length)
{
printf("插入位置错误!");
return 0;
}
for (int k = l->length; k > i; k--)
{
l->data[k] = l->data[k - 1];
}
l->data[i] = x;
l->length++;
return 1;
}
/*删除操作*/
int Delete(SeqList *l, int i, int *ptr)
{
if (l->length == 0)
{
printf("发生下溢错误,即将要访问顺序表之前的地址.\n");
return 0;
}
if (i > l->length || i < 1)
{
printf("删除位置错误!\n");
return 0;
}
*ptr = l->data[i - 1];//把要删除的数据返回
for (int j = i; j < l->length; j++)
{
l->data[j - 1] = l->data[j];
}
l->length--;
return 1;
}
/*修改操作*/
int Modify(SeqList *l, int i, int x)
{
if (i > l->length || i < 1)
{
printf("位置错误!\n");
return 0;
}
l->data[i] = x;
return 1;
}
int main()
{
int a[5] = {5,15,25,35,45 };
int i, x;
SeqList list1;
InitList(&list1); //初始化顺序表
if (Empty(&list1))
{
printf("初始化顺序表成功!\n");
}
printf("给顺序表赋值:5 15 25 35 45\n遍历并输出顺序表:\n");
CreatList(&list1,a,5 ); //建立一个长度为5的线性表
PrintList(&list1); //遍历输出此顺序表
printf("\n在第五位后插入一个500:\n");
Insert(&list1, 5, 500);
PrintList(&list1);
if (Modify(&list1, 3, 250) == 1)
{
printf("\n把第三位改成250\n");
PrintList(&list1);
}
if (Delete(&list1, 6, &x) == 1)
{
printf("\n把倒数第一位删除,删除的值是%d\n",x);
PrintList(&list1);
}
system("pause");
return 0;
}
欢迎留言!