顺序表的增删改查

顺序表的增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#define N 10
typedef struct {
int* arr;
int arr_length;
int length_max;
} list;
bool init_list(list* list1)
{

list1->arr = (int*)malloc(N * sizeof(int));
if (list1->arr == NULL)
{
return false;
}
list1->arr_length = 0;
list1->length_max = N;
return true;
}
bool add_list(list* list1, int order, int num)
{
if (order<1 || order>list1->arr_length || list1->arr_length == list1->length_max)
{
return false;
}
for (int i = list1->arr_length; i >= order; i--)
{
list1->arr[i] = list1->arr[i - 1];
}
list1->arr[order - 1] = num;
list1->arr_length++;
return true;
}
bool subtract_list(list* list1, int order, int* num)
{
if (order<1 || order>list1->arr_length || list1->arr_length == 0)
{
return false;
}
*num = list1->arr[order - 1];
for (int i = order; i < list1->arr_length; i++)
{
list1->arr[i - 1] = list1->arr[i];
}
list1->arr_length--;
printf("%d", *num);
return true;
}
bool IncreaseSize(list* list1, int len)
{
int* p1 = list1->arr;
list1->arr = (int*)malloc((len + list1->length_max)*sizeof(int));
if (list1->arr== NULL)
{
return false;
}
for (int i = 0; i < list1->arr_length; i++)
{
list1->arr[i] = p1[i];
}
list1->length_max += len;
free(p1);
return true;
}
int ValueSearch(list* list1, int value)
{
for (int i = 0; i < list1->arr_length; i++)
{
if (list1->arr[i] == value)
return i + 1;
}
return -1;
}
int OrderSearch(list* list1, int order)
{
if (order<1 || order>list1->arr_length)
{
return -1;
}
return list1->arr[order - 1];
}