您的位置:首页 > 其它

第三周-删除线性表的元素

2015-10-04 17:49 330 查看
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
#define MaxSize 50
typedef struct
{
ElemType data[MaxSize];
int length;
} SqList;

//用数组创建线性表
void CreateList(SqList *&L, ElemType a[], int n)
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for (i=0; i<n; i++)
L->data[i]=a[i];
L->length=n;
}
bool ListEmpty(SqList *L)
{
return(L->length==0);
}
void DispList(SqList *L)
{
int i;
if (ListEmpty(L)) return;
for (i=0; i<L->length; i++)
printf("%d ",L->data[i]);
printf("\n");
}
void delx2y(SqList *&L, ElemType x,  ElemType y)
{
int k=0,i; //k记录非x的元素个数
ElemType t;
if(x>y)
{
t=x;
x=y;
y=t;
}
for (i=0; i<L->length; i++)
if (L->data[i]<x || L->data[i]>y )  //复制不在[x, y]之间的元素
{
L->data[k]=L->data[i];
k++;
}
L->length=k;
}

//用main写测试代码
int main()
{
SqList *sq;
ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};
CreateList(sq, a, 10);
printf("删除前 ");
DispList(sq);

delx2y(sq, 4, 7);

printf("删除后 ");
DispList(sq);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: