您好,登錄后才能下訂單哦!
實現動態順序表
SeqList.h
#pragma once #include<stdio.h> #include<string.h> #include<assert.h> #include<malloc.h> typedef int DataType; typedef struct SeqList { DataType* arry; size_t size; size_t capacity; }SeqList; void check(SeqList*Seq);//判斷指針是否有效,并動態開辟內存 void InitSeqList(SeqList*Seq);//初始化結構體 void PrintSeqList(SeqList*Seq);//輸出函數 void PushBack(SeqList*Seq, DataType x);//后插 void PopBack(SeqList*Seq);//后刪 int Find(SeqList*Seq, DataType x);//查找數據 void Erase(SeqList*Seq, size_t pos);//清除第幾個數據 void Remove(SeqList*Seq, DataType x);//刪除第一個數據x void Removeall(SeqList*Seq, DataType x);//刪除所有的x void Modify(SeqList*Seq, size_t pos, DataType x);//把第pos個數據修改成x void Insert(SeqList*Seq, size_t pos, DataType x);//把第pos個數據插入x void check(SeqList*Seq) { assert(Seq); if (Seq->size >= Seq->capacity) { DataType*tmp; Seq->capacity *= 2; tmp = (DataType*)malloc(sizeof(DataType)*(Seq->capacity)); memcpy(tmp, Seq->arry, sizeof(DataType)*(Seq->size)); free(Seq->arry); Seq->arry = tmp; } } void InitSeqList(SeqList*Seq) { assert(Seq); Seq->capacity = 2; Seq->arry = (DataType*)malloc(sizeof(DataType)*Seq->capacity); Seq->size = 0; } void PrintSeqList(SeqList*Seq) { int i = 0; for (i = 0; i < (int)Seq->size; i++) { printf("%d\n", Seq->arry[i]); } } void PushBack(SeqList*Seq, DataType x) { check(Seq); Seq->arry[Seq->size++] = x; } void PopBack(SeqList*Seq) { assert(Seq); if (Seq->size == 0) { printf("順序表已空!\n"); return; } --(Seq->size); } void PushFront(SeqList*Seq, DataType x) { check(Seq); int i = Seq->size - 1; for (; i >= 0; i--) { Seq->arry[i + 1] = Seq->arry[i]; } Seq->arry[0] = x; ++Seq->size; } void PopFront(SeqList*Seq) { assert(Seq); int i = 0; if (Seq->size == 0) { printf("順序表已空!\n"); return; } for (; i <= (int)Seq->size; i++) { Seq->arry[i] = Seq->arry[i + 1]; } --Seq->size; } void Remove(SeqList*Seq, DataType x) { assert(Seq); int i = 0; for (; i < (int)Seq->size; i++) { if (Seq->arry[i] == x) { Seq->arry[i] = Seq->arry[i + 1]; } } --Seq->size; } void Removeall(SeqList*Seq, DataType x) { assert(Seq); int firstIndex = 0; int secondIndex = 0; int count = 0; while (secondIndex <= (int)Seq->size) { if (Seq->arry[secondIndex] == x) { count++; } else { Seq->arry[firstIndex] = Seq->arry[secondIndex]; firstIndex++; } secondIndex++; } Seq->size -= count; } int Find(SeqList*Seq, DataType x) { assert(Seq); int i = 0; for (; i < (int)Seq->size; i++) { if (Seq->arry[i] == x) { return Seq->arry[i]; } else { return -1; } } return 0; } void Erase(SeqList*Seq, size_t pos) { assert(Seq); int i = pos - 1; for (; i < (int)Seq->size - 1; i++) { Seq->arry[i] = Seq->arry[i + 1]; } --Seq->size; } void Modify(SeqList*Seq, size_t pos, DataType x) { check(Seq); int i = 0; for (; i < (int)Seq->size - 1; i++) { if (i == pos) { Seq->arry[i] = x; } } } void Insert(SeqList*Seq, size_t pos, DataType x) { check(Seq); int i = Seq->size - 1; for (; i >= (int)pos; i--) { Seq->arry[i + 1] = Seq->arry[i]; } Seq->arry[pos] = x; Seq->size++; } Test.c #include<stdio.h> #include"SeqList.h" SeqList Seq; /*void test() { PushBack(&Seq, 1); PushBack(&Seq, 2); PushBack(&Seq, 3); PushBack(&Seq, 4); PopBack(&Seq); PrintSeqList(&Seq); }*/ void test1() { PushFront(&Seq, 1); PushFront(&Seq, 2); PushFront(&Seq, 2); PushFront(&Seq, 3); PushFront(&Seq, 4); //Remove(&Seq, 2); // Removeall(&Seq, 2); // Erase(&Seq, 1); Modify(&Seq, 3, 5); //Insert(&Seq, 4, 6); PrintSeqList(&Seq); } int main(void) { InitSeqList(&Seq); test1(); system("pause"); return 0; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。