数据类型及API声明
线性表的顺序存储是指各元素按顺序依次排列存储在一段连续内存的数据结构。
1. //线性表数据类型 2. typedef void LinearList; 3. 4. //线性表结点数据类型 5. typedef void LinearListNode; 6. 7. //线性表的表头数据类型 8. typedef struct _LinearList _LinearListHead; 9. 10. //创建线性表并返回线性表句柄 11. LinearList* LinearList_Create(int length); 12. 13. //销毁线性表(释放内存) 14. void LinearList_Destroy(LinearList* lList); 15. 16. //清空线性表(不释放内存) 17. void LinearList_Clear(LinearList* lList); 18. 19. //返回线性表长度(元素个数) 20. int LinearList_Length(LinearList* lList); 21. 22. //插入元素 23. int LinearList_Insert(LinearList* lList, LinearListNode* node, int pos); 24. 25. //返回pos处的元素 26. LinearListNode* LinearList_Get(LinearList* lList, int pos); 27. 28. //删除一个结点并返回结点值 29. LinearListNode* LinearList_Delete(LinearList* lList, int pos);
线性表的API实现
定义一个表头结构体
- 表头含义:该表头存储了线性表的最大长度,当前长度(元素个数),元素内存起始地址。
1. struct _LinearList //这是一个线性表头 2. { 3. int length; //总长度 4. int cur_length; //当前长度 5. int* node; //线性表的元素存放在这个内存中 6. };
创建线性表并返回线性表句柄
- 函数功能:创建一个线性表,根据传入的长度分配内存,并返回线性表的句柄
- 函数参数:length 线性表长度
- 函数返回:创建好的线性表句柄
1. LinearList* LinearList_Create(int length) 2. { 3. _LinearListHead* head; 4. 5. head = (_LinearListHead*)malloc(sizeof(_LinearListHead)); 6. if (head == NULL) 7. { 8. printf("head = (_LinearListHead*)malloc(sizeof(_LinearListHead)) err\n"); 9. return NULL; 10. } 11. memset(head, 0, sizeof(_LinearListHead)); 12. 13. head->length = length; 14. head->cur_length = 0; 15. 16. head->node = (int*)malloc(sizeof(int) * length); 17. if (head->node == NULL) 18. { 19. printf("head->node = (int*)malloc(sizeof(int) * length) err\n"); 20. return NULL; 21. } 22. 23. return head; 24. }
销毁线性表
- 函数功能:销毁线性表并释放所有内存
- 函数参数:lList 线性表的句柄
- 函数返回:无
1. void LinearList_Destroy(LinearList* lList) 2. { 3. _LinearListHead* head = NULL; 4. 5. if (lList == NULL) 6. { 7. printf("LinearList* lList = NULL\n"); 8. return; 9. } 10. 11. head = (_LinearListHead*)lList; 12. if (head->node != NULL) 13. { 14. free(head->node); 15. } 16. 17. free(head); 18. }
清空线性表
- 函数功能:清空线性表仅仅把当前长度置为0,不会释放内存,相当于把线性表恢复到刚创建的状态
- 函数参数:lList 线性表句柄
- 函数返回:无
1. void LinearList_Clear(LinearList* lList) 2. { 3. _LinearListHead* head = NULL; 4. 5. if (lList == NULL) 6. { 7. printf("LinearList* lList = NULL\n"); 8. return; 9. } 10. 11. head = (_LinearListHead*)lList; 12. 13. memset(head->node, 0, sizeof(int) * head->length); 14. head->cur_length = 0; 15. }
返回线性表当前长度
- 函数功能:返回线性表中当前存储的元素个数
- 函数参数:lList 链表句柄
- 函数返回:链表元素个数
1. int LinearList_Length(LinearList* lList) 2. { 3. int ret = 0; 4. _LinearListHead* head = NULL; 5. 6. if (lList == NULL) 7. { 8. ret = -1; 9. printf("LinearList* lList = NULL err: %d\n", ret); 10. return ret; 11. } 12. 13. head = (_LinearListHead*)lList; 14. 15. return head->cur_length; 16. }
插入元素
- 函数功能:在给定位置插入一个元素
- 函数参数:lList 线性表句柄 node 要插入的元素 pos插入位置
- 函数返回:插入成功返回0
1. int LinearList_Insert(LinearList* lList, LinearListNode* node, int pos) 2. { 3. int ret = 0, i = 0; 4. _LinearListHead* head = NULL; 5. 6. if ((lList == NULL) || (node == NULL) || (pos < 0)) 7. { 8. ret = -1; 9. printf("(lList == NULL) || (node == NULL) || (pos < 0) err: %d\n", ret); 10. return ret; 11. } 12. 13. head = (_LinearListHead*)lList; 14. 15. if (head->cur_length >= head->length) 16. { 17. ret = head->length; 18. printf("The linear table is full\n"); 19. return ret; 20. } 21. 22. if (pos >= head->cur_length) 23. { 24. head->node[head->cur_length] = (int)node; 25. } 26. 27. for (i = head->cur_length; i > pos; i--) 28. { 29. head->node[i] = head->node[i - 1]; 30. } 31. 32. 33. head->node[i] = *(int*)node; 34. head->cur_length++; 35. return 0; 36. }
获取一个元素
- 函数功能:返回给定位置处的元素
- 函数参数:lList 线性表句柄 pos 要返回第几个元素
- 函数返回:要返回的元素
1. LinearListNode* LinearList_Get(LinearList* lList, int pos) 2. { 3. _LinearListHead* head = NULL; 4. 5. if ((lList == NULL) || (pos < 0)) 6. { 7. printf("(lList == NULL) || (pos < 0) err\n"); 8. return NULL; 9. } 10. 11. head = (_LinearListHead*)lList; 12. 13. return (LinearListNode*)head->node[pos]; 14. }
删除元素
- 函数功能:删除一个元素并返回该元素
- 函数参数:lList 线性表句柄 pos 删除元素的位置
- 函数返回:被删除的元素
1. LinearListNode* LinearList_Delete(LinearList* lList, int pos) 2. { 3. int i = 0; 4. _LinearListHead* head = NULL; 5. LinearListNode* temp_node = NULL; 6. 7. if ((lList == NULL) || (pos < 0)) 8. { 9. printf("(lList == NULL) || (pos < 0) err\n"); 10. return NULL; 11. } 12. 13. head = (_LinearListHead*)lList; 14. temp_node = (LinearListNode*)head->node[pos]; 15. 16. for (i = pos + 1; i < head->cur_length; i++) 17. { 18. head->node[i - 1] = head->node[i]; 19. } 20. head->cur_length--; 21. 22. return temp_node; 23. }
测试函数
1. #include "LinearList.h" 2. 3. int main() 4. { 5. int i = 0; 6. LinearList* mList = NULL; 7. LinearListNode* pNode = NULL; 8. 9. mList = LinearList_Create(10); 10. 11. for (int i = 0; i < 10; i++) 12. { 13. int ret = 0; 14. int temp = i + 1; 15. ret = LinearList_Insert(mList, (LinearListNode*)&temp, 0); 16. if (ret != 0) 17. { 18. printf("LinearList_Insert() err: %d\n", ret); 19. return ret; 20. } 21. } 22. 23. for (i = 0; i < 10; i++) 24. { 25. printf("第%d个元素值:%d\n", i, (int)LinearList_Get(mList, i)); 26. } 27. 28. pNode = LinearList_Delete(mList, 3); 29. printf("被删除元素:%d\n", (int)pNode); 30. printf("当前线性表长度:%d\n", LinearList_Length(mList)); 31. 32. i = 12; 33. i = LinearList_Insert(mList, (LinearListNode*)&i, 3); 34. if (i != 0) 35. { 36. printf("LinearList_Insert() err\n"); 37. } 38. 39. for (i = 0; i < 10; i++) 40. { 41. printf("第%d个元素值:%d\n", i, (int)LinearList_Get(mList, i)); 42. } 43. 44. LinearList_Destroy(mList); 45. 46. system("pause"); 47. return 0; 48. }
具体的代码资源已经上传在这个链接