/* Author: ACb0y Date: 2010-6-11 Description: sequen_list Version: 1.1 */ #include <stdio.h> #define MAXSIZE 1 << 10 typedef int elemtype; typedef struct { elemtype data[MAXSIZE]; int length; }sequen_list; /* FUNCTION sequen_list_init DESCRIPTION initialize the sequence list PAREMETERS p_list: the pointer of sequence list RETURNS void */ void sequen_list_init(sequen_list * p_list) { p_list->length = 0; } /* FUNCTION sequen_list_length DESCRIPTION get the length of sequence list PARAMETERS p_list: the pointer of sequence list RETURNS p_list->length: the length of sequence list */ int sequen_list_length(sequen_list * p_list) { return p_list->length; } /* FUNCTION sequen_list_get_element DESCRIPTION get the element by position PARAMETERS p_list: the pointer of sequence list position: the element' position RETURNS p_list->data[position]: the element itself */ elemtype sequen_list_get_element(sequen_list * p_list, int position) { return p_list->data[position]; } /* FUNCTION locate DESCRIPITION get the element's position in the sequence list. PARAMETERS p_list: the pointer of the sequence list value: the element RETURNS position: the element's position */ int locate(sequen_list * p_list, elemtype value) { int position = -1; int i; for (i = 0; i < p_list->length; ++i) { if (p_list->data[i] == value) { position = i; break; } } return position; } /* FUNCTION prior DESCRIPTION get the position of appoint element's prior element PARAMETERS p_list: the pointer of sequence list value: the appoint element RETURNS position - 1: the position of appoint element's prior element */ int prior(sequen_list * p_list, elemtype value) { int position; position = locate(p_list, value); if (position == -1) { printf("The value isn't exits./n"); } else if (position == 0) { printf("The data isn't exits prior element./n"); } return position - 1; } /* FUNCTION next DESCRIPTION get the position of appoint element's next element PARAMETERS p_list: the pointer of sequence list value: appoint element RETURNS position + 1: the position of appoint element's next element */ int next(sequen_list * p_list, elemtype value) { int position; position = locate(p_list, value); if (position == -1) { printf("The value isn't exits./n"); } else if (position == p_list->length) { printf("The data isn't exits next element./n"); position = -1; } return position + 1; } /* FUNCTION sequen_list_insert DESCRIPTION insert the element to the appoint position in sequence list PARAMETERS p_list: the pointer of sequence list value: the element prepare for insert position: the position to insert RETURNS 1: insert successful 0: insert failure */ int sequen_list_insert(sequen_list * p_list, elemtype value, int position) { if (position < 0 || position > p_list->length) { return 0; } else { int i; for (i = p_list->length; i >= position; --i) { p_list->data[i] = p_list->data[i - 1]; } p_list->data[position] = value; ++p_list->length; return 1; } } /* FUNCTION sequen_list_delete DESCRIPTION delete the appoint position's element PARAMETERS p_list: the pointer of sequence list position: the position the element which will be deleted RETURNS 1: delete the element successful 0: delete the element failure */ int sequen_list_delete(sequen_list * p_list, int position) { if (position < 0 || position > p_list->length) { return 0; } else { int i; for (i = position; i < p_list->length; i++) { p_list->data[i] = p_list->data[i + 1]; } --p_list->length; return 1; } } /* FUNCTION print_sequen_list DESCRIPTION print the all element in the sequence list PARAMETERS p_list: the pointer of sequence list RETURNS void */ void print_sequen_list(sequen_list * p_list) { int i; for (i = 0; i < p_list->length; i++) { printf("%d ", p_list->data[i]); } printf("/n"); } int main() { sequen_list list; sequen_list_init(&list); int i; for (i = 0; i < 10; i++) { sequen_list_insert(&list, i + 1, i); } print_sequen_list(&list); printf("The sequen_list's length = %d/n", sequen_list_length(&list)); printf("The element = %d/n", sequen_list_get_element(&list, 3)); sequen_list_delete(&list, 2); print_sequen_list(&list); printf("The sequen_list's length = %d/n", sequen_list_length(&list)); printf("The %d's next is %d/n", 5, list.data[next(&list, 5)]); printf("The %d's prior is %d/n", 5, list.data[prior(&list, 5)]); printf("The locate of %d is %d/n", 5, locate(&list, 5)); sequen_list_init(&list); printf("The length of sequence list is %d/n", sequen_list_length(&list)); return 0; }