//*********************************** |
//功能:删除顺序表中大于X的元素 |
//日期:2017年9月13日 |
//作者:Ryan2019 |
//*********************************** |
#include <iostream> |
using namespace std; |
const int ListlnitSize=0; |
const int Listlnc=10; |
typedef int LElemType; |
struct SList |
{ |
LElemType *elem; |
int length,listsize; |
}; |
bool ListCreate(SList &L, int n,LElemType a[]); //创建顺序表 |
bool DeleteX(SList &L,LElemType x); //删除顺序表中大于X的元素 |
int main() |
{ |
const int i=7; int x=5; |
LElemType a[i]={3,7,1,5,9,6,4}; |
SList L; |
ListCreate(L,i,a); |
cout<< "原顺序表为" <<endl; |
for ( int j=0;j<i;j++){cout<<L.elem[j]<< " " ;} |
DeleteX(L,x); |
cout<<endl<< "删除大于" <<x<< "后的顺序表为" <<endl; |
for (j=0;j<L.length;j++){cout<<L.elem[j]<< " " ;} |
cout<<endl; |
return 0; |
} |
bool ListCreate(SList &L, int n,LElemType a[]) |
{ |
int i; |
L.elem= new LElemType[n+ListlnitSize]; |
if (!L.elem) return false ; |
L.length=n; |
L.listsize=n+ListlnitSize; |
for (i=0;i<n;i++) |
{ |
L.elem[i]=a[i]; |
} |
return true ; |
} |
bool DeleteX(SList &L,LElemType x) |
{ |
int m=0,n; |
for (n=0;n<L.length;n++) |
{ |
if (L.elem[n]<=x) |
{ |
L.elem[m]=L.elem[n]; |
m++; |
} |
} |
L.length=m; |
return true ; |
} |