实现顺序表多种操作(removeall、冒泡排序等)

在上一篇实现顺序表基本操作的基础上,继续实现下面几种操作:

1.删除顺序表中所有的指定的值
void seqlistRemoveAll(seqlist* seq, seqType to_delete);
2.获取顺序表元素个数
size_t seqlistSize(seqlist* seq);
3.判定顺序表是否为空
int seqlistEmpty(seqlist* seq);
4.冒泡排序
void seqlistBubbleSort(seqlist* seq);

实现函数:

void seqlistRemoveAll(seqlist* seq, seqType to_delete)//删除元素表中所有指定值
{
    assert(seq);//判断指针是否为空
        size_t i = 0;
        if (seq->size == 0)
        {
            printf("线性表为空\n");
            return;
        }
        for (i = 0; i < seq->size; i++)
        {
            if (seq->arr[i] == to_delete)
            {
                size_t j = i;//j=1
                while ( jsize )//j=1~5
                {
                    seq->arr[j] = seq->arr[j + 1];
                    j++;//j=2
                }
                seq->size--;//size=5
                i--;//i=0
            }
        }
}

size_t seqlistSize(seqlist* seq)//获取元素表个数
{
    assert(seq);//判断指针是否为空
    if (seq->size == NULL)
    {
        printf("元素表已为空\n");
    }
    size_t count= 1;
    while (count size)
    {
        count++;
    }
    return count;
}

int seqlistEmpty(seqlist* seq)//判定顺序表是否为空,为空返回1,否返回0
{
    assert(seq);
    if (seq->size == NULL)
        return 1;
    else
        return 0;
}

void seqlistBubbleSort(seqlist* seq)//对顺序表进行冒泡排序
{
    assert(seq);
    if (seq->size == NULL)
        return;
    int i = 0;
    for (; i < seq->size; i++)
    {
        int j = 0;
        for (; j < seq->size -i- 1; j++)
        {
            if (seq->arr[j]>seq->arr[j + 1])
            {
                seqType tmp = seq->arr[j];
                seq->arr[j] = seq->arr[j + 1];
                seq->arr[j + 1] = tmp;
            }
        }
    }
}

对函数进行测试:

void Test_RemoveAll()//删除所有指定元素
{
    print_seqlist("*****在顺序表中删除所有指定元素*****");
    seqlist seq;
    seqlistInit(&seq);
    seqlistPushBack(&seq, 2);
    seqlistPushBack(&seq, 2);
    seqlistPushBack(&seq, 3);
    seqlistPushBack(&seq, 4);
    seqlistPushBack(&seq, 2);
    seqlistPushBack(&seq, 5);
    seqlistPushBack(&seq, 6);
    seqlistPushBack(&seq, 7);
    PrintSeqList(&seq);
    seqlistRemoveAll(&seq, 2);
    PrintSeqList(&seq);
}

void Test_Size()//获取元素表个数
{
    print_seqlist("*****获取顺序表元素个数*****");
    seqlist seq;
    seqlistInit(&seq);
    seqlistPushBack(&seq, 2);
    seqlistPushBack(&seq, 3);
    seqlistPushBack(&seq, 4);
    seqlistPushBack(&seq, 5);
    seqlistPushBack(&seq, 6);
    seqlistPushBack(&seq, 7);
    PrintSeqList(&seq);
    size_t count = seqlistSize(&seq);
    printf("元素表个数为%d:\n", count);
}

void Test_Empty()//判断元素表是否为空
{
    print_seqlist("*****判断顺序表是否为空*****");
    seqlist seq;
    seqlistInit(&seq);
    seqlistPushBack(&seq, 2);
    seqlistPushBack(&seq, 3);
    seqlistPushBack(&seq, 4);
    seqlistPushBack(&seq, 5);
    int i = seqlistEmpty(&seq);
    printf("ecpect 0,actual :%d\n", i);
}
void Test_BubbleSort()
{
    print_seqlist("*****顺序表冒泡排序*****");
    seqlist seq;
    seqlistInit(&seq);
    seqlistPushBack(&seq, 8);
    seqlistPushBack(&seq, 4);
    seqlistPushBack(&seq, 3);
    seqlistPushBack(&seq, 9);
    seqlistPushBack(&seq, 14);
    seqlistPushBack(&seq, 5);
    PrintSeqList(&seq);
    seqlistBubbleSort(&seq);
    printf("冒泡排序后:\n");
    PrintSeqList(&seq);
}

图片测试结果:
实现顺序表多种操作(removeall、冒泡排序等)_第1张图片
实现顺序表多种操作(removeall、冒泡排序等)_第2张图片
之前有关顺序表的博客:
http://blog.csdn.net/getitstarted/article/details/78826402

你可能感兴趣的:(c语言基础)