Set --- create linear table
dissolve---destroy the linear table
length --- get the length of the linear table
Dequeue --- remove an element from a linear list
Add --- add an element to a specific position in the linear table
The linear table is represented as a special data type in the program, and the operations of the linear table are represented in the program as a set of functions
Describe the linear table in C language:
1. The sequential storage structure of the linear table refers to storing the data elements of the linear table in sequence with a segment of storage units with consecutive addresses.
The following paragraph is SeqList.c
#include <malloc.h> #include <stdio.h> #include "SeqList.h" typedef unsigned int TSeqListNode; typedef struct _tag_SeqList { int capacity; int length; TSeqListNode * node; } TSeqList; SeqList* SeqList_Create(int capacity) { TSeqList * ret = NULL; if(capacity >= 0) { ret = (TSeqList*)malloc(sizeof(TSeqList)+sizeof(TSeqListNode)*capacity); //Apply for a memory space, the first address is ret } if(ret != NULL) { ret->capacity = capacity; ret->length = 0; ret->node = (TSeqListNode*)(ret+1); //points to the end of the structure } return ret; } void SeqList_Destroy(SeqList* list) { free(list); } void SeqList_Clear(SeqList* list) { TSeqList* sList = (TSeqList*)list; if(sList!=NULL) { sList->length=0; } } int SeqList_Length(SeqList* list) //Actually contains the number of data elements { TSeqList* sList = (TSeqList*)list; int ret = -1; if(sList!=NULL) { ret = sList->length; } return ret; } int SeqList_capacity(SeqList* list) { TSeqList* sList = (TSeqList*)list; int ret = -1; if(sList!=NULL) { ret = sList->capacity; } return ret; } int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) { TSeqList* sList = (TSeqList*)list; int ret = (sList! = NULL); int i=0; ret = ret && (sList->length +1 <= sList->capacity); ret = ret && (pos >= 0); if (ret) { if(pos >= (sList->length)) { pos = sList->length; } for(i=(sList->length); i>pos; i--) { sList->node[i] = sList->node[i-1]; } sList->node[i] = (TSeqListNode)node; sList->length++; } return ret; } SeqListNode* SeqList_Get(SeqList* list, int pos) { TSeqList* sList = (TSeqList*)list; SeqListNode * ret = NULL; if((sList != NULL)&&(pos >= 0)&&(pos <= sList->length)) { ret = (SeqListNode*)(sList->node[pos]); } return ret; } SeqListNode* SeqList_Delete(SeqList* list, int pos) { TSeqList* sList = (TSeqList*)list; SeqListNode * ret = NULL; int i=0; if((sList != NULL)&&(pos >= 0)&&(pos < sList->length)) { ret = (SeqListNode*)(sList->node[pos]); for(i=pos+1; i<sList->length;i++) { sList->node[i-1] = sList->node[i]; } sList->length--; } return ret; }
The following is the header file
SeqList.h
#ifndef _SEQLIST_H_ #define _SEQLIST_H_ typedef void SeqList; typedef void SeqListNode; /* Used to create and return an empty linear table with a maximum capacity of capacity */ SeqList* SeqList_Create(int capacity); /* Used to destroy a linear table */ void SeqList_Destroy(SeqList* list); /* Used to clear all elements in a linear list SeqList, linear list Return to the initial state when it was created */ void SeqList_Clear(SeqList* list); /* Used to return the number of all elements in a linear table */ int SeqList_Length(SeqList* list); /* Used to return the number of all elements in a linear table */ int SeqList_capacity(SeqList* list); /* Used to insert a new element node into the pos position of a linear list SeqList A return value of 1 indicates that the insertion was successful, and a return value of 0 indicates that the insertion failed */ int SeqList_Insert(SeqList* list, SeqListNode* node, int pos); /* The element node used to get the pos position of the linear table SeqList The return value is the element at the pos position, NULL indicates that the acquisition failed */ SeqListNode* SeqList_Get(SeqList* list, int pos); /* Used to delete the element node at the pos position of the linear table SeqList Returns the element at the deleted pos position, NULL indicates that the deletion failed */ SeqListNode* SeqList_Delete(SeqList* list, int pos); #endif // __2_1_H_
The following is the main function, the test sequence linked list
#include <stdio.h> #include <stdlib.h> #include "SeqList.h" intmain() { int index=0; SeqList* list = SeqList_Create(5); int i=0; int j=1; int k=2; int x=3; int y=4; int z = 5; SeqList_Insert(list,&i,0); SeqList_Insert(list,&j,0); SeqList_Insert(list,&k,0); SeqList_Insert(list,&x,0); SeqList_Insert(list,&y,0); SeqList_Insert(list,&z,0); for(index=0; index<SeqList_Length(list); index++) { int* p = (int*)SeqList_Get(list,index); printf("%d\n",*p); } while(SeqList_Length(list)>0) { int *p = SeqList_Delete(list,0); printf("%d\n",*p); } SeqList_Destroy(list); return 0; }