数据结构练习(线性表)

题目要求:
定义一个包含学生信息(学号,姓名,性别,手机号,qq号)的线性表(采用链表实现),使其具有如下功能:

(1)指定学生个数为5个,并初始化学生信息,学生信息如下表所示:

学号姓名性别手机号QQ号
2013002李思139101219788796532
2013003陈琪137894500123789123
2013004王强136345678566543783
2013005赵括135344089765679
2013006刘刚1338654321198315

(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.统计表中学生个数。
在这里插入图片描述

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值