题目要求:
定义一个包含学生信息(学号,姓名,性别,手机号,qq号)的线性表(采用链表实现),使其具有如下功能:
(1)指定学生个数为5个,并初始化学生信息,学生信息如下表所示:
学号 | 姓名 | 性别 | 手机号 | QQ号 |
---|---|---|---|---|
2013002 | 李思 | 女 | 13910121978 | 8796532 |
2013003 | 陈琪 | 女 | 13789450012 | 3789123 |
2013004 | 王强 | 男 | 13634567856 | 6543783 |
2013005 | 赵括 | 男 | 13534408976 | 5679 |
2013006 | 刘刚 | 男 | 13386543211 | 98315 |
(2) 逐个显示学生表中所有学生的相关信息;
(3) 根据姓名进行查找,返回此学生的学号和手机号;
(4) 根据指定的位置可返回相应的学生信息(学号,姓名);
(5) 给定一个学生信息,插入到表中指定的位置;
(6) 删除指定位置的学生记录;
(7) 统计表中学生个数。
以上每一步都做成函数,并给出相关代码。
实验可以参考的结构体类型:
struct student{
char number[14];//学号
char name[20]; //姓名
int sex; //性别
char tel[14]; //联系电话
char qq[12]; //QQ号
struct student *next;
};
参考代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//全局变量
int linkList_length;
struct node{
char number[14];//学号
char name[15]; //姓名
char sex[4]; //性别
char tel[14]; //联系电话
char qq[12]; //QQ号
struct node *next;
};
//1.指定学生个数为5个,并初始化学生信息,学生信息如下表所示:
struct node* init(struct node* linkList){
linkList = NULL;
struct node *p;
struct node arr[5]={
{"2013002","李思","女","13910121978","8796532",NULL},
{"2013003","陈琪","女","13789450012","3789123",NULL},
{"2013004","王强","男","13634567856","6543783",NULL},
{"2013005","赵括","男","13534408976","5679",NULL},
{"2013006","刘刚","男","13386543211","98315",NULL}
};
int i;
for(i=0;i<5;i++){
p=(struct node*)malloc(sizeof(struct node));
//printf("地址为:%p\n",p);
strcpy(p->number,arr[i].number);
strcpy(p->name,arr[i].name);
strcpy(p->sex,arr[i].sex);
strcpy(p->tel,arr[i].tel);
strcpy(p->qq,arr[i].qq);
p->next=linkList;
linkList=p;
}
return linkList;
}
//2.逐个显示学生表中所有学生的相关信息;
void Print(struct node* linkList){
linkList_length=0;
struct node* result=linkList;
printf("学号\t姓名\t性别\t电话\t\tQQ\n");
printf("-----------------------------------------------\n");
for(result;result != NULL;result = result ->next){
printf("%s\t",result->number);
printf("%s\t",result->name);
printf("%s\t",result->sex);
printf("%s\t",result->tel);
printf("%s\t",result->qq);
printf("\n");
linkList_length++;
}
printf("-----------------------------------------------\n");
}
//3.根据姓名进行查找,返回此学生的学号和手机号;
void select(struct node* linkList,char *name){
struct node* result=linkList;
int sign=0;
printf("姓名\t学号\t电话\n");
printf("-------------------------------\n");
for(result;result != NULL;result = result ->next){
if(strcmp(result->name,name)==0){
printf("%s\t%s\t%s\n",result->name,result->number,result->tel);
sign=1;
}
}
if(sign==0){
printf("抱歉,暂未找到该学生信息!");
}
printf("-------------------------------\n");
}
//4.根据指定的位置可返回相应的学生信息(学号,姓名)
void local(struct node* linkList,int local){
struct node* result=linkList;
int i=1;
if(local>linkList_length || local<=0){
printf("--------------------------------------------\n");
printf("当前共存储了%d位学生信息,请在有效范围内查询!\n|",linkList_length);
printf("--------------------------------------------\n");
}else{
printf("学号\t姓名\n");
printf("-------------------------------\n");
for(result;result != NULL;result = result ->next,i++){
if(local==i){
printf("%s\t%s\n",result->number,result->name);
}
}
printf("-------------------------------\n");
}
}
//(5) 给定一个学生信息,插入到表中指定的位置;
struct node* insert(struct node* linkList,struct node arr[1],int local){
struct node *head;
head =(struct node*)malloc(sizeof(struct node));
head->next=linkList;
struct node *tail,*temp,*add;
tail = head;
int i=1;
for(head;head != NULL;head=head->next,i++){
if(local==i){
temp=head->next;
add=(struct node*)malloc(sizeof(struct node));
strcpy(add->number,arr[0].number);
strcpy(add->name,arr[0].name);
strcpy(add->sex,arr[0].sex);
strcpy(add->tel,arr[0].tel);
strcpy(add->qq,arr[0].qq);
head->next=add;
add->next=temp;
linkList_length+=1;
}
}
return tail->next;
}
//(6) 删除指定位置的学生记录;
struct node* delete(struct node* Linklist,int local){
struct node* head;
head = (struct node*)malloc(sizeof(struct node));
head->next=Linklist;
struct node* tail,*temp;
tail = head;
if(local>linkList_length || local<=0){
printf("--------------------------------------------\n");
printf("当前共存储了%d位学生信息,请在有效范围内删除!\n|",linkList_length);
printf("--------------------------------------------\n\n");
}else{
int i=1;
for(head;head != NULL;head = head->next,i++){
if(local==i){
temp=head->next;
head->next=temp->next;
free(temp);
if(linkList_length>=1){
linkList_length-=1;
}else{
printf("链表长度为0,删除失败!");
}
}
}
}
return tail->next;
}
//(7) 统计表中学生个数。
void count(){
printf("表中共%d个学生。",linkList_length);
}
//main函数
int main(){
struct node *start,*head;
//初始化
head=init(start);
//打印
Print(head);
//根据姓名查找
printf("\n根据姓名查找功能:王强\n");
select(head,"王强");
//根据位置查找
printf("\n根据位置查找功能:查找第2位同学信息\n");
local(head,2);
//(5) 给定一个学生信息,插入到表中指定的位置;
//要添加的学生信息
printf("\n添加学生:学号2013008,姓名:张三,性别:男,手机:123456789,QQ:987789,插入第1位!\n");
struct node arr[1]={{"2013008","张三","男","123456789","987789",NULL}};
//执行
head=insert(head,arr,1);
//打印
Print(head);
//(6) 删除指定位置的学生记录;
printf("\n\删除指定位置的学生功能:删除第6位同学信息!\n");
head=delete(head,6);
Print(head);
//(7) 统计表中学生个数。
printf("\n\统计表中学生个数:\n");
count();
printf("\n");
system("pause");
return 0;
}
运行结果:
1.逐个显示学生表中所有学生的相关信息。
2.根据姓名进行查找,返回此学生的学号和手机号;
3.根据指定的位置可返回相应的学生信息(学号,姓名)。
4.给定一个学生信息,插入到表中指定的位置。
5.删除指定位置的学生记录。
6.统计表中学生个数。