实现动态顺序表
实现动态顺序表
10多年的虎林网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整虎林建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“虎林网站设计”,“虎林网站推广”以来,每个客户项目都认真落实执行。
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);//判断指针是否有效,并动态开辟内存 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 #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; }
本文标题:实现动态顺序表
网页网址:http://myzitong.com/article/jgpdcj.html