-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseqlist.c
More file actions
119 lines (95 loc) · 2.09 KB
/
seqlist.c
File metadata and controls
119 lines (95 loc) · 2.09 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* 顺序表
*
*/
#include <string.h>
#include <stdio.h>
#define MAX_SIZE 50
typedef int seqdata;
struct seqlist {
seqdata array[MAX_SIZE];
int count;
};
void seqlist_init(struct seqlist *list)
{
memset(list->array, 0, sizeof(list->array));
list->count = 0;
}
int seqlist_add(struct seqlist *list, seqdata value)
{
if (list->count >= MAX_SIZE)
return -1;
list->array[list->count] = value;
list->count++;
return 0;
}
int seqlist_insert(struct seqlist *list, int index, seqdata value)
{
int i, j;
if (list->count >= MAX_SIZE)
return -1;
if (index >= list->count)
return -1;
/* move valid data form [index] ~ [list->count -1] */
for (i = list->count - 1; i >= index; i--) {
list->array[i + 1] = list->array[i];
}
list->array[index] = value;
list->count++;
return 0;
}
int seqlist_remove(struct seqlist *list, int index)
{
int i;
if (index >= list->count)
return -1;
/* move valid data index form [index+1] ~ [list->count -1] */
for (i = index + 1; i < list->count; i++) {
list->array[i - 1] = list->array[i];
}
list->count--;
return 0;
}
int seqlist_get_index(struct seqlist *list, seqdata value)
{
int i;
for (i = 0; i < list->count; i++) {
if (list->array[i] == value)
break;
}
if (i >= list->count)
return -1;
return i;
}
seqdata seqlist_get_value(struct seqlist *list, int index)
{
if (index >= list->count)
return -1;
return list->array[index];
}
void print_seqlist(struct seqlist *list)
{
int i;
printf("Seqlist number:%d\n", list->count);
for (i = 0; i < list->count; i++)
printf("Data[%d]:%d, ", i, list->array[i]);
printf("\n");
}
int main(int argc, char *argv[])
{
struct seqlist seqlist;
seqlist_init(&seqlist);
seqlist_add(&seqlist, 1);
seqlist_add(&seqlist, 2);
seqlist_add(&seqlist, 3);
seqlist_add(&seqlist, 4);
seqlist_add(&seqlist, 5);
printf("Init seqlite:\n");
print_seqlist(&seqlist);
printf("Insert 0 to seqlist index 2:\n");
seqlist_insert(&seqlist, 2, 0);
print_seqlist(&seqlist);
printf("Data 5 index:%d\n", seqlist_get_index(&seqlist, 5));
printf("Index 4 data:%d\n", seqlist_get_value(&seqlist, 4));
return 0;
}