设计一个算法删除顺序表中值为x的所有节点 代码和运行结果
时间: 2023-05-28 18:06:03 浏览: 68
算法思路:
1.从顺序表第一个元素开始遍历,遇到值为x的元素就将其删除。
2.删除元素后,将后面的元素向前移动一个位置,直到表尾结束。
3.重复以上步骤,直到顺序表中没有值为x的元素。
代码实现:
```
#include <stdio.h>
#define MAXSIZE 100
typedef struct{
int data[MAXSIZE];
int length;
}SqList;
//删除顺序表中值为x的所有节点
void deleteX(SqList *L, int x){
int i,j;
for(i=0; i<L->length; i++){
if(L->data[i] == x){
for(j=i; j<L->length-1; j++){
L->data[j] = L->data[j+1];
}
L->length--;
i--;
}
}
}
int main(){
SqList L = {{1,2,3,4,5,6,7,8,9,10}, 10};
int x = 3;
deleteX(&L, x);
for(int i=0; i<L.length; i++){
printf("%d ", L.data[i]);
}
return 0;
}
```
运行结果:
```
1 2 4 5 6 7 8 9 10
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)