#include <stdio.h>
#include <stdlib.h>
//顺序表
//#define MAXLEN 20
//1 定义节点类型 假设为int
typedef int Data_t;
//2 定义顺序表
struct list_t
{
int max_len; //存储顺序表的最大长度
int cnt; //存储当前表中节点个数
Data_t *data;//存储节点的数组动态分配长度
// data_t data[MAXLEN]; //静态分配数组长度
};
//定义对 顺序表的操作
//1 创建表
int create_list(struct list_t *head, int max_len)
{
//入参检查
if(head == NULL || max_len <= 0)
return -1; // 返回错误码
//填充 参数
head->max_len = max_len;
head->cnt = 0;
//申请空间
head->data = (Data_t *)malloc(max_len * sizeof(Data_t));
if(head->data == NULL)
{
printf("malloc err!\n");
return -2;
}
return 0;
}
//2.删除表
int del_list(struct list_t *head)
{
if(head == NULL) return -1;
if(head->data)//释放堆区内存
free(head->data);
head->data = NULL;
head->cnt = 0;
head->max_len = 0;
return 0;
}
//插入数据
int insert_list(struct list_t *head, int index, Data_t data)
{
if(head == NULL || head->data == NULL) return -1;
//判断位置能否插入
if( index > head->cnt )
{
printf("插入位置%d,不存在!\n", index);
return -2;
}
//判断满否
if(head->cnt == head->max_len)
{
printf("顺序表已满!!\n");
return -3;
}
//移开位置
//找到最后一个元素下标
//int i = head->cnt - 1;
for( int i = head->cnt -1 ; i >= index ;i--)
{
head->data[i+1] = head->data[i];
}
//写入数据
head->data[index] = data;
head->cnt ++;//更新元素个数
return 0;
}
int show_list(struct list_t *head)
{
if(head ==NULL || head->data == NULL) return -1;
for(int i=0; i< head->cnt; i++) printf("%d ",head->data[i]);
printf("\n");
return 0;
}
//获取某个下标的元素值
int get_list_data(struct list_t *head, int index, Data_t *data)
{
if(head == NULL || head->data == NULL) return -1;
//下标不存在
if(index >= head->cnt)
{
printf("%d号元素不存在!\n",index);
return -2;
}
*data = head->data[index];
return 0;
}
//根据元素的值 得到下标 找第一个值
int get_list_index(struct list_t *head, int *index, Data_t data)
{
if(head == NULL || head->data == NULL) return -1;
for(int i=0; i<head->cnt; i++)
{//找到值相同的下标
if(head->data[i] == data)
{
*index = i;
return 0;
}
}
}
//添加删除节点的函数del_node实现
int del_node(struct list_t *head, int index)
{
if (head == NULL || head->data == NULL) return -1;
if (index < 0 || index >= head->cnt)
{
printf("删除位置%d不存在!\n", index);
return -2;
}
for (int i = index; i < head->cnt-1; i++)
{
head->data[i] = head->data[i+1];
}
head->cnt--;
return 0;
}
//添加判空判满函数is_empty和is_full
int is_empty(struct list_t *head)
{
if (head == NULL) return -1;
return head->cnt == 0;
}
int is_full(struct list_t *head)
{
if (head == NULL) return -1;
return head->cnt == head->max_len;
}
//添加获取表长度函数get_length
int get_length(struct list_t *head)
{
if (head == NULL) return -1;
return head->cnt;
}
//添加拼接两个表的函数concat_list
struct list_t *concat_list(struct list_t *list1, struct list_t *list2)
{
if (list1 == NULL || list2 == NULL) return NULL;
int total_len = list1->cnt + list2->cnt;
struct list_t *new_list = (struct list_t *)malloc(sizeof(struct list_t));
if (new_list == NULL)
{
printf("malloc err!\n");
return NULL;
}
new_list->max_len = total_len;
new_list->cnt = total_len;
new_list->data = (Data_t *)malloc(total_len * sizeof(Data_t));
if (new_list->data == NULL)
{
printf("malloc err!\n");
free(new_list);
return NULL;
}
int i, j;
for (i = 0; i < list1->cnt; i++)
{
new_list->data[i] = list1->data[i];
}
for (j = 0; j < list2->cnt; j++)
{
new_list->data[i+j] = list2->data[j];
}
return new_list;
}
int main()
{
struct list_t L;
int ret = create_list(&L, 25);
if (ret == 0)
{
printf("表L创建成功!!\n");
printf("data=%p\n", L.data);
}
insert_list(&L, 0, 10);
insert_list(&L, 0, 20);
insert_list(&L, 0, 30);
insert_list(&L, 0, 40);
show_list(&L);
del_node(&L, 2); // 删除位置为2的节点
show_list(&L);
printf("表L是否为空:%d\n", is_empty(&L));
printf("表L是否已满:%d\n", is_full(&L));
Data_t dt;
if (get_list_data(&L, 3, &dt) == 0)
{
printf("3号元素的值为%d\n", dt);
}
int index;
if (get_list_index(&L, &index, 20) == 0)
{
printf("元素值为20的下标是%d\n", index);
}
struct list_t L2;
ret = create_list(&L2, 10);
if (ret == 0)
{
printf("表L2创建成功!!\n");
printf("data=%p\n", L2.data);
}
insert_list(&L2, 0, 50);
insert_list(&L2, 1, 60);
struct list_t *new_list = concat_list(&L, &L2);
if (new_list != NULL)
{
printf("拼接后的顺序表:");
show_list(new_list);
}
del_list(&L);
del_list(&L2);
if (new_list != NULL)
{
free(new_list->data);
free(new_list);
}
return 0;
}
顺序表: C语言程序模型
于 2023-08-10 19:57:46 首次发布