2.7 顺序表从前删除数据
void SeqListPopFront(SeqList* ps) { assert(ps); assert(ps->size > 0); int i = 0; for (i = 0; i < ps->size; i++) { ps->a[i] = ps->a[i + 1]; } ps->size--; }
删除第一个首元素,实际上就是把第一个元素后面的元素一次从前覆盖,然后对有效数据个数-1,即可
运行测试:
void text2() { SeqList s; SeqListInit(&s); SeqListPushFront(&s, 1); SeqListPushFront(&s, 2); SeqListPushFront(&s, 3); SeqListPushFront(&s, 4); SeqListPrint(&s); SeqListPopFront(&s); SeqListPopFront(&s); SeqListPrint(&s); } int main() { text2(); return 0; }
结果如下:
2.8 查找功能实现
int SeqListFind(SeqList* ps, SLDateType x) { int i = 0; for (i = 0; i < ps->size; i++) { if (ps->a[i] == x) { return i; } } return -1; }
这里我们是以内容查找,对有效的数据进行循环,找到内容一致的元素就返回该下标,否则返回-1。
运行测试:
void text3() { SeqList s; SeqListInit(&s); SeqListPushBack(&s, 1); SeqListPushBack(&s, 2); SeqListPushBack(&s, 3); SeqListPushBack(&s, 4); SLDateType x = 0; printf("输入你要查找的内容>"); scanf("%d", &x); int i = SeqListFind(&s, x); printf("你所查找的元素在:%d", i); } int main() { text3(); return 0; }
结果如下:
2.9 顺序表在pos位置插入
void SeqListInsert(SeqList* ps, int pos, SLDateType x) { assert(ps); assert(pos >= 0 && pos <ps->size); SLCheckCapacity(ps); int i = 0; for (i = ps->size; i >=pos; i--) { ps->a[i + 1] = ps->a[i]; } ps->a[pos] = x; ps->size++; }
对于pos的大小范围要求是0~ps->size,既然要在pos位置插入那么我们就行需要将pos之后的有效元素后移,然后再给pos位置赋值要插入的内容。
运行测试:
void text4() { SeqList s; SeqListInit(&s); SeqListPushBack(&s, 1); SeqListPushBack(&s, 2); SeqListPushBack(&s, 3); SeqListPushBack(&s, 4); SeqListPrint(&s); SeqListInsert(&s, 1, 5); SeqListInsert(&s, 2, 6); SeqListPrint(&s); } int main() { text4(); return 0; }
结果如下:
2.10 顺序表在pos位置删除
void SeqListErase(SeqList* ps, int pos) { assert(ps); assert(pos >= 0 && pos <= ps->size); int i = 0; for (i = pos+1; i < ps->size; i++) { ps->a[i-1] = ps->a[i]; } ps->size--; }
这里和上面头删除的原理相同,只是这里是将pos位置之后的值进行覆盖。
运行测试:
void text4() { SeqList s; SeqListInit(&s); SeqListPushBack(&s, 1); SeqListPushBack(&s, 2); SeqListPushBack(&s, 3); SeqListPushBack(&s, 4); SeqListPrint(&s); SeqListInsert(&s, 1, 5); SeqListInsert(&s, 2, 6); SeqListPrint(&s); SeqListErase(&s, 2); SeqListErase(&s, 3); SeqListPrint(&s); } int main() { text4(); return 0; } 结果如下: 总代码: text.c #include"Sqlist.h" void text1() { SeqList s; SeqListInit(&s); SeqListPushBack(&s,1); SeqListPushBack(&s, 2); SeqListPushBack(&s, 3); SeqListPushBack(&s, 4); SeqListPrint(&s); SeqListPopBack(&s); SeqListPopBack(&s); SeqListPrint(&s); } void text2() { SeqList s; SeqListInit(&s); SeqListPushFront(&s, 1); SeqListPushFront(&s, 2); SeqListPushFront(&s, 3); SeqListPushFront(&s, 4); SeqListPrint(&s); SeqListPopFront(&s); SeqListPopFront(&s); SeqListPrint(&s); } void text3() { SeqList s; SeqListInit(&s); SeqListPushBack(&s, 1); SeqListPushBack(&s, 2); SeqListPushBack(&s, 3); SeqListPushBack(&s, 4); SLDateType x = 0; printf("输入你要查找的内容>"); scanf("%d", &x); int i = SeqListFind(&s, x); printf("你所查找的元素在:%d", i); } void text4() { SeqList s; SeqListInit(&s); SeqListPushBack(&s, 1); SeqListPushBack(&s, 2); SeqListPushBack(&s, 3); SeqListPushBack(&s, 4); SeqListPrint(&s); SeqListInsert(&s, 1, 5); SeqListInsert(&s, 2, 6); SeqListPrint(&s); SeqListErase(&s, 2); SeqListErase(&s, 3); SeqListPrint(&s); } int main() { text4(); return 0; } Sqlist.c #include<stdio.h> #include"Sqlist.h" void SeqListInit(SeqList* ps) { assert(ps); ps->size = 0; ps->a = (SLDateType*)malloc(sizeof(SLDateType) * INIT_CAPACITY); if (ps->a == NULL) { perror("malloc fail"); return; } ps->capacity = INIT_CAPACITY; } void SeqListDestroy(SeqList* ps) { assert(ps); free(ps->a); ps->a = NULL; ps->size = 0; ps->capacity = 0; } void SLCheckCapacity(SeqList* ps) { assert(ps); if (ps->size == ps->capacity) { SLDateType* temp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * ps->capacity * 2); if (temp == NULL) { perror("realloc fail"); return; } ps->a = temp; ps->capacity *= 2; } } void SeqListPrint(SeqList* ps) { assert(ps); int i = 0; for (i = 0; i < ps->size; i++) { printf("%d ", ps->a[i]); } printf("\n"); } void SeqListPushBack(SeqList* ps, SLDateType x) { assert(ps); SLCheckCapacity(ps); ps->a[ps->size++] = x; } void SeqListPopBack(SeqList* ps) { assert(ps); assert(ps->size > 0); ps->size--; } void SeqListPushFront(SeqList* ps, SLDateType x) { assert(ps); SLCheckCapacity(ps); int i = 0; for (i = ps->size; i >=0; i--) { ps->a[i + 1] = ps->a[i]; } ps->a[0] = x; ps->size++; } void SeqListPopFront(SeqList* ps) { assert(ps); assert(ps->size > 0); int i = 0; for (i = 0; i < ps->size; i++) { ps->a[i] = ps->a[i + 1]; } ps->size--; } int SeqListFind(SeqList* ps, SLDateType x) { int i = 0; for (i = 0; i < ps->size; i++) { if (ps->a[i] == x) { return i; } } return -1; } void SeqListInsert(SeqList* ps, int pos, SLDateType x) { assert(ps); assert(pos >= 0 && pos <ps->size); SLCheckCapacity(ps); int i = 0; for (i = ps->size; i >=pos; i--) { ps->a[i + 1] = ps->a[i]; } ps->a[pos] = x; ps->size++; } void SeqListErase(SeqList* ps, int pos) { assert(ps); assert(pos >= 0 && pos <= ps->size); int i = 0; for (i = pos+1; i < ps->size; i++) { ps->a[i-1] = ps->a[i]; } ps->size--; } Sqlist.h #include <stdio.h> #include <assert.h> #include <stdlib.h> #define INIT_CAPACITY 4 typedef int SLDateType; typedef struct SeqList { SLDateType* a; int size; int capacity; }SeqList; // 对数据的管理:增删查改 void SeqListInit(SeqList* ps); void SeqListDestroy(SeqList* ps); void SeqListPrint(SeqList* ps); void SeqListPushBack(SeqList* ps, SLDateType x); void SeqListPushFront(SeqList* ps, SLDateType x); void SeqListPopFront(SeqList* ps); void SeqListPopBack(SeqList* ps); // 顺序表查找 int SeqListFind(SeqList* ps, SLDateType x); // 顺序表在pos位置插入x void SeqListInsert(SeqList* ps, int pos, SLDateType x); // 顺序表删除pos位置的值 void SeqListErase(SeqList* ps, int pos);