顺序表
This commit is contained in:
parent
a7ea423353
commit
37722e30cb
Binary file not shown.
3
main.c
3
main.c
@ -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;
|
||||||
}
|
}
|
66
sq_list.c
66
sq_list.c
@ -9,3 +9,69 @@ sq_list* init_sq_list(sq_list* name)
|
|||||||
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");
|
||||||
|
}
|
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user