YIEAC


私信TA

用户名:yzc233

访问量:1178

签 名:

身处在黑暗的人早已不会在乎什么了

等  级
排  名 3830
经  验 1754
参赛次数 0
文章发表 4
年  龄 0
在职情况 学生
学  校 泰州市省泰中附属初级中学
专  业

  自我简介:

#include <iostream>
#include <cstring>
using namespace std;

#define MAX 1000

//此结构体仅用于表示数据域
struct Data{
char name[10];
int age,num;
};

//创建线性表(顺序表)结构体
struct Slist{
struct Data list[MAX];
int lenth;
};

//1、清空线性表
void refresh(Slist *sl){
sl->lenth = 0;
}

//2、获取线性表的长度
int getlen(Slist *sl){
return sl->lenth;
}

//3、线性表的追加
bool add(Slist *sl,Data data){
if(sl->lenth >= MAX){
cout << "越界,追加失败!";
return false;
}
// sl->lenth++;
sl->list[sl->lenth++] = data;
return true;
}

//4、线性表的删除
void del(Slist *sl,int n){
if(n<0 || n>sl->lenth){
cout << "查无此项!";
return;
}
for(int i=n;i<sl->lenth;i++){
sl->list[i] = sl->list[i+1];
}
sl->lenth--;
}

//线性表的插入
void insert(Slist *sl,int n,Data data){
if(n<0 || n>sl->lenth || sl->lenth+1>=MAX){
cout << "插入失败!";
return;
}
for(int i=sl->lenth;i>=n;i--){
sl->list[i+1] = sl->list[i];
}
sl->lenth++;
sl->list[n] = data;
}

//线性表的修改
void rebuild(Slist *sl,int n,Data data){
if(n<0 || n>sl->lenth || sl->lenth+1>=MAX){
cout << "修改失败!";
return;
}
sl->list[n] = data;
}

//查询所有数据
void searchAll(Slist *sl){
for(int i=0;i<sl->lenth;i++){
printf("%s %d %d\n",sl->list[i].name,sl->list[i].age,sl->list[i].num);
}
}

//关键字查询(按名字查询)
void searchByname(Slist *sl,char *name){
for(int i=0;i<sl->lenth;i++){
if(!strcmp(sl->list[i].name,name)){
printf("%s",sl->list[i].name);
return;
}
}
}

/*
1、清空线性表
2、获取线性表的长度
3、线性表的追加
4、线性表的删除
5、线性表的修改
6、线性表的全部查询
7、线性表的关键字查询
*/

int main(){

Slist list,*sl; //定义线性表
sl = &list;
Data data; //定义数据域
int n; //查询用的项
char name[20]; //关键字
refresh(sl);
do{
scanf("%s %d %d",&data.name,&data.age,&data.num);
if(data.age){
if(!add(sl,data)){
break;
}
}else{
break;
}
}while(1);
searchAll(sl);
cin >> n;
del(sl,n);
searchAll(sl);
cin >> n;
scanf("%s %d %d",&data.name,&data.age,&data.num);
insert(sl,n,data);
searchAll(sl);
return 0;
}

 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区