Sequence table
*** * this paper mainly talks about one kind of linear table, that is, sequential table. Sequential table is to use sequential storage to store data. The so-called sequential storage means that the address of data in memory is also stored continuously, and there is no space (interval) in the middle. The advantage of sequential table is that it supports random access, that is, accessing elements through subscripts; The disadvantage is that a large number of elements need to be moved when inserting and deleting data. There are two ways to create a sequence table: static sequence table and dynamic sequence table.
Start of text:
1. Static sequence table
Through the preface, we can understand that the data in the sequence table is stored continuously, so it is not difficult for us to think of using arrays to establish the sequence table. The static sequence table consists of a fixed length array.
//Static sequence table #define MAX 10 typedef int SLDateType;//Declare data type typedef struct Seqlist//Sequence table { SLDateType arr[MAX];//Array for storing data size_t size;//Number of valid data }SeqList;
You may be interested in typedef int SLDateType; This sentence is a little questionable and I think it's redundant. In fact, it is not. This is to improve the portability of the program. If you want to change the data type to char, you only need to change it to typedef char SLDateType, which is more convenient.
Renaming the structure name to SeqList is to improve the readability of the code and simplify the complex type declaration.
The basic operations of sequence table are: (1) initialization of sequence table (2) capacity increase of sequence table
(3) Insert sequence table footer (4) delete sequence table footer (5) insert sequence header (6) delete sequence header (7) search sequence table (8) insert sequence table at specified position x (9) delete sequence table at specified position (10) destroy sequence table (11) print sequence table
There are the above basic operations. In this article, I mainly use the dynamic sequence table to explain these operations, because the dynamic sequence table is relatively more used.
2. Dynamic sequence table
Dynamic sequence table is to open up an array on the heap to store data. If the memory is not enough, it will be increased. The rest of the basic and static sequence tables are no different.
//Dynamic sequence table typedef int SLDateType; typedef struct SeqList { SLDateType* a;//Points to the dynamically opened array space size_t size;//Number of saved data size_t capacity; // Total capacity }SeqList;
The basic operation of the sequence table has been described above. Let's break it one by one.
(1) Sequence table initialization:
void SeqListInit(SeqList* ps)//initialization { ps->capacity = 0;//The capacity is set to 0 first ps->size = 0; ps->a = NULL;//Set to NULL first to avoid wild pointer problems }
(2) Capacity increase of sequence table:
void CheckCapacity(SeqList* ps)//increase capacity { //Check whether the capacity is 0. If it is 0, set the new capacity to 4. If it is not 0, expand it twice int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; //Resize space int* tmp = (int*)realloc(ps->a, newcapacity*sizeof(int)); //Judge whether the development is successful if (tmp == NULL) { printf("realloc fail\n"); exit(-1); } //If the development is successful, assign a new value to a and capacity ps->a = tmp; ps->capacity = newcapacity; }
(3) End of sequence table:
void SeqListPushBack(SeqList* ps, SLDateType x)//Tail insertion { //Check the capacity first CheckCapacity(ps); //Insert data at tail x ps->a[ps->size] = x; //Saved data + 1 ps->size++;
Code run wave
#include"sxb.h" void test1() { SeqList sxb; //initialization SeqListInit(&sxb); //Tail insertion 1,2,3,4,5 SeqListPushBack(&sxb, 1); SeqListPushBack(&sxb, 2); SeqListPushBack(&sxb, 3); SeqListPushBack(&sxb, 4); SeqListPushBack(&sxb, 5); //Print SeqListPrint(&sxb); } int main() { test1(); return 0; }
(4) Sequential tail deletion
void SeqListPopBack(SeqList* ps)//Tail deletion { assert(ps->size>0);//Asserts whether valid data exists ps->size--;//Effective data reduction }
(5) Sequential header interpolation
void SeqListPushFront(SeqList* ps, SLDateType x)//Head insert { assert(ps); CheckCapacity(ps);//Inspection capacity increase //Mobile data for (int i = ps->size; i >0; i--) { ps->a[i] = ps->a[i - 1]; } //Head insert ps->a[0] = x; ps->size++; }
(6) Sequential header deletion
void SeqListPopFront(SeqList* ps)//Header deletion { assert(ps->a); //Overwrite first data for (size_t i = 0; i<ps->size; i++) { ps->a[i] = ps->a[i+1]; } //Valid data minus one ps->size--; }
(7) Sequential table lookup
int SeqListFind(SeqList* ps, SLDateType x)//lookup { assert(ps->size > 0); //Traverse the array and return the subscript if found for (int i = 0; i < ps->size; i++) { if (x == ps->a[i]) return i; } return -1; }
(8) The sequence table inserts an x at the specified location
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x) //Insert at specified location { assert(ps->size>0); CheckCapacity(ps); int end = ps->size; //Judge whether the pos position is correct if (pos >= 0 && pos <= ps->size) { //The position after pos moves back one bit for (int i = end; i >pos; i--) { ps->a[i] = ps->a[i - 1]; } ps->a[pos] = x; ps->size++; } else return; }
(9) The sequence table is deleted at the specified location
void SeqListErase(SeqList* ps, size_t pos)//Arbitrary deletion { assert(ps->size > 0); int begin = pos; if (pos >= 0 && pos <= ps->size) { //The element after pos moves forward one bit to cover the pos position for (int i = begin; i < ps->size; i++) { ps->a[i] = ps->a[i + 1]; } ps->size--; } }
(10) Sequence table destruction
void SeqListDestory(SeqList* ps)//Sequence table destruction { //Set the capacity to 0 and the number of valid data to 0 ps->capacity = 0; ps->size = 0; //Frees the specified space free(ps->a); //Set pointer to NULL ps->a = NULL; }
(11) Sequence table printing
void SeqListPrint(SeqList* ps)//Print { //Traversal array printing for (size_t i = 0; i < ps->size; i++) { printf("%d ", ps->a[i]); } printf("\n"); }
The above is the content of this issue. I hope you can gain something!!!