当前位置:网站首页>顺序表删除所有值为e的元素
顺序表删除所有值为e的元素
2022-08-09 06:40:00 【又菜又爱玩呜呜呜~】
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
//定义结构体
typedef struct sqlist{
int *data;
int length;
}Sqlist;
//创建线性表
void creatList(Sqlist &L){
//开辟一个大小为 MAXSIZE 的空间
L.data = (int *)malloc(sizeof(int) * MAXSIZE);
//判断开辟是否成功
if (!L.data){
printf("ERROR\n");
} else {
printf("ok\n");
}
printf("please input length\n");
//输入线性表长度
scanf("%d", &L.length);
//循环输入线性表中的值
for (int i = 0 ; i < L.length; i ++){
scanf("%d", &L.data[i]);
}
}
//遍历输出线性表
void TraveList(Sqlist &L){
for (int i = 0 ; i < L.length; i ++){
printf("%2d", L.data[i]);
}
}
//删除表中所有值为e的元素
void deletList(Sqlist &L, int e){
//count计数记录有多少值符合规定
int count = 0;
//如果L中的值不为e则将其放入线性表中,负责不录入
for (int i = 0 ; i < L.length ; i ++){
if (L.data[i] != e){
L.data[count ++] = L.data[i];
}
}
//将表长变为count,表示未被删除的所有元素的个数
L.length = count;
}
int main(){
Sqlist L;
int n;
creatList(L);
TraveList(L);
printf("\n");
printf("what do you want delet?\n");
scanf("%d", &n);
deletList(L,n);
printf("\n");
TraveList(L);
}
边栏推荐
猜你喜欢
图论,二叉树,dfs,bfs,dp,最短路专题
The solution that does not work and does not take effect after VScode installs ESlint
按图搜索1688商品接口(item_search_img-按图搜索1688商品(拍立淘接口)代码对接教程
ZIP压缩包文件删除密码的方法
移远EC20 4G模块拨号相关
e-learning summary
leetcode 之 零移位
The water problem of leetcode
Zero shift of leetcode
db.sqlite3没有“as Data Source“解决方法
随机推荐
workbench 数据导出
install flask
TCP segment of a reassembled PDU
Reverse Engineering
Altium designer软件常用最全封装库,包含原理图库、PCB库和3D模型库
找出数组中不重复的值php
逆向工程
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道
crc计算
P6阿里机试题之2020 斐波那契数
普罗米修斯原理及节点发布
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disab
After the VB.net program is closed, the background is still connected to SQL
mysql summary
C语言实现顺序栈和链队列
细谈VR全景:数字营销时代的宠儿
缓存技术使用
Distributed id generator implementation
语句加锁分析
为什么以太网无法接收大于1500字节的数据包?