视频讲解在这里(谢谢各位大佬)
p18 第三题数据结构课后算法题_哔哩哔哩_bilibili
本题代码如下
void deletex(struct sqlist* s, int x)
{
int k = 0;
int i = 0;
for (i = 0; i < s->length; i++)
{
if (s->a[i] != x)//只要不等于x,就将值移到k下标处
{
s->a[k] = s->a[i];
k++;
}
}
s->length = k;
}
完整测试代码
#include
#define Max 10
struct sqlist
{
int a[Max];
int length;
};
void deletex(struct sqlist* s, int x)
{
int k = 0;
int i = 0;
for (i = 0; i < s->length; i++)
{
if (s->a[i] != x)//只要不等于x,就将值移到k下标处
{
s->a[k] = s->a[i];
k++;
}
}
s->length = k;
}
int main()
{
struct sqlist s;
int j = 0;
s.length = 6;
for (j = 0; j < s.length; j++)
scanf("%d", &s.a[j]);
printf("原始顺序表为:");
for (j = 0; j < s.length; j++)
printf("%d", s.a[j]);
deletex(&s, 1);
printf("\n删除x后的顺序表为:");
for (j = 0; j < s.length; j++)
printf("%d", s.a[j]);
return 0;
}