1. Header file
-
What I'm talking about here is the dynamic sequence table. Of course, the operation of the static sequence table is basically the same, but the static sequence table cannot be expanded. The quantity is controlled, which will cause a waste of space.
-
Static sequence table
#pragma once / / prevent header file from containing #include<stdio.h> #define MaxSize 100 typedef int SLDataType; //Use typedef to rename int to SLDataType, which is convenient for changing data type in the future typedef struct SeqList { SLDataType arr[MaxSize]; size_t size;//size_t -> unsigned int }SeqList;
- Dynamic sequence table
#pragma once / / prevent header file from containing #include<stdio.h> #include<stdlib. h> / / header file of dynamic memory function #include<assert. h> / / header file of assert, used to check typedef int SLDataType; typedef struct SeqList { SLDataType* a;//Created as a pointer, you can dynamically increase space size_t size;//Current data volume size_t Capacity;//Total capacity }SeqList;
2. Initialization
- Initialization pointer a=NULL, current data size=0, total Capacity=0
void SeqListInit(SeqList* ps) { assert(ps);//Assertion check. If the program makes an error, it will report an interrupt. Where is the error pointed at the display end ps->a = NULL; ps->Capacity = 0; ps->size = 0; }
3. Destroy
- If the pointer PS - > A is not null, it is released and set to null
void SeqListDestory(SeqList* ps) { if (ps->a) { free(ps->a);//Prevent memory leaks ps->a = NULL;//Avoid becoming a wild pointer } ps->Capacity = 0;//Set 0 ps->size = 0;//Set 0 }
4. Print
- Loop traversal, output sequence table
- Time complexity: o(n)
void SeqListPrint(SeqList* ps) { assert(ps); for (size_t i = 0; i < ps->size; i++) { printf("%d ", ps->a[i]); } printf("\n"); }
5. Tail insertion
- First check whether the sequence table is full. When it is full, increase the capacity, and then insert a value at the end of the data, size+1
- Time complexity: o(1)
void SeqListPushBack(SeqList* ps, SLDataType x) { assert(ps); SeqListCheckCapacity(ps);//Check whether the size is equal to the Capacity, and expand the Capacity if it is equal to the Capacity ps->a[ps->size] = x; ps->size++; }
6. Tail deletion
- Just change size-1, and you won't be able to access the last element
- Time complexity: o(1)
void SeqListPopBack(SeqList* ps) { assert(ps); ps->size--; }
7. Head insert
- The head insertion is the same as the tail insertion. It is necessary to check whether the sequence table is full. If it is full, the capacity will be increased. Move all elements back one bit to make room for position 0 to x, size+1
- Time complexity: o(n)
void SeqListPushFront(SeqList* ps, SLDataType x) { assert(ps); SeqListCheckCapacity(ps);//Check whether the size is equal to the Capacity, and expand the Capacity if it is equal to the Capacity size_t end = ps->size; while (end > 0) { ps->a[end] = ps->a[end - 1]; --end; } ps->a[0] = x; ps->size++; }
8. Header deletion
- To delete the first element, you need to move the following element forward, and then size-1
- Time complexity: o(n)
void SeqListPopFront(SeqList* ps) { assert(ps); size_t begin = 1; while (begin < ps->size) { ps->a[begin-1] = ps->a[begin]; ++begin; } ps->size--; }
9. Search
- Search in the sequence table according to element x, traverse the search, find the position returned in the sequence table, and return - 1 if not found
- Worst time complexity: o(n)
- Best time complexity: o(1)
- Average time complexity: o(n/2)
- Time complexity: o(n)
int SeqListFind(SeqList* ps, SLDataType x) { assert(ps); size_t begin = 0; while (begin < ps->size) { if (ps->a[begin] == x) return begin; begin++; } return -1; }
10. Insert
- Insert the value x into the sequence table according to the value of pos. check whether POS is within the length + 1 of the sequence table. When POS is found, move the original data back one bit, and insert the value x into POS
- Worst time complexity: o(n)
- Best time complexity: o(1)
- Average time complexity: o(n/2)
- Time complexity: o(n)
void SeqListInsert(SeqList* ps, size_t pos, SLDataType x) { assert(ps); assert(pos <= ps->size && pos >= 0); SeqListCheckCapacity(ps); size_t end = ps->size; while (end > pos) { ps->a[end] = ps->a[end - 1]; --end; } ps->a[pos] = x; ps->size++; }
11. Delete
- Search in the sequence table according to the value of pos. first, check whether the value of POS is within the length range of the sequence table. If yes, move the data behind POS one bit forward and delete the value of POS
- Worst time complexity: o(n)
- Best time complexity: o(1)
- Average time complexity: o(n/2)
- Time complexity: o(n)
void SeqListErase(SeqList* ps, size_t pos) { assert(ps); assert(pos < ps->size && pos >= 0); size_t begin = pos; while (begin < ps->size - 1) { ps->a[begin] = ps->a[begin + 1]; ++begin; } ps->size--; }
12. Check capacity
- Check the Capacity. If the size increases to be equal to the Capacity, it means that the sequence table is full, and the Capacity must be expanded to increase the size. At this time, call this function to increase the value of Capacity
void SeqListCheckCapacity(SeqList* ps) { assert(ps); if (ps->size == ps->Capacity) { size_t newCapacity = ps->Capacity == 0 ? 4 : ps->Capacity * 2; SLDataType*ps1 = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));//Use realloc function to re open up a space for capacity expansion if (ps1==NULL)//Check whether the development fails { perror("realloc"); return; } ps->a = ps1;//New space assigned to a ps->Capacity = newCapacity;//Assign value to Capacity for Capacity expansion } }
Full code link:
https://gitee.com/deng_yuniubi/data-structure
https://github.com/yuxuanniu6/Data-Struct