動態順序表: 容量不夠時 自動增容(靜態順序表的改進)
動態順序表的結構:
typedef int DataType;
typedef struct SeqList
{
DataType* _array; //指向數據塊的指針
size_t _size; //有效數據個數
size_t _capacity; //容量
}SeqList;
增容:
void _CheckCapacity(SeqList* pSeq)
{
if (pSeq->_size >= pSeq->_capacity)
{
//防止開始空間爲0,而容量一直爲0,所以多開闢一些空間。
pSeq->_capacity = 2 * pSeq->_capacity + 3;
pSeq->_array = (DataType *)realloc(pSeq->_array, pSeq- >_capacity*sizeof(DataType));
}
}
//順序表中數據爲空可用提示語句給出或者assert報錯
void InitSeqList(SeqList* pSeq) //初始化 { assert(pSeq); //memset(pSeq->_array, 0, sizeof(DataType)*MAX_SIZE); pSeq->_array = NULL; pSeq->_size = 0; pSeq->_capacity = 0; } void PrintSeqList(SeqList* pSeq) { assert(pSeq); for (size_t i = 0; i<pSeq->_size; ++i) { printf("%d ", pSeq->_array[i]); } printf("NULL \n"); } void _CheckCapacity(SeqList* pSeq) { if (pSeq->_size >= pSeq->_capacity) { //防止開始空間爲0,而容量一直爲0,所以多開闢一些空間。 pSeq->_capacity = 2 * pSeq->_capacity + 3; pSeq->_array = (DataType *)realloc(pSeq->_array, pSeq->_capacity*sizeof(DataType)); } } void PushBack(SeqList* pSeq, DataType x) //尾插 { assert(pSeq); _CheckCapacity(pSeq); pSeq->_array[pSeq->_size++] = x; } void PopBack(SeqList* pSeq) { assert(pSeq); if (pSeq->_size) { pSeq->_array[pSeq->_size] = NULL; pSeq->_size--; } } void PushFront(SeqList* pSeq, DataType x) //頭插 { assert(pSeq); _CheckCapacity(pSeq); //注意此處應用int而不能用size_t,否則會一直循環.size_t永遠>=0 for (int i = pSeq->_size - 1; i >= 0; --i) { pSeq->_array[i + 1] = pSeq->_array[i]; } pSeq->_array[0] = x; pSeq->_size++; } void PopFront(SeqList* pSeq) { assert(pSeq); if (pSeq->_size <= 0) { printf("SeqList is empty\n"); return; } //assert(pSeq->_size); for (size_t i = 0; i < pSeq->_size; ++i) { pSeq->_array[i] = pSeq->_array[i + 1]; } pSeq->_size--; } void Insert(SeqList* pSeq, size_t pos, DataType x) //增 { assert(pSeq); assert(pos <= pSeq->_size); _CheckCapacity(pSeq); for (int i = pSeq->_size - 1; i >= (int)pos; --i) // 注意size_t永遠>=0 { pSeq->_array[i + 1] = pSeq->_array[i]; } pSeq->_array[pos] = x; pSeq->_size++; } int Find(SeqList* pSeq, size_t pos, DataType x) { assert(pSeq); for (size_t i = pos; i < pSeq->_size; ++i) { if (pSeq->_array[i] == x) return i; } return -1; } void Erase(SeqList* pSeq, size_t pos) //刪 { assert(pSeq); assert(pos<pSeq->_size); for (size_t i = pos + 1; i < pSeq->_size; ++i) { pSeq->_array[i - 1] = pSeq->_array[i]; } pSeq->_size--; } int Remove(SeqList* pSeq, DataType x) { assert(pSeq); int pos = Find(pSeq, 0, x); if (pos != -1) { Erase(pSeq, pos); } return pos; } void RemoveAll(SeqList* pSeq, DataType x) //清除 { int count = 0; assert(pSeq); for (size_t i = 0; i <pSeq->_size; i++) { if (pSeq->_array[i] == x) { count++; } else { pSeq->_array[i - count] = pSeq->_array[i]; } } pSeq->_size -= count; }
測試:
//PushBack PopBack void Test1() { SeqList seq; InitSeqList(&seq); PushBack(&seq, 1); PushBack(&seq, 2); PushBack(&seq, 3); PushBack(&seq, 4); PushBack(&seq, 5); PushBack(&seq, 6); PrintSeqList(&seq); PopBack(&seq); PopBack(&seq); PopBack(&seq); PrintSeqList(&seq); PopBack(&seq); PopBack(&seq); PrintSeqList(&seq); } //PushBack PopBack void Test2() { SeqList seq; InitSeqList(&seq); PushFront(&seq, 1); PushFront(&seq, 2); PushFront(&seq, 3); PushFront(&seq, 4); PrintSeqList(&seq); PushFront(&seq, 0); PushFront(&seq, -1); PrintSeqList(&seq); PopFront(&seq); PopFront(&seq); PopFront(&seq); PrintSeqList(&seq); PopFront(&seq); PopFront(&seq); PopFront(&seq); PopFront(&seq); PrintSeqList(&seq); } // Insert/Find/Erase void Test3() { SeqList seq; InitSeqList(&seq); PushBack(&seq, 1); PushBack(&seq, 2); PushBack(&seq, 4); PushBack(&seq, 5); PrintSeqList(&seq); int tmp = Find(&seq, 0, 2); printf("%d\n", tmp); Insert(&seq, tmp, 100); Insert(&seq, 2, 10); Insert(&seq, 2, 3); PrintSeqList(&seq); //刪除 tmp = Find(&seq, 0, 3); printf("%d\n", tmp); Erase(&seq, tmp); PrintSeqList(&seq); //添加 越界情況 tmp = Find(&seq, 0, 1); printf("%d\n", tmp); Insert(&seq, tmp, 100); PrintSeqList(&seq); } //Remove RemoveAll void Test4() { SeqList seq; InitSeqList(&seq); PushBack(&seq, 1); PushBack(&seq, 2); PushBack(&seq, 3); PushBack(&seq, 4); PushBack(&seq, 2); PrintSeqList(&seq); /*Remove(&seq, 3); PrintSeqList(&seq);*/ RemoveAll(&seq, 2); PrintSeqList(&seq); }