题目要求:
代码实现:
(与第三题思想完全相同)
#include<stdio.h>
#define ElemType int
#define MaxSize 100
typedef struct{
ElemType data[MaxSize];
int Length;
}SqList;
void InitList(SqList &S)
{
for(int i=0;i<MaxSize;i++)S.data[i]=0;
S.Length=0;
}
bool ListInsert(SqList &S,int i,ElemType e)
{
if(i<1||i>S.Length+1)return false;
if(S.Length>=MaxSize)return false;
for(int j=S.Length;j>=i;j--)
{
S.data[j]=S.data[j-1];
}
S.data[i-1]=e;
S.Length++;
return true;
}
//删除所有的大小在5到10之间的所有元素:
bool Del_s_t(SqList &S,ElemType s,ElemType t)
{
int i,k=0;
if(s>=t||S.Length==0)return false;
for(i=0;i<S.Length;i++)
{
if(S.data[i]>=s&&S.data[i]<=t)k++;
else S.data[i-k]=S.data[i];
}
S.Length-=k;
return true;
}
void ListShow(SqList &S)
{
printf("输出删除所有的大小在5到10之间的所有元素之后的整个顺序表:\n");
for(int i=0;i<S.Length;i++)
{
printf("%d ",S.data[i]);
}
printf("\n");
}
int main()
{
SqList S;
InitList(S);
printf("输入数值:\n");
int e,i=0;
while(scanf("%d",&e)!=EOF)
{
i++;
ListInsert(S,i,e);
}
Del_s_t(S,5,10);
ListShow(S);
return 0;
}