#define SeqListMaxSize 1000
typedef char SeqType;
typedef struct SeqList {
SeqType data[SeqListMaxSize];
size_t size;
} SeqList;
1、头插尾插:
void SeqListPushBack(SeqList* seq, SeqType value){
assert(seq);
if (seq->size >= SeqListMaxSize){
return;
}
seq->data[seq->size] = value;
seq->size++;
}
void SeqListPushFront(SeqList* seq, SeqType value){
assert(seq);
if (seq->size >= SeqListMaxSize){
return;
}
size_t i = seq->size;
for (; i > 0; --i){
seq->data[i + 1] = seq->data[i];
}
seq->data[1] = seq->data[0];//注意当i=0时,要将第一个元素放到第二个元素那里
seq->data[0] = value;
++seq->size;
}
2、任意位置插入元素
void SeqListInsert(SeqList* seq, size_t pos, SeqType value){
assert(seq);
if (seq->size >= SeqListMaxSize){
return;
}
if (pos > seq->size){
return;
}
++seq ->size;
size_t i = seq->size - 1;
for (; i > pos; --i){
seq->data[i] = seq->data[i - 1];
}
seq ->data[pos] = value;
}
3、删除指定元素(Erase)、删除给定的值(Remove)
void SeqListErase(SeqList* seq, size_t pos){
assert(seq);
if (pos >=seq->size){
return;
}
if (seq->size == 0){
return;
}
size_t i = pos;
for (; i < seq->size - 1; ++i){
seq->data[i] = seq->data[i + 1];
}
--seq->size;
/*if (pos == 0){
SeqListPopFront(&seq);
}
else if (pos == seq->size - 1){
SeqListPopBack(&seq);
}
else{*/ //下面的的循环已经考虑了头删尾删以及其他位置删除的情况
}
void SeqListRemove(SeqList* seq, SeqType to_delete){
assert(seq);
size_t pos = SeqListFind(seq, to_delete);
if (pos ==(size_t)-1){
return;
}
SeqListErase(seq, pos);
}
4、查改
size_t SeqListFind(SeqList* seq, SeqType value){
assert(seq);
size_t i = 0;
for (; i < seq->size; ++i){
if (seq->data[i] == value){
return i;
}
}
return (size_t)-1;
}
void SeqListSet(SeqList* seq, size_t pos, SeqType value){
assert(seq);
if (pos < seq->size){
seq->data[pos] = value;
}
return;
}
SeqList.h文件:
#pragma once
#include
#define SeqListMaxSize 1000
typedef char SeqType;
typedef struct SeqList {
SeqType data[SeqListMaxSize];
size_t size;
} SeqList;
/**
* @brief 给一个顺序表进行初始化
*
* @param seq 表示一个顺序表结构的指针
*/
void SeqListInit(SeqList* seq);
/**
* @brief 将一个元素插入到顺序表的末尾
*
* @param seq 顺序表结构的指针
* @param value 新插入的值
*/
void SeqListPushBack(SeqList* seq, SeqType value);
/**
* @brief 将顺序表的最后一个元素删除
*
* @param seq 顺序表结构的指针
*/
void SeqListPopBack(SeqList* seq);
/**
* @brief 往顺序表的前面插入一个元素
*
* @param seq 顺序表结构的指针
* @param value 新插入的值
*/
void SeqListPushFront(SeqList* seq, SeqType value);
/**
* @brief 删除顺序表的第一个元素
*
* @param seq 顺序表结构的指针
*/
void SeqListPopFront(SeqList* seq);
/**
* @brief 取顺序表中任意位置的一个元素
*
* @param seq 顺序表的指针
* @param pos 要取的元素的下标
* @param default_value 如果取元素失败, 就返回这个值
*
* @return 对应下标位置的元素的值
*/
SeqType SeqListGet(SeqList* seq, size_t pos, SeqType default_value);
/**
* @brief 将顺序表中指定位置的值进行设置
*
* @param seq 顺序表的结构指针
* @param pos 要修改的顺序表的元素位置
* @param value 要设置的值.
*/
void SeqListSet(SeqList* seq, size_t pos,SeqType value);
/**
* @brief 查找顺序表中指定元素的下标
*
* @param seq 顺序表结构指针
* @param value 要查找的值
*
* @return 对应值的下标. 如果找不到, 就返回
* (size_t)-1
*/
size_t SeqListFind(SeqList* seq, SeqType value);
/**
* @brief 在指定位置插入元素
*
* @param seq 顺序表指针
* @param pos 要插入的位置
* @param value 要插入的值.
*/
void SeqListInsert(SeqList* seq, size_t pos,SeqType value);
/**
* @brief 删除指定位置的元素
*
* @param seq 顺序表指针
* @param pos 要删除的元素位置
*/
void SeqListErase(SeqList* seq, size_t pos);
/**
* @brief 删除顺序表中指定的值, 如果存在重复元素, 只删除第一个
*
* @param seq 顺序表指针
* @param to_delete 待删除的值
*/
void SeqListRemove(SeqList* seq, SeqType to_delete);
/**
* @brief 删除顺序表中所有的指定的值, 另外要实现一个时间复杂度为 O(N) 的优化版本
*
* @param seq 顺序表指针
* @param to_delete 待删除的元素
*/
void SeqListRemoveAll(SeqList* seq, SeqType to_delete);
/**
* @brief 获取顺序表元素个数
*
* @param seq 顺序表指针
*
* @return 顺序表元素的个数
*/
size_t SeqListSize(SeqList* seq);
/**
* @brief 判定顺序表是否为空
*
* @param seq 顺序表指针
*
* @return 如果顺序表为空, 返回 1; 否则返回 0
*/
int SeqListEmpty(SeqList* seq);
/**
* @brief 冒泡排序
*
* @param seq 顺序表指针
*/
void SeqListBubbleSort(SeqList* seq);
/**
* @brief 选择排序
*
* @param seq 顺序表指针
*/
void SeqListSelectSort(SeqList* seq);
/**
* @brief 顺序表的二分查找
*
* @param seq 顺序表指针
*/
size_t SeqListBinarySearch(SeqList* seq, SeqType value);
/**
* @brief 顺序表的销毁
*
* @param seq 顺序表指针
*/
void SeqListDestroy(SeqList* seq);
SeqList.c文件:
#include"Seqlist.h"
#include
void SeqListInit(SeqList* seq){
assert(seq);
seq->size = 0;
}
void Display(SeqList* seq){
assert(seq);
size_t i = 0;
for (; i size; i++){
printf("%d:[%c:%p]---", i, seq->data[i], &seq->data[i]);
}
printf("\n");
}
void SeqListPushBack(SeqList* seq, SeqType value){
assert(seq);
if (seq->size >= SeqListMaxSize){
return;
}
seq->data[seq->size] = value;
seq->size++;
}
void SeqListPopBack(SeqList* seq){
assert(seq);
if (seq->size == 0){
return;
}
--seq->size;
}
void SeqListPushFront(SeqList* seq, SeqType value){
assert(seq);
if (seq->size >= SeqListMaxSize){
return;
}
size_t i = seq->size;
for (; i > 0; --i){
seq->data[i + 1] = seq->data[i];
}
seq->data[1] = seq->data[0];//注意当i=0时,要将第一个元素放到第二个元素那里
seq->data[0] = value;
++seq->size;
}
void SeqListPopFront(SeqList* seq){
assert(seq);
if (seq->size == 0){
return;
}
size_t i =0;
for (; i size-1; i++){
seq->data[i] = seq->data[i + 1];
}
--(seq->size);
return;
}
SeqType SeqListGet(SeqList* seq, size_t pos, SeqType default_value){
//default_value 如果取元素失败, 就返回这个值
assert(seq);
//assert(pos);size_t 的数据总是大于0
size_t i = 0;
if (pos >= 0 && pos < seq->size){
return seq->data[pos];
}
return default_value;
}
void SeqListSet(SeqList* seq, size_t pos, SeqType value){
assert(seq);
if (pos < seq->size){
seq->data[pos] = value;
}
return;
}
size_t SeqListFind(SeqList* seq, SeqType value){
assert(seq);
size_t i = 0;
for (; i < seq->size; ++i){
if (seq->data[i] == value){
return i;
}
}
return (size_t)-1;
}
void SeqListInsert(SeqList* seq, size_t pos, SeqType value){
assert(seq);
if (seq->size >= SeqListMaxSize){
return;
}
if (pos > seq->size){
return;
}
++seq ->size;
size_t i = seq->size - 1;
for (; i > pos; --i){
seq->data[i] = seq->data[i - 1];
}
seq ->data[pos] = value;
}
void SeqListErase(SeqList* seq, size_t pos){
assert(seq);
if (pos >=seq->size){
return;
}
if (seq->size == 0){
return;
}
size_t i = pos;
for (; i < seq->size - 1; ++i){
seq->data[i] = seq->data[i + 1];
}
--seq->size;
/*if (pos == 0){
SeqListPopFront(&seq);
}
else if (pos == seq->size - 1){
SeqListPopBack(&seq);
}
else{*/ //下面的的循环已经考虑了头删尾删以及其他位置删除的情况
}
void SeqListRemove(SeqList* seq, SeqType to_delete){
assert(seq);
size_t pos = SeqListFind(seq, to_delete);
if (pos ==(size_t)-1){
return;
}
SeqListErase(seq, pos);
}
void SeqListRemoveAll(SeqList* seq, SeqType to_delete){
assert(seq);
assert(seq);
while (1){
size_t pos = SeqListFind(seq, to_delete);
if (pos == (size_t)-1){
return;
}
SeqListErase(seq, pos);
}
return;
}
size_t SeqListSize(SeqList* seq){
assert(seq);
if (seq->size == 0){
return 0;
}
return seq->size;
}
int SeqListEmpty(SeqList* seq){
assert(seq);
if (seq->size == 0){
return 1;
}
return 0;
}
void Swap(char* a, char* b){
assert(a);
assert(b);
SeqType tmp = *a;
*a = *b;
*b = tmp;
return;
}
void SeqListBubbleSort(SeqList* seq){
assert(seq);
size_t i = 0;
for (; i < seq->size - 1; ++i){
size_t j = 0;
for (; j < seq->size - i - 1; ++j){
if (seq->data[j]>seq->data[j + 1]){
Swap(&seq->data[j], &seq->data[j + 1]);
}
}
}
}
//int cmp(SeqType a, SeqType b){
// return a > b ? 1 : 0;
//}
//void SeqListBubbleSortEx(SeqList* seq, int(*cmp)(SeqType, SeqType)){
// assert(seq);
// size_t i = 0;
// for (; i < seq->size - 1; ++i){
// size_t j = 0;
// for (; j < seq->size - i - 1; ++j){
// if (cmp(seq->data[j],seq->data[j+1])){
// Swap(&seq->data[j], &seq->data[j + 1]);
// }
// }
// }
//}
void SeqListSelectSort(SeqList* seq){
assert(seq);
size_t i = 0;
for (; i < seq->size; ++i){
size_t k = i;
for (; k < seq->size; ++k){
if (seq->data[k] < seq->data[i]){
Swap(&seq->data[k], &seq->data[i]);
}
}
}
}
size_t SeqListBinarySearch(SeqList* seq, SeqType value){
assert(seq);
size_t beg = 0; size_t end = seq->size;
while (beg < end){
size_t mid = beg + (end - beg) / 2;
if (value>seq->data[mid]){
beg = mid + 1;
}
else if (value < seq->data[mid]){
end = mid - 1;
}
else
return mid;
}
}
void SeqListDestroy(SeqList* seq){
assert(seq);
seq->size = 0;
}
test.c文件:
#if 1
#include
#include
#include"Seqlist.h"
#define TEST_HEADER printf("\n===================%s===============\n",__FUNCTION__);
void testSeqListInit(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("seq.size是:%d\n", seq.size);
}
void testSeqListPushBack(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->siz:%d\n", seq.size);
Display(&seq);
}
void testSeqListPopBack(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->siz:%d\n", seq.size);
Display(&seq);
printf("尾删一个元素:");
SeqListPopBack(&seq);
printf("seq->siz:%d\n", seq.size);
Display(&seq);
}
void testSeqListPushFront(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->siz:%d\n", seq.size);
Display(&seq);
printf("头插两个元素:");
SeqListPushFront(&seq, 'e');
SeqListPushFront(&seq, 'f');
printf("seq->siz:%d\n", seq.size);
Display(&seq);
}
void testSeqListPopFront(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->size:%d\n", seq.size);
Display(&seq);
printf("头删一个元素:");
SeqListPopFront(&seq);
printf("seq->size:%d\n", seq.size);
Display(&seq);
}
void testSeqListGet(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->size:%d\n", seq.size);
Display(&seq);
printf("查找一个存在的位置:");
int ret=SeqListGet(&seq, 2, -1);
printf("ret expect is 'c'(99),actual is %d\n", ret);
printf("查找一个不存在的位置:");
ret = SeqListGet(&seq, 2, -1);
printf("ret expect is -1,actual is %d\n", ret);
}
void testSeqListSet(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->size:%d\n", seq.size);
Display(&seq);
SeqListSet(&seq, 1, 'e');
SeqListSet(&seq, 2, 'f');
SeqListSet(&seq, 3, 'g');
printf("seq->size:%d\n", seq.size);
printf("seq expect is'a-e-f-g' ,actual is :" );
Display(&seq);
}
void testSeqListFind(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->size:%d\n", seq.size);
Display(&seq);
printf("查找一个存在的元素a:");
int ret = SeqListFind(&seq, 'a');
printf("ret expect is 0,actual is %d\n", ret);
printf("查找一个不存在的元素f:");
ret = SeqListFind(&seq, 'f');
printf("ret expect is very big number,actual is %lu\n", ret);
}
void testSeqListInsert(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->size:%d\n", seq.size);
Display(&seq);
printf("非法位置插入元素:");
SeqListInsert(&seq, 5, 'z');
printf("seq expect is'a-b-c-d',actual is:");
Display(&seq);
printf("seq->size expect is 4,actual :%d\n", seq.size);
printf("合法位置插入元素:");
SeqListInsert(&seq, 2, 'z');
printf("seq expect is'a-b-z-c-d',actual is:");
Display(&seq);
printf("seq->sizeexpect is 5,actual:%d\n", seq.size);
}
void testSeqListErase(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->size:%d\n", seq.size);
Display(&seq);
SeqListErase(&seq, 0);
printf("seq->size expect is 3 actual is:%d\n", seq.size);
Display(&seq);
SeqListErase(&seq, 1);
printf("seq->size expect is 2 actual is:%d\n", seq.size);
Display(&seq);
}
void testSeqListRemove(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->size:%d\n", seq.size);
Display(&seq);
printf("删除一个存在的单独元素d:");
SeqListRemove(&seq, 'd');
printf("seq->size 3vs:%d\n", seq.size);
Display(&seq);
printf("删除一个不存在的元素f:");
SeqListRemove(&seq, 'f');
printf("seq->size3 vs:%d\n", seq.size);
Display(&seq);
printf("删除一个存在的重复元素a:");
SeqListRemove(&seq, 'a');
printf("seq->size 2vs:%d\n", seq.size);
Display(&seq);
}
void testSeqListRemoveAll(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->size:%d\n", seq.size);
Display(&seq);
SeqListRemoveAll(&seq, 'a');
printf("seq->size:%d\n", seq.size);
Display(&seq);
}
void testSeqListSize(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
printf("seq->size:%d\n", SeqListSize(&seq));
Display(&seq);
}
void testSeqListBubbleSort(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插四个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 's');
SeqListPushBack(&seq, 'g');
SeqListPushBack(&seq, 'e');
printf("seq->size:%d\n", SeqListSize(&seq));
Display(&seq);
SeqListBubbleSort(&seq);
Display(&seq);
}
void testSeqListBinarySearch(){
TEST_HEADER;
SeqList seq;
SeqListInit(&seq);
printf("尾插五个元素:");
SeqListPushBack(&seq, 'a');
SeqListPushBack(&seq, 'b');
SeqListPushBack(&seq, 'c');
SeqListPushBack(&seq, 'd');
SeqListPushBack(&seq, 'e');
printf("seq->size:%d\n", SeqListSize(&seq));
Display(&seq);
printf("查找元素e:");
size_t ret=SeqListBinarySearch(&seq, 'e');
printf("e expect is 4,actual:%lu", ret);
}
int main(){
//testSeqListInit();
//testSeqListPushBack();
//testSeqListPushFront();
//testSeqListPopBack();
//testSeqListPopFront();
//testSeqListGet();
//testSeqListSet();
//testSeqListFind();
//testSeqListInsert();
//testSeqListErase();
//testSeqListRemove();
//testSeqListRemoveAll();
//testSeqListSize();
//testSeqListBubbleSort();
testSeqListBinarySearch();
system("pause");
return 0;
}
#endif