删除顺序表中所有值为x的结点

顺序表定义:

typedef struct
{
	DataType list[MaxSize];
	int length;
}SeqList;

在顺序表的第i个位置插入元素:

void ListInitiate(SeqList* L)
{
	L->length = 0;
}
int Sqlist_insert(SeqList* L, int i, DataType x)
{
	int j;
	if (L->length >= MaxSize)
		return 0;
	else if (i<1 || i>L->length + 1)
		return 0;
	else
	{
		for (j = L->length; j >= i; j--)
			L->list[j] = L->list[j - 1];
		L->list[i - 1] = x;
		L->length++;
		return 1;
	}

}

删除顺序表中所有值为x的结点:

void ListDelete(SeqList *L, int x)
{
	int k = 0;
	for (int i = 0; i < L->length; i++)
	{
		if (L->list[i] == x)
			k++;   
		else
			L->list[i - k] = L->list[i];  
									  
	}
	L->length -= k;
}

主函数设计测试:

#include<stdio.h>
#define MaxSize 100
#define MaxLen 10
typedef int DataType;
#include"SeqList.h";
int main()
{
	SeqList L;
	int i, x, e;
	ListInitiate(&L);
	Sqlist_insert(&L, 1, 2);
	Sqlist_insert(&L, 2, 3);
	Sqlist_insert(&L, 3, 5);
	Sqlist_insert(&L, 4, 4);
	Sqlist_insert(&L, 5, 6);
	Sqlist_insert(&L, 6, 1);
	Sqlist_insert(&L, 7, 4);
	Sqlist_insert(&L, 8, 7);
	Sqlist_insert(&L, 9, 4);
	Sqlist_insert(&L, 10, 0);
	for (i = 0; i < L.length; i++)
	{
		printf("%d ", L.list[i]);
	}
	printf("\n");
	ListDelete(&L, 4);
	for (i = 0; i < L.length; i++)
	{
		printf("%d ", L.list[i]);
	}
}

测试结果:

 

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

离~殇~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值