#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define DEBUG 1
#define UNION_LIST 0 // 0表示不使用这个函数
#define MERGE_LIST 1 // 1表示使用这个函数
// 定义线性表结构
typedef struct
{
int *data; // 指向存储数据的数组
int capacity; // 数组的容量
int length; // 当前线性表的长度
} List;
// 初始化线性表
int InitList(List *list)
{
list->length = 0; // 将线性表长度初始化为0
list->capacity = MAX_NUM; // 设置线性表的容量为预定义的最大值
list->data = malloc(sizeof(int) * (list->capacity + 1)); // 为线性表的数组分配内存空间
if (list->data == NULL)
{
return ERROR; // 内存分配失败,返回错误
}
return OK;
}
// 在线性表的指定位置插入元素
int ListInsert(List *list, int i, int e)
{
if (list->length >= list->capacity)
{
int NewCapacity = list->capacity * 2; // 若线性表已满,则将线性表容量扩大为原来的两倍
int *NewData = realloc(list->data, sizeof(int) * (NewCapacity + 1)); // 重新分配内存空间
if (!NewData)
return ERROR; // 内存分配失败,返回错误
list->data = NewData;
list->capacity = NewCapacity;
}
if (i < 1 || i > list->length + 1) // 若插入位置不合法,则返回错误
{
return ERROR;
}
for (int j = list->length; j >= i; j--) // 将插入位置之后的元素后移一位
{
list->data[j + 1] = list->data[j];
}
list->data[i] = e; // 在指定位置插入元素
list->length++; // 线性表长度加一
return OK;
}
// 获取线性表指定位置的元素值
void GetElem(List *list, int i, int *x)
{
if (i < 1 || i > list->length) // 若访问位置不合法,则返回默认值0或其他处理方式
{
*x = 0;
}
else
{
*x = list->data[i]; // 获取指定位置的元素值
}
}
// 判断线性表是否为空
int ListEmpty(List *list)
{
if (list->length == 0)
{
return TRUE; // 若线性表长度为0,则为空
}
return FALSE;
}
// 判断两个元素是否相等
int equal_num(int a, int b)
{
if (a == b)
return TRUE; // 若两个元素相等,则返回真
return FALSE;
};
// 在线性表中查找指定元素的位置
int LocateElem(List *list, int e)
{
// 返回线性表中第一个与e相等的元素的位置,若不存在则返回0
for (int i = 1; i <= list->length; i++) // 遍历线性表中的元素
{
if (equal_num(list->data[i], e)) // 若找到与e相等的元素,则返回位置
{
return i;
}
}
return 0; // 找不到返回0
}
// 删除线性表中指定位置的元素
void delete_element(List *list, int index)
{
if (index < 1 || index > list->length) // 若删除位置不合法,则直接返回
{
return;
}
for (int i = index; i < list->length; i++) // 将删除位置之后的元素前移一位
{
list->data[i] = list->data[i + 1];
}
list->length--; // 线性表长度减一
}
// 将线性表Lb中的元素合并到La中,去除重复元素
void union_linear_tables(List *La, List *Lb)
{
int Lb_len = Lb->length; // 获取线性表Lb的长度
int e = 0;
int num = La->length;
for (int i = 1; i <= Lb_len; i++) // 遍历线性表Lb中的元素
{
GetElem(Lb, i, &e);
if (LocateElem(La, e) == 0) // 若La中不存在与Lb中元素相等的元素
{
ListInsert(La, num + 1, e); // 在La的末尾插入元素e
num++;
}
}
}
// 打印线性表的元素
void PrintList(List *list)
{
for (int i = 1; i <= list->length; i++) // 遍历线性表中的元素
{
printf("%d ", list->data[i]); // 打印元素值
}
printf("\n");
}
// 合并两个有序线性表La和Lb为一个有序线性表Lc
void MergeList(List *La, List *Lb, List *Lc)
{
InitList(Lc); // 初始化线性表Lc
int i = 1, j = 1, k = 0; // 初始化计数器
int La_len = La->length;
int Lb_len = Lb->length;
int ai, bj;
while (i <= La_len && j <= Lb_len) // 合并La和Lb中的元素到Lc中
{
GetElem(La, i, &ai);
GetElem(Lb, j, &bj);
if (ai <= bj)
{
ListInsert(Lc, k + 1, ai); // 将La中的元素插入到Lc中
i++;
}
else
{
ListInsert(Lc, k + 1, bj); // 将Lb中的元素插入到Lc中
j++;
}
k++;
}
while (i <= La_len)
{
GetElem(La, i, &ai);
ListInsert(Lc, k + 1, ai); // 将La中剩余的元素插入到Lc中
i++;
k++;
}
while (j <= Lb_len)
{
GetElem(Lb, j, &bj);
ListInsert(Lc, k + 1, bj); // 将Lb中剩余的元素插入到Lc中
j++;
k++;
}
}
void ClearList(List *list) {
list->length = 0; //设置数组长度为0
}
// 销毁线性表
int DestroyList(List *list)
{
if (list != NULL)
{
if (list->data != NULL)
{
free(list->data); // 释放线性表的数组内存空间
list->data = NULL; // 将指针设置为空指针
return OK;
}
list->length = 0; // 释放后将线性表长度设置为0
}
return FALSE;
}
int main(void)
{
List lista;
List listb;
List listc;
if (InitList(&lista) && InitList(&listb))
{ // 如果初始化成功
ListInsert(&lista, 1, 3);
ListInsert(&lista, 2, 5);
ListInsert(&lista, 3, 8);
ListInsert(&lista, 4, 11);
#if DEBUG
printf("ListA Length: %d\n", lista.length);
printf("ListA Elements: ");
PrintList(&lista);
#endif
ListInsert(&listb, 1, 2);
ListInsert(&listb, 2, 6);
ListInsert(&listb, 3, 8);
ListInsert(&listb, 4, 9);
ListInsert(&listb, 5, 7);
ListInsert(&listb, 6, 11);
ListInsert(&listb, 7, 15);
ListInsert(&listb, 8, 20);
#if DEBUG
printf("ListB Length: %d\n", listb.length);
printf("ListB Elements: ");
PrintList(&listb);
#endif
#if MERGE_LIST
MergeList(&lista, &listb, &listc); // 合并La和Lb为Lc
printf("Merged List Elements: ");
PrintList(&listc);
#endif
#if UNION_LIST
printf("Union List Elements: ");
union_linear_tables(&lista, &listb); // 将Lb中的元素合并到La中
PrintList(&lista);
#endif
DestroyList(&lista);
DestroyList(&listb);
DestroyList(&listc);
}
else
{
printf("线性表初始化失败。。。");
}
return 0;
}
线性表-顺序表-C实现
最新推荐文章于 2024-05-02 18:02:39 发布