最近开始准备软件设计师考试,所以把数据结构的知识梳理一遍。
首先线性表的顺序表示。
//list.c #include<stdio.h> #include<stdlib.h> #define ERROR -1 #define OVERFLOW 2 #define TRUE 1 #define FALSE 0 #define OK 1 #define LIST_INIT_SIZE 100 #define LIST_INCREMENT 10 typedef struct{ int * elem; int length; int listsize; }sq_list; int init_list_sq(sq_list *sl); void destroy_list_sq(sq_list * sl); int clear_list(sq_list * sl); int list_empty(sq_list sl); int list_length(sq_list sl); void get_elem(sq_list sl, int index, int * elem); int locate_elem(sq_list sl, int elem); int prior_elem(sq_list sl, int curelem, int *priorelem); int next_elem(sq_list sl, int curelem, int *nextelem); int insert_elem(sq_list *sl, int index, int elem); int list_delete_elem(sq_list * sl, int index, int * elem); int visit_all(sq_list sl); int init_list_sq(sq_list *sl) { sl->elem = (int*)malloc(LIST_INIT_SIZE * sizeof(int)); if(!sl->elem) exit(OVERFLOW); sl->length = 0; sl->listsize = LIST_INIT_SIZE; return OK; } void destroy_list_sq(sq_list * sl) { free(sl->elem); } int clear_list(sq_list * sl) { destroy_list_sq(sl); init_list_sq(sl); return OK; } int list_empty(sq_list sl) { if(sl.length) return FALSE; else return TRUE; } int list_length(sq_list sl) { return sl.length; } void get_elem(sq_list sl, int index, int * e) { *e = sl.elem[index-1]; } int locate_elem(sq_list sl, int e) { int len; for (len = 1; len <= sl.length; len++) if(sl.elem[len-1] == e) return len; return ERROR; } int insert_elem(sq_list *sl, int index, int e) { int * insert_ptr, * temp_ptr; if(index < 1 || index > sl->length + 1) return ERROR; if(sl->length >= sl->listsize) { sl->elem = (int *)realloc(sl->elem, (sl->listsize + LIST_INCREMENT) * sizeof(int) ); if(!sl->elem) exit(OVERFLOW); sl->listsize += LIST_INCREMENT; } insert_ptr = &(sl->elem[index-1]); for ( temp_ptr = &(sl->elem[sl->length-1]); temp_ptr >= insert_ptr; temp_ptr--) *(temp_ptr+1) = *temp_ptr; * insert_ptr = e; sl->length++; return OK; } int list_delete_elem(sq_list * sl, int index, int * e) { int * end_ptr, * temp_ptr; if(index < 1|| index > sl->length) return ERROR; end_ptr = &(sl->elem[sl->length-1]); temp_ptr = &(sl->elem[index-1]); *e = *temp_ptr; for (; temp_ptr < end_ptr; temp_ptr++ ) { *temp_ptr = *(temp_ptr+1); } sl->length--; return OK; } int prior_elem(sq_list sl, int curelem, int *priorelem) { int * temp_ptr; if( (sl.elem[0]) == curelem) return ERROR; temp_ptr = sl.elem; for(++temp_ptr; temp_ptr < sl.elem+sl.length; temp_ptr++) { if(*temp_ptr == curelem ) { *priorelem = *(temp_ptr-1); return OK; } } return ERROR; } int next_elem(sq_list sl, int curelem, int *nextelem) { int * temp_ptr; if( (sl.elem[sl.length-1]) == curelem) return ERROR; temp_ptr = sl.elem; for(; temp_ptr < sl.elem+sl.length-1; temp_ptr++) { if(*temp_ptr == curelem) { *nextelem = *(temp_ptr+1); return OK; } } return ERROR; } int visit_all(sq_list sl) { int *temp_ptr; temp_ptr = sl.elem; for(; temp_ptr<= sl.elem+sl.length-1; temp_ptr++) printf("%5d", *temp_ptr); return OK; }
下面是主函数
//main.c #include"list.c" #include"prompt.c" int main() { sq_list sl; int elem, pos, elem1; int op; while((op = prompt()) >= 0 && op <= 9 ) { switch(op){ case 0 : if (init_list_sq(&sl) == OK) printf("A table has beend created.\n"); break; case 1 : printf("Enter an element and position:"); scanf("%d %d", &elem, &pos); if(insert_elem(&sl, pos, elem) == OK) printf("Insertion succeed!\n"); break; case 2 : printf("Enter deleteted position:"); scanf("%d", &pos); if(list_delete_elem(&sl, pos, &elem) == OK) printf("Delete succeed!\n"); break; case 3 : printf("Enter an element‘s position:"); scanf("%d", &pos); get_elem(sl, pos, &elem1); printf("get elemnet:%d\n", elem1); break; case 4 : printf("Enter an element :"); scanf("%d", &elem); if (prior_elem(sl, elem, &elem1) == OK) printf("%d‘s prior element:%d\n", elem, elem1); break; case 5 : printf("Enter an element :"); scanf("%d", &elem); if (next_elem(sl, elem, &elem1) == OK) printf("%d‘s next element:%d\n", elem, elem1); break; case 6 : printf("length:%d\n", list_length(sl)); break; case 7 : printf("Enter an element:"); scanf("%d", &elem); printf("position:%d\n", locate_elem(sl,elem) ); break; case 8 : destroy_list_sq(&sl); printf("The table has been destroyed!\n"); break; case 9 : if(list_empty(sl) == TRUE) printf("The table is blank!\n"); else printf("The table is not blank!\n"); break; case ‘x‘ : return 0; default : printf("Enter a number (0-9)\n"); break; } } return 0; }
还有提示操作选项的
//prompt.c int prompt() { int option; printf("\n\n ********The Sequential Table********\n"); printf(" 0. Create an new senquential table.\n"); printf(" 1. Insert an element into the table.\n"); printf(" 2. Delete an element from the table.\n"); printf(" 3. Get an specific element from the table.\n"); printf(" 4. Get an specific element‘s prior element.\n"); printf(" 5. Get an specific element‘s next element.\n"); printf(" 6. Get the table‘s length.\n"); printf(" 7. Locate an specific elemnet.\n"); printf(" 8. Destory the table.\n"); printf(" 9. Whether if the table is empty?\n"); printf(" x. Exit\n"); printf("Enter your option: "); scanf("%d", &option); return option; }
Makefile文件
#makefile main: main.o cc -o $@ $^ main.o: main.c clean: rm main.o list.o prompt.o
心得体会:
基础很重要,还要注意细节。哈哈
本文出自 “兵疯千里” 博客,请务必保留此出处http://slientradio.blog.51cto.com/7241495/1389299