数据结构与算法编程题4

删除线性表中值在s与t之间的元素(s

#include 
using namespace std;

typedef int ElemType;
#define Maxsize 100
#define	OK 1
#define  ERROR 0
typedef struct SqList
{
	ElemType data[Maxsize];
	int length;
}SqList;

void Init_SqList(SqList& L)
{
	L.length = 0;
}

int delete_somevalue(SqList& L,int s,int t)
{
	if (s >= t)
	{
		cout << "s和t取值不合理" << endl;
		return ERROR;
	}
	if (L.length == 0)
	{
		cout << "顺序表数据为空" << endl;
		return ERROR;
	}
	int k = 0;
	for (int i = 0; i < L.length; i++)
	{
		if (L.data[i]<s||L.data[i]>t)
		{
			L.data[k++] = L.data[i];
		}
	}
	L.length = k;
	return OK;
}
//s= 1 t= 3
//0 1 2 3 4   
//5 3 3 1 10
/*删除线性表中值在s与t之间的元素(s < t),s和t不合理
或者顺序表为空则显示出错信息并退出运行*/
int main(void)
{
	SqList L;
	Init_SqList(L);
	L.data[0] = 5;
	L.data[1] = 3;
	L.data[2] = 3;
	L.data[3] = 1;
	L.data[4] = 10;
	L.length = 5;
	for (int i = 0; i < L.length; i++)
		cout << L.data[i] << "     ";
	cout << endl; 
	delete_somevalue(L, 1, 3);
	for (int i = 0; i < L.length; i++)
		cout << L.data[i] << "     ";
	cout << endl;
	return 0;
}

在这里插入图片描述

你可能感兴趣的:(算法与数据结构,数据结构)