[Data structure] stores a single-chain table sequentially, and stores a single-chain data structure.
Data StructureSequential storage of Single-Chain tables
There is nothing to worry about. Let's review the data structures we 've learned before and write a bit of fun. There's not much to talk about the theory, and there's a lot of online searches;
It is important to master the idea of this data structure. The most important lesson of the entire data structure is the idea!
The following code is used:
//====================================================================== // // Copyright (C) 2014-2015 SCOTT // All rights reserved // // filename: SeqList.c// description: a demo to display SeqList// // created by SCOTT at 01/26/2015 // http://blog.csdn.net/scottly1// //====================================================================== #include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <string.h>#define TRUE1#define FALSE0#define MAX_SIZE1000typedef int Type;typedef int Bool;typedef struct _tag_list{Type *elem;int length;// current lengthint size;// max length}LIST, *PLIST;Bool createList(LIST *list, int len){if(len > MAX_SIZE || len <= 0 ){printf("### OVER MAX_LEN!\n");return FALSE;}list->elem = (Type *)malloc(len * sizeof(Type));if(NULL == list){printf("### createList Create Error!\n");return FALSE;}else{list->length = 0;list->size = MAX_SIZE;}printf("### createList Create list Success!\n");return TRUE;}Bool clearList(LIST *list){if(NULL == list){printf("### clearList List Error!\n");return FALSE;}Type *p = list->elem;for(; p < list->elem + list->size; ++p){memset(p, 0, sizeof(*p));}return TRUE;}Bool displayList(LIST *list){if(NULL == list){printf("### displayList List Error!\n");return FALSE;}int i = 0;Type *p = list->elem;for(; p < list->elem + list->length; ++p, i++){printf("list->elem[%d] = %d\n", i, *p);}return TRUE;}Bool insertList(LIST *list, int flag, Type elem){int i = 0;if(NULL == list || flag < 0 || flag >= list->size){printf("### insertList List Error!\n");return FALSE;}if(list->length + 1 > list->size){printf("### List Full!\n");return FALSE;}flag = flag>list->length?list->length:flag;for(i=list->length; i>flag; --i){list->elem[i] = list->elem[i-1];}list->elem[i] = elem;++list->length;return TRUE;}Bool deleteList(LIST *list, int flag, Type *elem){int i = 0;if(NULL == list || flag < 0 || flag >= list->size){printf("### deleteList List Error!\n");return FALSE;}if(list->length - 1 < 0 ){printf("### List Empty!\n");return FALSE;}flag = flag>list->length?list->length:flag;*elem = list->elem[flag];for(i=flag; i<list->length; ++i){list->elem[i] = list->elem[i+1];}--list->length;return TRUE;}Bool mergeList(LIST *src1, LIST *src2, LIST *dst){int i = 0;Type *pA, *pB, *pC;Type *pA_last, *pB_last, *pC_last;if(NULL == src1 || NULL == src2 || NULL == dst){printf("### displayList List Error!\n");return FALSE;}pA = src1->elem; pA_last = src1->elem + src1->length;pB = src2->elem; pB_last = src2->elem + src2->length;pC = dst->elem;while(pA < pA_last && pB < pB_last){if(*pA >= *pB)*pC++ = *pB++;else*pC++ = *pA++;++dst->length;}while(pA < pA_last){*pC++ = *pA++;++dst->length;}while(pB < pB_last){*pC++ = *pB++;++dst->length;}return TRUE;}int main(int argc, char *argv[]){LIST list1;LIST list2;LIST dst;Type elem1 = 10;Type elem2 = 30;createList(&list1, 5);createList(&list2, 5);createList(&dst, 10);printf("===================\n");insertList(&list1, 0, elem1);insertList(&list1, 1, elem1+1);insertList(&list1, 2, elem1+2);insertList(&list1, 3, elem1+4);insertList(&list1, 4, 100);displayList(&list1);printf("===================\n");insertList(&list2, 0, elem2+3);insertList(&list2, 1, elem2+4);insertList(&list2, 2, elem2+5);insertList(&list2, 3, elem2+6);insertList(&list2, 4, elem2+7);displayList(&list2);printf("===================\n");mergeList(&list1, &list2, &dst);displayList(&dst);printf("\n\n");return 0;}
Note: original article, reproduced please indicate the source: http://blog.csdn.net/scottly1/article/details/43154589