[C language data structure] sequence table
SeqList.h
# ifndef SEQ_LIST_H#define SEQ_LIST_Htypedef void SeqListNode;typedef void SeqList;/* * create sequence table * @ maximum capacity of param capacity sequence table * @ return returns pointer to sequence table * / SeqList* SeqList_Create (unsigned int capacity); / * destroy sequence table * @ param list sequence table pointer * / void SeqList_Destroy (SeqList* list) / * * clear the order table * @ param list sequence table pointer * / void SeqList_Clear (SeqList * list); / * * insert an element * @ param list sequence table pointer * @ param node element pointer * @ param pos inserted index * / int SeqList_Insert (SeqList * list,SeqListNode * node,int pos) into the sequence table pos location / * * get the element at the index position in the sequence table * @ param list sequence table pointer * @ param pos order table index value * @ param return element pointer * / SeqListNode* SeqList_Get (SeqList * list,int pos) / * * delete the value at the index position in the sequence table * @ param list the pointer to the sequence table * @ param pos order table index * @ param return non-zero indicates that the deletion is successful * / int SeqList_Remove (SeqList * list,int pos) / * * get the number of currently stored elements in the order table * @ pointer to the param list order table * @ the number of elements stored in the return sequence table * / int SeqList_Length (SeqList * list); / * get the maximum number of elements that can be stored in the order table * @ param list the pointer to the order table * @ return the maximum number of elements that can be stored in the sequence table * / int SeqList_Capacity (SeqList * list); # endif / / SEQLIST_H
SeqList.c
# include "SeqList.h" # include typedef unsigned int TSeqListNode; / / sequence table node typedef struct _ SeqList {unsigned int length; / / number of stored elements in sequence table unsigned int capacity; / / maximum number of storage elements in sequence table TSeqListNode * element []; / order table node flexible array} TSeqList / * * create sequence table * @ maximum capacity of param capacity sequence table * @ return returns the pointer to the sequence table * / SeqList* SeqList_Create (unsigned int capacity) {TSeqList * list = 0; / / create a space int size = sizeof (TSeqList) + sizeof (TSeqListNode) * capacity; list = (TSeqList *) malloc (size) that can accommodate the SeqList structure and flexible nodes If (list! = 0) {list- > length = 0; list- > capacity = capacity;} return list;} / * * destroy the sequence table * @ param list sequence table pointer * / void SeqList_Destroy (SeqList * list) {free (list) } / * * clear the order table * @ param list sequence table pointer * / void SeqList_Clear (SeqList * list) {TSeqList * s_list = (TSeqList *) list; if (s_list! = 0) {sequence list-> length = 0 }} / * * insert element * @ param list sequence table pointer * @ param node element pointer * @ param pos inserted index * @ param return into the sequence table pos position * / int SeqList_Insert (SeqList * list,SeqListNode * node,int pos) {int I / / determine whether the sequence table pointer and the inserted element pointer are null int ret = ((list! = 0) & & (node! = 0)); TSeqList * s_list = (TSeqList *) list; TSeqListNode * s_node = (TSeqListNode *) node / / determine whether the inserted index is legal and whether the array space is left ret = ((pos > = 0) & & (pos length) & & (slots list-> length + 1 capacity)); if (ret) {/ / parameter is valid, you can insert / / move the data back from the last one (I = slots list-> length;i > pos) Element [I] = element [I-1];} element [I] = length++;} return ret } / * * get the element * @ param list order table pointer * @ param pos order table index value * @ param return element pointer * / SeqListNode* SeqList_Get (SeqList * list,int pos) {SeqListNode* s_node = 0; TSeqList * s_list = (TSeqList *) list; if ((s_list! = 0) & (pos > = 0) & (pos)
< s_list->Length) {s_node = sordered list-> element [pos];} return list,int pos;} / * * Delete the value at index position in ordered table * @ pointer to param list ordered table * @ param pos ordered table index * @ value of element at index position in param return ordered table * / int SeqList_Remove (SeqList * list,int pos) {int i; TSeqList * s_list = (TSeqList *) list Int ret = (s_list! = 0) & & (pos > = 0) & & (pos
< s_list->Length); if (ret) {for (I = pos + 1)
< s_list->Length;i++) {slots list-> element [I-1] = slots list-> element [I];} slots list-> length--;} return ret } / * * get the number of currently stored elements in the sequence table * @ param list pointer to the sequence table * @ number of stored elements in the return sequence table * / int SeqList_Length (SeqList * list) {TSeqList * s_list = (TSeqList *) list; int ret =-1; if (s_list! = 0) {ret = slots list-> length;} return ret } / * * get the maximum number of elements that can be stored in the sequence table * @ param list the pointer of the sequence table * @ return the maximum number of elements that can be stored in the sequence table * / int SeqList_Capacity (SeqList * list) {TSeqList * s_list = (TSeqList *) list; int ret = (s_list! = 0); if (ret) {ret = ordered list-> capacity;} return ret;}