标签:数据结构
/*seq_list.h: sequence list header. *Author :ZMG. *E-mail : *Date :2017-8-20 *Version :0.1 *ALL rights reserved! *Code style try to refer to GNU linux kernel. :) */ #ifndef SEQ_LIST_H #define SEQ_LIST_H //include some essential headers. #include <stdio.h> #include <stdlib.h> //define sequence list structure type. #define list_size 1000 #define data_type int typedef struct { data_type list[list_size]; //also: data_type * list,then define int list_size. int length; }seq_list; //funtions declaration below void init_seq_list(seq_list *L); //initialize sequence list. void destroy_seq_list(seq_list *L); //destroy sequncent listist. void clear_seq_list(seq_list *L); //empty sequence list. void is_seq_list_empty(seq_list L); //sequence list is empty or not. int get_seq_list_length(seq_list L); //sequence list‘s length. int get_seq_list_element(seq_list L, int i, data_type *e); //get sequence list element. int locate_seq_list_element(seq_list L, data_type e); //locate sequence list element. int insert_seq_list_element(seq_list *L, int i, data_type e); //insert sequence list element. int delete_seq_list_element(seq_list *L, int i, data_type *e); //delete sequence list element. #endif // SEQ_LIST_H
seq_list.c
/*seq_list.c: sequence list implementation source file. *Author :ZMG. *E-mail : *Date :2017-8-20 *Version :0.1 *ALL rights reserved! *Code style try to refer to GNU linux kernel. :) */ #include "seq_list.h" //some functions implementation. void init_seq_list(seq_list *L) //initialize sequence list. { L->length = 0; } void destroy_seq_list(seq_list *L) //destroy sequncent listist. { free(L); //must have initialized the sequence list *L. } void clear_seq_list(seq_list *L) //empty sequence list. { L->length = 0; } void is_seq_list_empty(seq_list L) //sequence list is empty or not. { if (L.length == 0) { printf("sequence list is empty. \n"); } else if (L.length != 0) { printf("sequence list is not empty. \n"); } } int get_seq_list_length(seq_list L) //sequence list‘s length. { return L.length; } int get_seq_list_element(seq_list L, int i, data_type *e) //get sequence list element. { if (i < 1 || i > L.length) return -1; *e = L.list[i-1]; return 1; } int locate_seq_list_element(seq_list L, data_type e) //locate sequence list element. { int i = 0; for (i = 0; i < L.length; i++) { if (L.list[i] == e) {;} } return i-1; } int insert_seq_list_element(seq_list *L, int i, data_type e) //insert sequence list element. { int j = 0; if (i < 1 || i > L->length + 1) { printf ("insert illegal.exit!"); return -1; } else if (L->length >= list_size) { printf("the sequence list is full,can not insert element any more.exit! \n"); return -1; } else { for (j = L->length; j >= i; j--) L->list[j] = L->list[j-1]; //let list[i-1] = list[i] L->list[i-1] = e; L->length = L->length + 1; return 1; } } int delete_seq_list_element(seq_list *L, int i, data_type *e) //delete sequence list element. { int j = 0; if (L->length <= 0) { printf("sequence list is empty and cannot delete element any more.exit! \n"); return 0; } else if (i < 1 || i > L->length) { printf("the location you delete is not applicable.exit! \n"); return -1; } else { *e = L->list[i-1]; for (j = i; j <= L->length-1; j++) { L->list[j-1] = L->list[j]; //let list[i] = list[i-1] } L->length = L->length - 1; return 1; } }
main.c (for testing)
/*main : for testing. *Author :ZMG. *E-mail : *Date :2017-8-20 *Version :0.1 *ALL rights reserved! *Code style try to refer to GNU linux kernel. :) */ #include "seq_list.h" int main(void) { int i = 0; int j = 0; int flag = 0; data_type e = 0; int length = 0; seq_list A; seq_list B; init_seq_list(&A); init_seq_list(&B); //insert 1~10 to A for (i = 1; i <= 10; i++) { if (insert_seq_list_element(&A, i, i) == 0) { printf("the location you insert is illegal.exit! \n"); return -1; } } //insert some data into B for (i = 1, j = 1; j <= 6; i += 2, j++) { if (insert_seq_list_element(&B, j, i * 2) == 0) { printf("the location you insert is illegal.exit! \n"); return -1; } } printf("A: \n"); for (i = 1; i <= A.length; i++) { flag = get_seq_list_element(A, i, &e); if (flag == 1) printf("%4d", e); } printf("\n"); length = A.length; printf("A.length = %4d \n", length); printf("B: \n"); for (i = 1; i <= B.length; i++) { flag = get_seq_list_element(B, i, &e); if (flag == 1) printf("%4d", e); } printf("\n"); length = B.length; printf("B.length = %4d \n", length); system("pause"); }
本文出自 “DoIT” 博客,谢绝转载!
标签:数据结构
原文地址:http://zhouhappy88.blog.51cto.com/4161937/1957814