您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
实现动态顺序表
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。