搜档网
当前位置:搜档网 › DS:线性表的顺序存储结构实现学生记录表

DS:线性表的顺序存储结构实现学生记录表

DS:顺序表
利用线性表的顺序存储结构实现学生记录表

===================================================================================
===========================Node.h==================================================

#define ListSize 20

typedef struct record {
char number[6];
char name[10];
int age;
float score;
} DataType;



typedef struct {
DataType records[ListSize];
int length;
}SeqList;
===================================================================================
===================================================================================






===================================================================================
===========================SeqList_Basic.h=========================================

#include
#include
#include "Node.h"


void InitList( SeqList *L)
{

L->length = 0;

}

DataType WriteInfo () //向一个记录节点中写入信息
{
int j;
DataType temp;

printf("\n\n\t\t\t Please input a new student's information:\n");

for( j=1; j<=4; j++) //给第 i个 学生 输入信息
{
if(j == 1){

printf("input Number (5 figures): ");
scanf("%s", temp.number );
}
else if(j ==2){
printf("input Name: ");
scanf("%s", https://www.sodocs.net/doc/1a7595526.html, );
}
else if(j==3){
printf("input Age: ");
scanf("%d", &temp.age );
}
else{
printf("input Score: ");
scanf("%f", &temp.score );
}
}
return temp;
}


void Store( SeqList *L)
{
int n, i;
void InsertList( SeqList *L, int i, DataType x ); //插入函数的声明

printf(" Please input the number of records: ");
scanf("%d", &n);

for( i=0; i{
int j;
DataType temp;


printf("\n\n\t\t\t PLEASE INPUT the INFORMATION of STUDENT%d \n", i+1);

for( j=1; j<=4; j++) //给第 i个 学生 输入信息
{
if(j == 1){

printf("input Number (5 figures): ");
scanf("%s", temp.number );
}
else if(j ==2){
printf("input Name: ");
scanf("%s", https://www.sodocs.net/doc/1a7595526.html, );
}
else if(j==3){
printf("input Age: ");
scanf("%d", &temp.age );
}
else{
printf("input Score: ");
scanf("%f", &temp.score );
}
}

InsertList( L, i+1, temp ); //向记录表中插入第i个学生的信息;经过n次插入后,
//n个学生的信息就存储在记录表里面了。
}

}


void Show( SeqList *L)
{
int i;

printf("\t\t\t\t学号:\t姓名:\t年龄:\t学分:\n");
for( i=0 ; ilength ; i++)
printf("\t\t\t\t%s \t%s \t%3d \t%6.2f \n", L->records [i].number ,L->records [i].name ,L->records [i].age , L->records [i].score );

}


void InsertList( SeqList *L, int i, DataType x )
{
int j;

if( i < 1 || i > L->length +1 ) //插入位置的检查
{
printf(" position error! \n");
return;
}

if( L->length >= ListSize ) //剩余存储空

间检查
{
printf(" overflow! \n");
return;
}

for( j= L->length - 1; j >= i-1; j--) //后移元素,留出插入位置
L->records[j+1] = L->records[j];

L->records[i-1] = x ; //插入元素
L->length++ ; //修改表长
}


int ListLength( SeqList *L)
{
return L->length ;
}



//void LocateList( SeqList *L, char )
//{


DataType GetNode( SeqList *L , int i ) // 获取第i 个学生的信息
{
if(i<=0 || i> L->length )
{
printf(" Position Error\n" );
return ; // 这个位置按理说应该返回一个错误信息的,但是不知道这个错误信息该怎么写,仅用Error来代替那是伪代码做的事情。
}

return L->records [i-1];
}


DataType LocateNode( SeqList *L, char * name)
{
int i;

for(i=0; i< L->length; i++)
if( strcmp( name, L->records[i].name ) == 0 )
return L->records[i];
}


DataType DeleteList( SeqList *L, int i)
{
int j;
DataType temp;

if( i<1 || i>L->length )
{
printf("Postion Error!\n");
return;
}

temp= L->records [i-1];

for(j=i; j<=L->length ; j++)
L->records [j-1] = L->records [j];

L->length --;
return temp;
}

===================================================================================
===================================================================================




====================================================================================
=================================Main.c=============================================

#include
#include "SeqList_Basic.h"


void main(){

int position;
char *name=(char *)malloc(6*sizeof(char));
DataType temp;
SeqList student; //生成一个名为"student"的记录表

InitList(&student); //对新生成的 记录表 置空
Store(&student); //向 记录表 中存储数据
Show(&student); //刷新 记录表 中的信息

printf("\n\t\t\t\tThe length of this record is %d\n", ListLength(&student)); //计算学生记录个数


printf("Please input a student's position to get his information:");
scanf("%d",&position);
temp = GetNode(&student, position); //获取第i个学生的信息
printf("\t\t\t\t%s \t%s \t%3d \t%6.2f \n", temp.number , https://www.sodocs.net/doc/1a7595526.html, , temp.age , temp.score );


printf("Please input a student's name to get his information:");
scanf("%s", name);
temp = LocateNode(&student, name); // 根据姓名获取学生信息
printf("\t\t\t\t%s \t%s \t%3d \t%6.2f \n", temp.number , https://www.sodocs.net/doc/1a7595526.html, , temp.age , temp.score );

printf("Please input the student's position to insert a new record:");
scanf("%d", &position);
//printf("and then please input the new information:"); //这句可以看作是多余的,因为WriteInfo函数中已有该提示语句。
temp= WriteInfo();
InsertList(&student, position, temp); //向第i个学生之前插入一个信息为temp的新学生,并把记录表长加1
Show(&student); //显示插入后的新记录表

printf("Please inp

ut the student's position to delete his record:");
scanf("%d", &position);
temp=DeleteList(&student, position); //删除记录表中的第i个学生的信息,记录表长度减1
Show(&student);


}

=====================================================================================
=====================================================================================

相关主题