Please refer to the data for related concepts such as sequence table, which is mainly implemented here.
Note:
1. Implementation of sequence table C language;
2. It can be implemented in a simpler way, which is mainly helpful for understanding. It can be modified and improved on this basis;
3. Provide several simple functions, which can be added by yourself;
4. The principle of vector in STL can be known by C + + encapsulation.
Sequence table capacity.
#define CAPACITY 20 / / capacity
Sequence table structure.
typedef struct { int *m_pHead; //Point to array header int m_iSize; //Size int m_iCapacity; //capacity }STABLE;
Some operations, other operations can be added by yourself.
STABLE* createTable(); void addElement(STABLE* pTable, int iElement); void delElement(STABLE* pTable); void showTable(STABLE* pTable); void releaseMem(STABLE* pTable);
Allocated memory, number of elements is 0.
STABLE* createTable() { STABLE *pTable = (STABLE*)malloc(sizeof(STABLE)); if(!pTable) { printf("Dynamic memory allocation failed!\n"); return pTable; } pTable->m_pHead = NULL; pTable->m_iSize = 0; pTable->m_iCapacity = 0; pTable->m_pHead = (int*)malloc(sizeof(int) * CAPACITY); if(!pTable->m_pHead) { printf("Dynamic memory allocation failed!\n"); return NULL; } pTable->m_iSize = 0; pTable->m_iCapacity = CAPACITY; return pTable; }
Add to the end. If you want to add at any place, add an index parameter. Move the element backward. You can implement it yourself.
void addElement(STABLE* pTable, int iElement) { if(!pTable || !pTable->m_pHead) { printf("Sequence table does not exist!\n"); return; } if(pTable->m_iSize == pTable->m_iCapacity) { printf("Sequence table full!\n"); //You can reallocate memory, so you don't need to simplify return; } pTable->m_pHead[pTable->m_iSize] = iElement; pTable->m_iSize++; }
Delete at the end. If you want to delete at any position, add an index parameter and move the element forward. You can implement it by yourself.
void delElement(STABLE* pTable) { if(!pTable || !pTable->m_pHead) { printf("Sequence table does not exist!\n"); return; } if(pTable->m_iSize < 1) { printf("The number of order table elements is 0!\n"); return; } pTable->m_iSize--; }
Print all elements.
void showTable(STABLE* pTable) { if(!pTable || !pTable->m_pHead) { printf("Sequence table does not exist!\n"); return; } printf("Capacity:%d,Size:%d,Print elements:\n", pTable->m_iCapacity, pTable->m_iSize); for(int i = 0; i < pTable->m_iSize; i++) printf("%d ", pTable->m_pHead[i]); printf("\n"); }
Memory free.
void releaseMem(STABLE* pTable) { if(!pTable) { printf("Sequence table does not exist!\n"); return; } if(pTable->m_pHead) free(pTable->m_pHead); free(pTable); printf("Memory released successfully!\n"); }