最近在学数据结构与算法里面的顺序表
就是简单的插入新的学生信息
#include
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#define Size 10
typedef struct stu
{
int xuehao;
char jiankan[20];
char sex[8];
}stu;
typedef struct biao {
stu* head;
int length;
int size;
}biao;
biao initbiao() {
biao *t= new biao;
t->length = 0;
t->size = Size;
return *t;
}
biao addbiao(biao t, stu elem, int add)
{
int i;
if (add > t.length + 1 || add < 1) {
cout<<“插入位置有问题”;
return t;
}
if (t.length >= t.size) {
;
if (!t.head) {
cout<<“存储分配失败”;
}
t.size += 1;
}
for (i = t.length - 1; i >= add - 1; i–) {
t.head[i]= t.head[i-1];
}
t.head[add - 1]= elem;
t.length++;
return t;
}
void output(stu *head) {
cout << “学生健康情况:” << endl;
cout << head->jiankan;
cout << “学生学号:” << endl;
cout <xuehao;
cout << “学生性别:” << endl;
cout <sex;
}
void displaybiao(biao t) {
int i;
for (i = 0; i < sizeof(t); i++) {
output(&t.head[i]);
}
cout<<“n”;
}
最后运行时
void shuru(biao l,int i) {
cout << “输入学生的健康情况” << endl;
cin>>l.head[i].jiankan;
cout << “输入学生的性别”<<endl;
cin >> l.head[i].sex;
cout << “输入学生的学号” << endl;
cin>>l.head[i].xuehao;
}
stu shuru2(stu& e) {
cin>>e.jiankan;
cin>>e.sex;
cin>>e.xuehao;
return e;
}
int main() {
int num = 0;
int cha = 0;
stu e;
biao a;
a = initbiao();
cout << “输入总人数” << endl;
cin >> num;
for (int i = 0; i < num; i++) {
shuru(a,i);
}
displaybiao(a);
cout<<“输入要加入的学生的健康、性别、学号:”;
shuru2(e);
cout<<“输入要插入的位置:”;
cin>>cha;
a = addbiao(a, e, cha);
displaybiao(a);
return 0;
}
最后运行时候 弹出 istream的文件 说访问权限问题