展开全部
//stud_info.c
/**
**定义一个包含学生信息(学号,姓名,成绩)的链表,使其具有如下功能:
(1) 根据指定学生个数,逐个输入学生信息;
(2) 逐个显示学生表中所有学生的相关信息;
(3) 根据姓名进行查找,返回此学生的学号和成绩;
(4) 根据指定的位置可返回相应的学生信息(学号,姓名,成绩);
(5) 给定一个学生信息,插入到表中指定的位置;
(6) 删除指定位置的学生记录;
(7) 统计表中学生个数。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 36
#pragma pack(1)
typedef struct stu_info
{
char stuNo[MAX_LEN];
char stuName[MAX_LEN];
float score;
}STU;
typedef struct LNode
{
STU data;
struct LNode *next;
} StuNode,*StuList;
#pragma pack()
//创建带头结点的学生链表
StuList create_stuList()
{
StuList head = (StuNode*)malloc(sizeof(StuNode));
if(!head)
return NULL;
head->next = NULL;
return head;
}
//输入一个学生信息并插入到学生链表当中(头插法)
void input_stuInfo(StuList L)
{
StuList s;
s = (StuNode*)malloc(sizeof(StuNode));
printf("Please input student number: ");
scanf("%s", s->data.stuNo);
printf("Please input student name: ");
scanf("%s", s->data.stuName);
printf("Please input student score: ");
scanf("%f", &s->data.score);
s->next = L->next;
L->next = s;
}
//2. 逐个显示学生表中所有学生的相关信息
void show_studInfo(StuList L)
{
StuList s;
s = L->next; //s初始指向链表的第1个数据结点
if(s == NULL)
{
printf("The Student Linklist has no data!\n");
return;
}
printf("\n************************ Student Info ************************\n");
while(s)
{
printf("StuNo: %s Name: %s score: %.2f\n", s->data.stuNo, s->data.stuName, s->data.score);
s = s->next;
}
}
//3. 根据姓名进行查找,返回此学生的学号和成绩(这里就不考虑重名的问题了)
void find_stud_by_name(StuList L, const char *name)
{
StuList s;
s = L->next;
if(s == NULL)
{
printf("The Student Linklist has no data!\n");
return;
}
while(s)
{
if(!strcmp(s->data.stuName, name))
{
printf("Found it! StuNo: %s score: %.2f\n\n", s->data.stuNo, s->data.score);
return;
}
s = s->next;
}
printf("I`m sorry, there is no %s student!\n\n", name);
}
//7. 统计表中学生个数
int cal_stud_count(StuList L)
{
int count=0;
StuList s;
s = L->next;
if (s == NULL)
return 0;
while(s)
{
count++;
s = s->next;
}
return count;
}
//4. 根据指定的位置可返回相应的学生信息(学号,姓名,成绩)
void find_stud_by_location(StuList L, int loc)
{
StuList s;
s = L->next;
if(s == NULL)
{
printf("The Student Linklist has no data!\n\n");
return;
}
int i=0;
int count = cal_stud_count(L);
if(loc > count || loc <= 0)
{
printf("This location is out of range!\n\n");
return;
}
for(i=1; i<=count; ++i)
{
if(loc == i)
{
printf("StuNo: %s Name: %s score: %.2f\n\n", s->data.stuNo, s->data.stuName, s->data.score);
return;
}
s = s->next;
}
}
//5. 给定一个学生信息,插入到表中指定的位置
void insert_StudInfo(StuList L, int loc)
{
int i;
StuList p,s;
p = L->next; //p初始指向链表的第1个data结点
s = (StuNode*)malloc(sizeof(StuNode));
printf("Please input student number: ");
scanf("%s", s->data.stuNo);
printf("Please input student name: ");
scanf("%s", s->data.stuName);
printf("Please input student score: ");
scanf("%f", &s->data.score);
int length = cal_stud_count(L);
if(length == 0) //当链表为空时插入到头结点的后面
{
L->next = s;
s->next = NULL;
}
else if(loc <= length && loc > 0)
{
for(i=1; i<=length; ++i)
{
if(loc == 1) //在第1个位置插入data结点
{
L->next = s;
s->next = p;
break;
}
else if(i == loc-1)
{
s->next = p->next;
p->next = s;
break;
}
p = p->next;
}
}
else
{
printf("This location is out of range, can`t insert data!\n\n");
free(s);
return;
}
printf("Insert student info in %d location successfully.\n\n", loc);
}
//6. 删除指定位置的学生记录
void delete_studInfo(StuList L, int loc)
{
int i;
StuList p,s;
p = L->next;
int length = cal_stud_count(L);
if(length == 0)
{
printf("The stud_info list is empty, can`t delete data!\n\n");
return;
}
else if(loc <= length && loc > 0)
{
for(i=1; i<= length; ++i)
{
if(loc == 1)
{
s = p;
L->next = p->next;
free(s);
break;
}
else if(i == loc-1)
{
s = p->next;
p->next = s->next;
free(s);
break;
}
p = p->next;
}
}
else
{
printf("This location is out of range, can`t delete data!\n\n");
return;
}
printf("Delete student info in %d location successfully.\n\n", loc);
}
//销毁链表
void destroy_stuList(StuList L)
{
StuList p,s;
p=s=L->next;
while(p)
{
p = p->next;
free(s);
s = p;
}
free(L);
L = NULL;
}
//清空链表
void clear_stuList(StuList L)
{
StuList p,s;
p=s=L->next;
if(L == NULL)
return;
while(p)
{
p = p->next;
free(s);
s = p;
}
L->next = NULL;
}
int main(int argc, char const *argv[])
{
int n,i,loc;
char name[MAX_LEN];
StuList L = create_stuList();
if(!L)
{
printf("Create stuList failed!\n");
return -1;
}
//1. 根据指定学生个数,逐个输入学生信息
printf("Please enter the student`s number: ");
scanf("%d", &n);
printf("Please enter %d students info:\n", n);
for (i = 0; i < n; ++i)
{
input_stuInfo(L);
printf("\n");
}
//2. 显示学生链表的信息
show_studInfo(L);
//3. 根据姓名进行查找,返回此学生的学号和成绩
printf("\nPlease input student name: ");
scanf("%s", name);
find_stud_by_name(L, name);
//4. 根据指定的位置输出相应的学生信息
printf("\n请输入你要查找的学生的位置: ");
scanf("%d", &loc);
find_stud_by_location(L, loc);
//5. 给定一个学生信息,插入到表中指定的位置
printf("\n请输入你要插入的学生的位置: ");
scanf("%d", &loc);
insert_StudInfo(L, loc);
show_studInfo(L);
//6. 删除指定位置的学生记录
printf("\n请输入你要删除的学生的位置: ");
scanf("%d", &loc);
delete_studInfo(L, loc);
show_studInfo(L);
//7. 统计表中学生人数
printf("\n学生人数: %d\n", cal_stud_count(L));
destroy_stuList(L);
return 0;
}
//示例运行结果:
Please enter the student`s number: 6
Please enter 6 students info:
Please input student number: 20191001
Please input student name: aaaa
Please input student score: 98
Please input student number: 20191002
Please input student name: bbbb
Please input student score: 89
Please input student number: 20191003
Please input student name: cccc
Please input student score: 82
Please input student number: 20191004
Please input student name: dddd
Please input student score: 77
Please input student number: 20191005
Please input student name: eeee
Please input student score: 68
Please input student number: 20191006
Please input student name: ffff
Please input student score: 58
************************ Student Info ************************
StuNo: 20191006 Name: ffff score: 58.00
StuNo: 20191005 Name: eeee score: 68.00
StuNo: 20191004 Name: dddd score: 77.00
StuNo: 20191003 Name: cccc score: 82.00
StuNo: 20191002 Name: bbbb score: 89.00
StuNo: 20191001 Name: aaaa score: 98.00
Please input student name: cccc
Found it! StuNo: 20191003 score: 82.00
请输入你要查找的学生的位置: 5
StuNo: 20191002 Name: bbbb score: 89.00
请输入你要插入的学生的位置: 3
Please input student number: 20191007
Please input student name: gggg
Please input student score: 100
Insert student info in 3 location successfully.
************************ Student Info ************************
StuNo: 20191006 Name: ffff score: 58.00
StuNo: 20191005 Name: eeee score: 68.00
StuNo: 20191007 Name: gggg score: 100.00
StuNo: 20191004 Name: dddd score: 77.00
StuNo: 20191003 Name: cccc score: 82.00
StuNo: 20191002 Name: bbbb score: 89.00
StuNo: 20191001 Name: aaaa score: 98.00
请输入你要删除的学生的位置: 4
Delete student info in 4 location successfully.
************************ Student Info ************************
StuNo: 20191006 Name: ffff score: 58.00
StuNo: 20191005 Name: eeee score: 68.00
StuNo: 20191007 Name: gggg score: 100.00
StuNo: 20191003 Name: cccc score: 82.00
StuNo: 20191002 Name: bbbb score: 89.00
StuNo: 20191001 Name: aaaa score: 98.00
学生人数: 6
追问
谢谢
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个比较麻烦一点,时间就是金钱啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接看代码和结果吧:
/*
* File: main.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.h"
stu_p head = NULL;
unsigned int index = 0;
int main(void) {
int stu_sum;
stu_p current_node = NULL, previous_node = NULL;
printf("请输入学生总人数: ");
scanf("%d", &stu_sum);
while ( stu_sum-- ) {
current_node = malloc(sizeof(student));
if ( current_node == NULL ) {
perror("Error");
exit(1);
}
printf("请输入第%d个学生的学号: ", stu_sum + 1);
scanf("%ld", ¤t_node->num);
printf("请输入第%d个学生的名字: ", stu_sum + 1);
scanf("%s", current_node->name);
printf("请输入第%d个学生的成绩: ", stu_sum + 1);
scanf("%lf", ¤t_node->score);
printf("-----------------------\n");
link_node( current_node );
}
printf(" 所有的学生信息如下\n");
print_stu(); /* 打印所有学生信息 */
current_node = find_stu_index(2); /* 查找指定位置的学生信息 */
if ( current_node != NULL ) {
printf("查找指定位置的学生信息(指定的位置索引为2)\n");
printf("学号\t姓名\t成绩\n");
printf("%ld\t%s\t%0.1f\n", current_node->num, current_node->name, current_node->score);
}
current_node = malloc(sizeof(student));
current_node->num = 111;
strcpy(current_node->name, "Ekko");
current_node->score = 99.9;
insert_stu_node(current_node, 1); /* 插入指定位置 */
printf("将一个学生信息插入到指定位置(指定的位置为1)\n");
print_stu();
current_node = find_stu_name("Ekko");
if ( current_node != NULL ) {
printf("通过姓名查找学生信息(Ekko)\n学号\t成绩\n");
printf("%ld\t%0.1f\n", current_node->num, current_node->score);
} else {
printf("没有找到该学生信息\n");
}
delete_stu_index(1); /* 删除指定位置的数据 */
printf("删除指定位置的学生信息(指定位置为1)\n");
print_stu();
printf("表中学生个数为: %d\n", head->index + 1);
free_all_node();
return 0;
}
/*
* 连接节点, 头插法
*/
void link_node(stu_p current_node){
current_node->index = index++;
current_node->next = head;
head = current_node;
}
/*
* 打印所有学生信息
*/
void print_stu(void) {
stu_p current_node = head, next_node = NULL;
printf("学号\t姓名\t成绩\n");
while ( current_node ) {
printf("%ld\t%s\t%0.1f\n", current_node->num, current_node->name, current_node->score);
current_node = current_node->next;
}
}
/*
* 通过名字查找学生信息
*/
stu_p find_stu_name(unsigned char const *name) {
stu_p find_node = head;
while ( find_node ) {
if ( strcmp(find_node->name, name) == 0 ) {
return find_node;
}
find_node = find_node->next;
}
return NULL; /* 没有该学生 */
}
/*
* 通过指定位置查找学生信息
*/
stu_p find_stu_index(unsigned char const index_value) {
stu_p current_node = head;
if ( index_value > head->index ) {
printf("要查询的指定位置(%d)没有数据\n", index_value);
return NULL;
}
while ( current_node ) {
if ( current_node->index == index_value ) break;
current_node = current_node->next;
}
return current_node;
}
/*
* 将一个学生的信息插入到指定位置,index为索引号。
*/
void insert_stu_node(stu_p insert_node, unsigned char const index_value) {
stu_p current_node = head->next;
/*
* 节点插在链表的最前面
*/
if ( head->index <= index_value ) {
link_node(insert_node);
return;
}
/*
* 否则需要遍历链表在查询,并且在找到之前查询过的
* 节点的索引号都要加1
*/
head->index++;
while ( current_node ) {
if ( current_node->index == index_value ) {
current_node->index++;
insert_node->next = current_node->next;
current_node->next = insert_node;
insert_node->index = index_value;
index++;
return;
} else {
current_node->index++;
current_node = current_node->next;
}
}
/*
* 如果找不到证明传过来的索引号有问题
*/
printf("找不到需要插入的指定位置: %d。将放弃本次信息录入!\n", index);
}
/*
* 删除指定位置的学生信息,同样index会发生一系列的变化。
*/
void delete_stu_index(unsigned int const index_value) {
stu_p current_node = head->next, previous_node = head;
if ( index_value > head->index ) {
printf("错误提示: 索引号(%d)大于链表长度\n", index_value);
return;
}
if ( index_value == head->index ) {
head = current_node;
free(previous_node);
index--;
return;
}
previous_node->index--;
while ( current_node ) {
if ( current_node->index == index_value ) {
previous_node->next = current_node->next;
free( current_node );
index--;
return;
} else {
current_node->index--;
previous_node = current_node;
current_node = current_node->next;
}
}
}
/*
* 释放所有节点内存
*/
void free_all_node(void) {
stu_p current_node = head, next_node = NULL;
while ( current_node ) {
next_node = current_node->next;
free(current_node);
current_node = next_node;
}
}
/*
* student.h
*/
#ifndef _STUDENT_H_
#define _STUDENT_H_
#define MAX_CHAR_LEN 20
typedef struct Student student;
typedef student *stu_p;
struct Student {
unsigned int index; /* 题中的指定位置 */
unsigned long int num; /* 学号 */
unsigned char name[MAX_CHAR_LEN]; /* 名字 */
double score; /* 成绩 */
student *next; /* 指向下一个节点的指针 */
};
void link_node(unsigned int const n);
void print_stu(void);
void delete_stu_index(unsigned int const index);
stu_p find_stu_name(unsigned char const *name);
stu_p find_stu_index(unsigned char const index);
void insert_stu_node(stu_p insert_node, unsigned char const index);
void free_all_node();
#endif
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个我好像可以做。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询