c++实现线性表（以学生增删改查为例）
c++实现线性表（以学生增删改查为例）
cyleft 发表于1年前
c++实现线性表（以学生增删改查为例）
• 发表于 1年前
• 阅读 8
• 收藏 0
• 评论 0
``````#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define MAX 10

using namespace std;
typedef struct student
{

int id;
int age;
int score;
}Stu;
typedef struct List{
Stu data[MAX];
int len;
}L;

void init(List &l)
{
l.len=0;
}

void display_cy(List l)
{
if(l.len>9)
{
cout<<"full!";
return ;
}
for(int i=0;i<l.len;i++)
{
cout<<"age:["<<l.data[i].age<<"],id:["<<l.data[i].id<<"],score:["<<l.data[i].score<<"],len:["<<l.len<<"]."<<endl;
}
printf("cout is ok\n");
}

void insert_cy(List &l,Stu s)
{
l.data[l.len] = s;
l.len++;
printf("Model one insert is ok!\n");
}

void insert_in_cy(List &l)
{
Stu s;
int var_age;
int var_id;
int var_score;
cout<<"Please input id、age and score"<<endl;
cin>>var_age>>var_id>>var_score;
s.age = var_age;
s.id = var_id;
s.score = var_score;
l.data[l.len] = s;
l.len++;
printf("Model two insert is ok!\n");
}

void delete_cy(List &l,int a)
{
for(int i=a;i<l.len;i++)
{
l.data[i]=l.data[i+1];
}
l.len--;
cout<<"delete is ok"<<endl;
}

void exchange_cy(Stu &s1,Stu &s2){
int var_age;
int var_id;
int var_score;
var_score = s1.score;
s1.score = s2.score;
s2.score = var_score;

var_age = s1.age;
s1.age = s2.age;
s2.age = var_age;

var_id = s1.id;
s1.id = s2.id;
s2.id = var_id;

}

void sort_cy(L& l){
for(int i=0;i<l.len;i++){
for(int j=i+1;j<l.len;j++){
if(l.data[i].score<l.data[j].score)
{
exchange_cy(l.data[i],l.data[j]);
}
}
}
}

int main()
{
L l;
init(l);
Stu s1={1,18,80};
Stu s2={1,18,90};
Stu s3={1,18,70};
Stu s4={1,18,20};
insert_cy(l,s1);
insert_cy(l,s2);
insert_cy(l,s3);
insert_cy(l,s4);
insert_in_cy(l);
display_cy(l);
sort_cy(l);
display_cy(l);
return 0;
}
``````

×