c语言删除指定位置元素,c语言:【顺序表】静态顺序表的删除指定位置元素Erase、删除指定元素Remove...

#define _CRT_SECURE_NO_WARNINGS 1

#include

#include

#include

#include

#define MAXSIZE 1000

typedef int DateType;

typedef struct SeqList

{

DateType arr[MAXSIZE];

size_t size;

}SeqList;

//打印静态顺序表

void PrintSeqList(SeqList *Seq)

{

assert(Seq);

if (Seq->size == 0)

{

printf("静态顺序表当前为空!\n");

return;

}

for (int index = 0; index size; index++)

{

printf("%d-> ", Seq->arr[index]);

}

}

//初始化

void init(SeqList *Seq)

{

assert(Seq);

memset(Seq->arr, 0, sizeof(DateType)* MAXSIZE);

Seq->size = 0;

}

//尾插

void PushBack(SeqList *Seq,DateType x)

{

if (Seq->size >= MAXSIZE)

{

printf("静态顺序表当前已满,无法插入!\n");

return;

}

Seq->arr[Seq->size++] = x;

}

//void Erase(SeqList *Seq, int pos)

//{

//    assert(Seq);

//    if (Seq->size <= 0)

//    {

//        printf("静态顺序表当前已空,无法删除!\n");

//        return;

//    }

//    for (int index = pos; index size; index++)

//    {

//        Seq->arr[index] = Seq->arr[index + 1];

//    }

//    Seq->size--;

//}

void Remove(SeqList *Seq, DateType x)

{

assert(Seq);

int tag = 0;

if (Seq->size <= 0)

{

printf("静态顺序表当前已空,无法删除!\n");

return;

}

int index = 0;

for (; index size; index++)

{

if (Seq->arr[index] == x)

{

for (; index size; ++index)

{

Seq->arr[index] = Seq->arr[index + 1];

}

--Seq->size;

}

tag = 1;

}

if (tag == 0)

printf("未找到该元素!\n");

}

void Test2(SeqList *Seq)

{

init(Seq);

PushBack(Seq, 0);

PushBack(Seq, 1);

PushBack(Seq, 2);

PushBack(Seq, 3);

PushBack(Seq, 4);

PushBack(Seq, 5);

PrintSeqList(Seq);

printf("\n");

/*Erase(Seq, 2);*/

Remove(Seq, 2);

PrintSeqList(Seq);

printf("\n");

}

int main()

{

/*SeqList Seq;

Test1(&Seq);*/

SeqList Seq;

Seq.size = 0;

Test2(&Seq);

system("pause");

return 0;

}

原文:http://10740184.blog.51cto.com/10730184/1743161

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值