-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq_linear_list.c
executable file
·67 lines (54 loc) · 1.04 KB
/
seq_linear_list.c
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
#include <stdio.h>
#define MAX 4
struct element {
int value;
};
struct seq_list {
struct element elements[MAX];
int size;
};
typedef struct seq_list seq_linear_list;
typedef struct element element;
void
initialize(seq_linear_list* list) {
list->size = MAX;
}
void
show(seq_linear_list* list){
for(int i=0; i < list->size; i++)
printf("%d ", list->elements[i].value);
printf("\n");
}
int
search(seq_linear_list *list, element e){
int i = 0;
while (i++ < list->size) {
if(e.value == list->elements[i].value) {
printf("It is the %d-th element.\n", i + 1);
return i;
}
}
printf("Not found!\n");
return -1;
}
void
insert(seq_linear_list *list, element e, int index){
if(index < 0 || index >= list->size) {
printf("Invalid index.\n");
return;
}
list->elements[index] = e;
}
int
main(void){
seq_linear_list list;
initialize(&list);
element e1;
e1.value = 9;
insert(&list, e1, 2);
e1.value = 7;
insert(&list, e1, 4);
show(&list);
search(&list, e1);
return 0;
}