加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Seqlist2.c 2.97 KB
一键复制 编辑 原始数据 按行查看 历史
luo 提交于 2021-11-22 22:18 . 顺序表插删查改
#include"Seqlist.h"
#include<malloc.h>
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
//初始化ps指向的变量
void SeqListInit(SeqList* ps, int initCapacity) {
assert(ps);
ps->arr = (int*)malloc(initCapacity * sizeof(int));
if (ps->arr == NULL) {
assert(0);
return;
}
ps->capacity = initCapacity;
ps->size = 0;
}
//销毁顺序表
void SeqListDestroy(SeqList* ps) {
assert(ps);
if (ps->arr) {
free(ps->arr);
ps->arr = NULL;
ps->capacity = 0;
ps->size = 0;
}
}
void SeqListPrint(SeqList* ps) {
assert(ps);
for (int i = 0; i < ps->size; i++) {
printf("%d ", ps->arr[i]);
}
printf("\n");
}
void SeqListPushBack(SeqList* ps, DataType data) {
assert(ps);
CheckCapacity(ps);
ps->arr[ps->size] = data;
ps->size++;
}
void SeqListPopBack(SeqList* ps) {
assert(ps);
//if (ps->size == 0) {
// return;
//}
if (SeqListEmpty(ps)) {
return;
}
ps->size--;
}
void SeqListPushFront(SeqList* ps, DataType data) {
assert(ps);
for (int i = ps->size - 1; i >= 0; i--) {
ps->arr[i + 1] = ps->arr[i];
}
ps->arr[0] = data;
ps->size++;
CheckCapacity(ps);
}
void SeqListPopFront(SeqList* ps) {
assert(ps);
if (SeqListEmpty(ps)) {
return;
}
for (int i = 0; i < ps->size; i++) {
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
int SeqListFind(SeqList* ps, DataType data) {
assert(ps);
for (int i = 0; i < ps->size; i++) {
if (ps->arr[i] == data) {
return i;
}
}
return -1;
}
void CheckCapacity(SeqList* ps) {
assert(ps);
if (ps->size >= ps->capacity) {
int n = 2; //每次扩容2倍
ps->capacity *= n;
int* new_arr = (int*)malloc(ps->capacity * sizeof(int));
memcpy(new_arr, ps->arr, ps->size * sizeof(ps->arr));
free(ps->arr);
ps->arr = new_arr;
printf("扩容成功!\n");
}
}
void SeqListInsert(SeqList* ps, size_t pos, DataType x) {
assert(ps);
if (pos<0 || pos>ps->size) {
return;
}
for (int i = ps->size - 1; i >= pos; i--) {
ps->arr[i + 1] = ps->arr[i];
}
ps->size++;
ps->arr[pos] = x;
CheckCapacity;
}
void SeqListErase(SeqList* ps, size_t pos) {
assert(ps);
if (pos<0 || pos>ps->size) {
return;
}
for (int i = pos; i < ps->size; i++) {
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
int SeqListSize(SeqList* ps)
{
assert(ps);
return ps->size;
}
int SeqListCapacity(SeqList* ps)
{
assert(ps);
return ps->capacity;
}
//检测顺序表
void TestSeqList1() {
SeqList s;
SeqListInit(&s, 10);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPushBack(&s, 5);
SeqListPushBack(&s, 6);
SeqListPushFront(&s, 0);
//SeqListPushFront(&s, 7);
/*SeqListPushBack(&s, 8);
SeqListPushBack(&s, 9);
SeqListPushBack(&s, 10);
SeqListPushBack(&s, 11);*/
SeqListPrint(&s);
SeqListPopBack(&s);
SeqListPopFront(&s);
SeqListPrint(&s);
SeqListInsert(&s, 5, 5);
//SeqListInit(&s, 20, 20);
SeqListPrint(&s);
SeqListErase(&s, 5);
//SeqListErase(&s, 20);
SeqListPrint(&s);
SeqListDestroy(&s);
}
void TestSeqList() {
TestSeqList1();
}
int SeqListEmpty(SeqList* ps) {
assert(ps);
return 0 == ps->size;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化