标签:more reserve def cat must ann exit div ret
seq_list.h
1 /*seq_list.h: sequence list header.
2 *Author :ZMG.
3 *E-mail :
4 *Date :2017-8-20
5 *Version :0.1
6
7 *ALL rights reserved!
8 *Code style try to refer to GNU linux kernel. :)
9 */
10
11
12 #ifndef SEQ_LIST_H
13 #define SEQ_LIST_H
14
15
16 //include some essential headers.
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 //define sequence list structure type.
21 #define list_size 1000
22 #define data_type int
23
24 typedef struct {
25 data_type list[list_size]; //also: data_type * list,then define int list_size.
26 int length;
27 }seq_list;
28
29
30 //funtions declaration below
31 void init_seq_list(seq_list *L); //initialize sequence list.
32 void destroy_seq_list(seq_list *L); //destroy sequncent listist.
33 void clear_seq_list(seq_list *L); //empty sequence list.
34 void is_seq_list_empty(seq_list L); //sequence list is empty or not.
35 int get_seq_list_length(seq_list L); //sequence list‘s length.
36 int get_seq_list_element(seq_list L, int i, data_type *e); //get sequence list element.
37 int locate_seq_list_element(seq_list L, data_type e); //locate sequence list element.
38 int insert_seq_list_element(seq_list *L, int i, data_type e); //insert sequence list element.
39 int delete_seq_list_element(seq_list *L, int i, data_type *e); //delete sequence list element.
40
41
42 #endif // SEQ_LIST_H
seq_list.c
1 /*seq_list.c: sequence list implementation source file.
2 *Author :ZMG.
3 *E-mail :
4 *Date :2017-8-20
5 *Version :0.1
6
7 *ALL rights reserved!
8 *Code style try to refer to GNU linux kernel. :)
9 */
10
11
12 #include "seq_list.h"
13
14 //some functions implementation.
15 void init_seq_list(seq_list *L) //initialize sequence list.
16 {
17 L->length = 0;
18 }
19
20 void destroy_seq_list(seq_list *L) //destroy sequncent listist.
21 {
22 free(L); //must have initialized the sequence list *L.
23 }
24
25 void clear_seq_list(seq_list *L) //empty sequence list.
26 {
27 L->length = 0;
28 }
29
30 void is_seq_list_empty(seq_list L) //sequence list is empty or not.
31 {
32 if (L.length == 0) {
33 printf("sequence list is empty. \n");
34 } else if (L.length != 0) {
35 printf("sequence list is not empty. \n");
36 }
37 }
38
39 int get_seq_list_length(seq_list L) //sequence list‘s length.
40 {
41 return L.length;
42 }
43
44 int get_seq_list_element(seq_list L, int i, data_type *e) //get sequence list element.
45 {
46 if (i < 1 || i > L.length)
47 return -1;
48 *e = L.list[i-1];
49
50 return 1;
51
52
53 }
54
55 int locate_seq_list_element(seq_list L, data_type e) //locate sequence list element.
56 {
57 int i = 0;
58
59 for (i = 0; i < L.length; i++) {
60 if (L.list[i] == e) {;}
61 }
62
63 return i-1;
64
65 }
66
67 int insert_seq_list_element(seq_list *L, int i, data_type e) //insert sequence list element.
68 {
69 int j = 0;
70
71 if (i < 1 || i > L->length + 1) {
72 printf ("insert illegal.exit!");
73 return -1;
74 } else if (L->length >= list_size) {
75 printf("the sequence list is full,can not insert element any more.exit! \n");
76 return -1;
77 } else {
78 for (j = L->length; j >= i; j--)
79 L->list[j] = L->list[j-1]; //let list[i-1] = list[i]
80 L->list[i-1] = e;
81 L->length = L->length + 1;
82
83 return 1;
84 }
85 }
86
87 int delete_seq_list_element(seq_list *L, int i, data_type *e) //delete sequence list element.
88 {
89 int j = 0;
90
91 if (L->length <= 0) {
92 printf("sequence list is empty and cannot delete element any more.exit! \n");
93 return 0;
94 } else if (i < 1 || i > L->length) {
95 printf("the location you delete is not applicable.exit! \n");
96 return -1;
97 } else {
98 *e = L->list[i-1];
99 for (j = i; j <= L->length-1; j++) {
100 L->list[j-1] = L->list[j]; //let list[i] = list[i-1]
101 }
102 L->length = L->length - 1;
103
104 return 1;
105 }
106 }
main.c
1 /*main : for testing
2 *Author :ZMG.
3 *E-mail :
4 *Date :2017-8-20
5 *Version :0.1
6
7 *ALL rights reserved!
8 *Code style try to refer to GNU linux kernel. :)
9 */
10
11
12 #include "seq_list.h"
13
14 int main(void)
15 {
16 int i = 0;
17 int j = 0;
18 int flag = 0;
19 data_type e = 0;
20 int length = 0;
21
22 seq_list A;
23 seq_list B;
24
25 init_seq_list(&A);
26 init_seq_list(&B);
27
28 //insert 1~10 to A
29 for (i = 1; i <= 10; i++) {
30 if (insert_seq_list_element(&A,i,i) == 0) {
31 printf("the location you insert is illegal.exit! \n");
32 return -1;
33 }
34 }
35
36 //insert some data into B
37 for (i = 1, j = 1; j <= 6; i +=2, j++) {
38 if (insert_seq_list_element(&B,j,i*2) == 0) {
39 printf("the location you insert is illegal.exit! \n");
40 return -1;
41 }
42 }
43
44 printf("A: \n");
45 for (i = 1; i <= A.length; i++) {
46 flag = get_seq_list_element(A,i,&e);
47
48 if (flag == 1)
49 printf("%4d", e);
50 }
51
52 printf("\n");
53
54 length = A.length;
55 printf("A.length = %4d \n", length);
56
57 printf("B: \n");
58 for (i = 1; i <= B.length; i++) {
59 flag = get_seq_list_element(B,i,&e);
60
61 if (flag == 1)
62 printf("%4d", e);
63 }
64
65 printf("\n");
66
67 length = B.length;
68 printf("B.length = %4d \n", length);
69
70 }
标签:more reserve def cat must ann exit div ret
原文地址:http://www.cnblogs.com/zhouhappy88/p/7674968.html