1、#include "stdafx.h"#include<iostream>using namespace std;#define LISTSIZE 100#define LISTINCREMENT 100struct Student{ char ID[100]; char name[100]; char grade[100]; int age; char sex[100];};//确定数据类型struct Sqlist{ int *elem; int length; int listsize; Student student[100];};//建立链表int InitSqlist (Sqlist&L){ L.elem =(int *)malloc(LISTSIZE*sizeof(int)); L.length =0; L.listsize =LISTSIZE; return 1;}int display(Sqlist&sqlist,int N){ cout<<"学号\t"<<"姓名\t"<<"性别\t"<<"年龄\t"<<"班级\t"<<endl; for(int i=1;i<=N;++i) cout<<sqlist.student[i].ID<<"\t"<<sqlist.student[i].name<<"\t"<<sqlist.student[i].sex <<"\t"<<sqlist.student[i].age <<"\t"<<sqlist.student[i].grade <<endl; return 1;}Sqlist input(Sqlist&sqlist,int N){ cout<<"请输入学生的学号,姓名,性别,年龄,班级"<<endl; for(int i=1;i<=N;++i) { sqlist.length++; cin>>sqlist.student[i].ID>>sqlist.student[i].name>>sqlist.student [i].sex >>sqlist.student [i].age >>sqlist.student [i].grade ; } return sqlist;}Sqlist SqlistInsert(Sqlist&L,int i){ if(L.length<=LISTSIZE) { int* newspace=(int*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int)); L.elem =newspace; L.listsize+=1; } L.length++; Student* p=(&L.student[i]); Student* m=(&L.student[L.length]); for(;p<=m;m--) { *(m+1)=*m; } cout<<"请输入学号,姓名,性别,年龄,班级"<<endl; cin>>L.student[i].ID>>L.student[i].name>>L.student [i].sex >>L.student [i].age >>L.student [i].grade ; return L;}Sqlist SqlistDelete(Sqlist&L,int i){ L.length --; Student *p=(&L.student[i]); Student *m=(&L.student[L.length] ); for(;p<=m;p++) { *p=*(p+1); } return L;}int compare(Sqlist&sqlist,char *q,int i){ if (strcmp(q,sqlist.student [i].ID)==0 ) return 1; else return 0;} int _tmain(int argc, _TCHAR* argv[]){ Sqlist sqlist;//成员声明 InitSqlist(sqlist);//顺序表初始化 int N; cout<<"请输入学生的数量N"<<endl; cin>>N; input(sqlist,N); display(sqlist,N); int i; cout<<"请输入插入学生的位序i"<<endl; cin>>i; sqlist=SqlistInsert(sqlist,i); int n=N+1; display(sqlist,n); cout<<"请输入删除学生的位序i"<<endl; cin>>i; sqlist=SqlistDelete(sqlist,i); n=N; display(sqlist,n); char q[10]; cout<<"请输入欲搜索的学生的学号"<<endl; cin>>q; for(int i=0;i<=n;i++) { if(compare(sqlist,q,i)) { cout<<i<<endl; } } return 0 ;}
2、调试一,输入n名学生的信息,设n=3
3、输出学生的信息
4、调试三,插入