實(shí)現(xiàn)動(dòng)態(tài)順序表
10多年的虎林網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷(xiāo)型網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整虎林建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“虎林網(wǎng)站設(shè)計(jì)”,“虎林網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
SeqList.h
#pragma once #include#include #include #include typedef int DataType; typedef struct SeqList { DataType* arry; size_t size; size_t capacity; }SeqList; void check(SeqList*Seq);//判斷指針是否有效,并動(dòng)態(tài)開(kāi)辟內(nèi)存 void InitSeqList(SeqList*Seq);//初始化結(jié)構(gòu)體 void PrintSeqList(SeqList*Seq);//輸出函數(shù) void PushBack(SeqList*Seq, DataType x);//后插 void PopBack(SeqList*Seq);//后刪 int Find(SeqList*Seq, DataType x);//查找數(shù)據(jù) void Erase(SeqList*Seq, size_t pos);//清除第幾個(gè)數(shù)據(jù) void Remove(SeqList*Seq, DataType x);//刪除第一個(gè)數(shù)據(jù)x void Removeall(SeqList*Seq, DataType x);//刪除所有的x void Modify(SeqList*Seq, size_t pos, DataType x);//把第pos個(gè)數(shù)據(jù)修改成x void Insert(SeqList*Seq, size_t pos, DataType x);//把第pos個(gè)數(shù)據(jù)插入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 #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; }