顺序表

This commit is contained in:
Jdhggg 2025-03-17 14:24:44 +08:00
parent a7ea423353
commit 37722e30cb
4 changed files with 71 additions and 4 deletions

3
main.c
View File

@ -6,7 +6,8 @@
int main(void) int main(void)
{ {
sq_list list;
init_sq_list(&list);
printf("Hello World!\n"); printf("Hello World!\n");
return 0; return 0;
} }

View File

@ -3,9 +3,75 @@
#include <string.h> #include <string.h>
#include "sq_list.h" #include "sq_list.h"
sq_list* init_sq_list(sq_list* name) sq_list* init_sq_list(sq_list *name)
{ {
name = (sq_list*)malloc(sizeof(sq_list)); name = (sq_list*)malloc(sizeof(sq_list));
memset(name, 0, sizeof(sq_list)); memset(name, 0, sizeof(sq_list));
return name; return name;
} }
// 删除
void delete_sq_list(sq_list* list,int pos)
{
if (pos < 0 || pos > list->length || list->length == MAX)
{
printf("位置错误\n");
return;
}
if (pos == list->length - 1)
list->data[pos] = 0;
else
{
for (int i = pos; i < list->length - 1; i++)
list->data[i] = list->data[i + 1];
list->data[list->length - 1] = 0;
}
list->length--;
}
// 替换
void replace_sq_list(sq_list* list, int pos, int value)
{
if (pos < 0 || pos > list->length || list->length >= MAX)
{
printf("位置错误\n");
return;
}
if (pos > list->length && pos <= MAX)
printf("不建议在此处插入");
list->data[pos] = value;
}
// 插入
void insert_sq_list(sq_list* list, int pos, int value)
{
if (pos < 0 || pos > list->length || list->length >= MAX)
{
printf("位置错误\n");
return;
}
if (pos == MAX)
printf("有损插入\n");
if 9pos > list->length && pos <= MAX)
printf("不建议在此处插入");
int i = list->length;
int flog = i;
for (i; i > pos; i--)
{
list->data[i + 1] = list->data[i];
}
if (flog + 1 > list->length)
list->length++;
}
// 打印
void print_sq_list(sq_list* list)
{
for (int i = 0; i < list->length; i++)
printf("%d ", list->data[i]);
printf("\n");
}

View File

@ -9,10 +9,10 @@ typedef struct sq_list
} sq_list; } sq_list;
// ³õʼ»¯ // ³õʼ»¯
sq_list* init_sq_list(sq_list* name); sq_list* init_sq_list(sq_list* list);
// ɾ³ý // ɾ³ý
void delete_sq_list(sq_list* list); void delete_sq_list(sq_list* list, int pos);
// Ìæ»» // Ìæ»»
void replace_sq_list(sq_list* list, int pos, int value); void replace_sq_list(sq_list* list, int pos, int value);