从顺序表L中删除所有值为x的元素

#include <stdio.h>
#define MAXSIZE 100
typedef struct
{
int elem[MAXSIZE];
int last;
}SeqList;
int creat(SeqList &L)
{
int a;
printf("请输入要创建的元素的个数:\n");
scanf("%d", &a);
L.last=0;
for (int i = 0;i<a;i++)
{
printf("请输入第%d个元素\n", i + 1);
scanf("%d", &L.elem[i]);
L.last++;
} 
return L.last;
}
void show(SeqList L)
{
int i;
printf("creat last=%d\n", L.last);
printf("线性表中的元素为:\n");
for (i = 0;i<L.last;i++)
printf("%d\n", L.elem[i]);
printf("\n");
}


void delx(SeqList *L, int x)   //删除所有值为x的元素
{
int i = 0;
int j = 0;
while (i <= L->last)
{  
if (L->elem[i] != x)
{
L->elem[j] = L->elem[i];
i++;
j++;
}
else i++;
}
L->last = j - 1;
printf("creat last=%d\n", L->last);


}


void main()
{
SeqList L;
int a,i,length;
L.last=creat(L);
show(L);
printf("请输入删除的数值:\n");
scanf("%d", &a);
delx(&L, a);
printf("删除后的线性表为:\n");
for (i = 0;i < L.last;i++)
printf("%d\n", L.elem[i]);
}

猜你喜欢

转载自blog.csdn.net/whg841001/article/details/72900193
今日推荐