c语言!!!程序设计:建立一个学生信息链表,包括学号,姓名,成绩.(实现添加,删除,查询,排序,平均)

C语言~~~~~~~~不要C++
建立一个学生信息链表,包括学号、姓名、成绩。
1.可任意添加学生信息
2.可根据学号或者姓名查询该学生的所有信息并输出
3.可根据学号或姓名删除学生信息
4.可根据学号或成绩进行排序并输出
5.统计平均成绩并输出

代码如下:

/*用c语言链表编写一个学生信息系统程序,要求输出学生的学号,姓名,性别,学号,姓名,成绩(实现添加,删除,查询,排序,平均)*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <stdlib.h>

using namespace std;

const int n=5;

/*

* nodeEntry : 节点数据类型

* nodeADT   : 节点结构

* linkADT   : 链表结构

*/

typedef struct Student

{

int num;

char name[30];

char sex;

float score1;//语文

float score2;//数学

float score3;//英语

//struct Student *next;

}Student;

typedef struct linkCDT {

nodeADT head;

}*linkADT;

/*

* InitLink   : 初始化链表

* CreateNode : 创建节点

* AppendLink : 添加数据

*/

nodeADT CreateNode(Student entry) {

nodeADT p=(nodeADT)malloc(sizeof*p);

p->entry=entry,p->next=0;

return p;

}

/*

SortLink : 排序链表

//按学号排序

void SortLinkID(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (pHead->entry.num>=p->entry.num)

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

//按英语成绩排序

void SortLinkEnglish(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (pHead->entry.score3>=p->entry.score3)

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

//按姓名的字典序进行排序

void SortLinkName(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (pHead->entry.name[0]>=p->entry.name[0])

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

//按姓名的长度进行排序

void SortLinkNameLength(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (strlen(pHead->entry.name)>=strlen(p->entry.name))

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

循环链表是与单链表一样

是一种链式的存储结构,所不同的是,循环链表的最后一个结点的指针是指向该循环链表的第一个结点或者表头结点,从而构成一个环形的链。

循环链表的运算与单链表的运算基本一致。所不同的有以下几点:

1、在建立一个循环链表时,必须使其最后一个结点的指针指向表头结点,而不是象单链表那样置为NULL。此种情况还使用于在最后一个结点后插入一个新的结点。

2、在判断是否到表尾时,是判断该结点链域的值是否是表头结点,当链域值等于表头指针时,说明已到表尾。而非象单链表那样判断链域值是否为NULL。

以上内容参考:百度百科-链表

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2019-08-06

代码如下:

/*用c语言链表编写一个学生信息系统程序,要求输出学生的学号,姓名,性别,

学号,姓名,成绩.(实现添加,删除,查询,排序,平均)*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <stdlib.h>

using namespace std;

const int n=5;

/*

* nodeEntry : 节点数据类型

* nodeADT   : 节点结构

* linkADT   : 链表结构

*/

typedef struct Student

{

int num;

char name[30];

char sex;

float score1;//语文

float score2;//数学

float score3;//英语

//struct Student *next;

}Student;

typedef struct nodeCDT {

Student entry;

struct nodeCDT *next;

}*nodeADT;

typedef struct linkCDT {

nodeADT head;

}*linkADT;

/*

* InitLink   : 初始化链表

* CreateNode : 创建节点

* AppendLink : 添加数据

*/

void InitLink(linkADT *link) {

*link=(linkADT)malloc(sizeof*(*link));

(*link)->head=0;

}

nodeADT CreateNode(Student entry) {

nodeADT p=(nodeADT)malloc(sizeof*p);

p->entry=entry,p->next=0;

return p;

}

void AppendLink(linkADT *link,Student entry) {

nodeADT newNode=CreateNode(entry),p;

if (!*link) InitLink(link);

if (!(*link)->head) (*link)->head=newNode;

else {

for (p=(*link)->head;p->next;p=p->next);

p->next=newNode;

}

}

/*

* SortLink : 排序链表

* -------------------

* 通过移动每个节点的指针来完成排序

*/

//按学号排序

void SortLinkID(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (pHead->entry.num>=p->entry.num)

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

//按语文成绩排序

void SortLinkChinese(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (pHead->entry.score1>=p->entry.score1)

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

//按数学成绩排序

void SortLinkMath(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (pHead->entry.score2>=p->entry.score2)

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

//按英语成绩排序

void SortLinkEnglish(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (pHead->entry.score3>=p->entry.score3)

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

//按姓名的字典序进行排序

void SortLinkName(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (pHead->entry.name[0]>=p->entry.name[0])

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

//按姓名的长度进行排序

void SortLinkNameLength(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next) 

if (strlen(pHead->entry.name)>=strlen(p->entry.name))

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

// PrintLink : 打印链表

void PrintLink(linkADT link) {

nodeADT p=link->head;

cout<<"学号"<<"  "<<"姓名"<<"\t"<<"性别"<<"\t"

<<"语文"<<"\t"<<"数学"<<"\t"<<"英语"<<endl;

for (;p;p!=NULL,p=p->next){

cout<<p->entry.num<<"  "<<p->entry.name<<"\t"<<p->entry.sex<<"\t"

<<p->entry.score1<<"\t"<<p->entry.score2<<"\t"<<p->entry.score3<<endl;

}

printf("\n");

}

/* 测试 */

int main() {

linkADT link=0;

Student stu[n]={{1002,"Gao Min",'M',80,80,80},{1008,"Wen LR",'M',79,80,70},

{1000,"Wan Huang",'F',72,94,87},{1006,"Zhang Xin",'F',90,90,90},

{1001,"Liu qing",'M',89,90,92}};

int i;

for (i=0;i<n;AppendLink(&link,*(stu+i++)));

cout<<"请选择:"<<endl;

cout<<"1、按照学号升序排序"<<endl;

cout<<"2、按照姓名字符长度升序排序"<<endl;

cout<<"3、按照姓名字典序升序排序"<<endl;

cout<<"4、按照语文成绩升序排序"<<endl;

cout<<"5、按照数学成绩升序排序"<<endl;

cout<<"6、按照英语升序排序"<<endl;

//cout<<"7、未排序数据"<<endl;

cout<<"0、退出"<<endl;

//cout<<"未排序数据:"<<endl;

//PrintLink(link);

int n;

while(~scanf("%d",&n)){

if(n==0) break;

else if(n==1){

cout<<"按照学号升序排序:"<<endl;

SortLinkID(link);

PrintLink(link);

}

else if(n==2){

cout<<"按照姓名字符长度升序排序:"<<endl;

SortLinkNameLength(link);

PrintLink(link);

}

else if(n==3){

cout<<"按照姓名字典序升序排序:"<<endl;

SortLinkName(link);

PrintLink(link);

}

else if(n==4){

cout<<"按照语文成绩升序排序:"<<endl;

SortLinkChinese(link);

PrintLink(link);

}

else if(n==5){

cout<<"按照数学成绩升序排序:"<<endl;

SortLinkMath(link);

PrintLink(link);

}

else if(n==6){

cout<<"按照英语升序排序:"<<endl;

SortLinkEnglish(link);

PrintLink(link);

}

else cout<<"输入错误,请重新输入!"<<endl;

}

return 0;

}

拓展资料:

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,操作复杂。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。

使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。链表最明显的好处就是,常规数组排列关联项目的方式可能不同于这些数据项目在记忆体或磁盘上顺序,数据的存取往往要在不同的排列顺序中转换。链表允许插入和移除表上任意位置上的节点,但是不允许随机存取。链表有很多种不同的类型:单向链表,双向链表以及循环链表。链表可以在多种编程语言中实现。像Lisp和Scheme这样的语言的内建数据类型中就包含了链表的存取和操作。程序语言或面向对象语言,如C,C++和Java依靠易变工具来生成链表。

本回答被网友采纳
第2个回答  推荐于2017-10-09
1、更多交流可参考我空间主页有关文章。
2、#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
/*定义结构体*/
struct student
{
int num;
float score;
struct student *next;
};

/*创建一个只有头结点的空链表*/
struct student *create_head()
{
struct student *head;
head=(struct student*)malloc(sizeof (struct student) );
if(head==NULL) //小心别漏这个
{
printf("申请头结点失败!\n");
return NULL;
}
head->next=NULL;
return head;
}
/*将s指向的结点插入链表,使链表保持升序,并返回头结点*/
struct student *insert(struct student *head,struct student *s)
{
struct student *p=head;
while(p->next!=NULL&&s->score>p->next->score)//特别注意&&左右不能写反,若s最大,最后p->next=NULL,p->next->score运行出错
p=p->next;
if(p->next==NULL) //s->score最大的情况 //其实两种情况可以并在一块写
{
p->next=s; //连接结点
s->next=NULL; //p->next就等于NULL
}
else
{
p->next=s; //连接结点
s->next=p->next;
}
return head ;
}
/*查找符合条件的结点,并返回指向该结点的指针*/
struct student *search(struct student *head)
{
struct student *p=head->next;
int num;
printf("请输入要查找学生的学号:\n");
scanf("%d",&num);
while(p!=NULL&&p->num!=num) //特别注意两条件不能写反,若写反最后p指向NULL时p->num找不到 运行出错
p=p->next;
if(p==NULL) //特别注意两个if不能调换,若调换最后p指向NULL时p->num运行出错
{
printf("找不到符合条件的结点!!!");
return NULL; //查找不到返回空指针
}
if(p->num==num)
{
printf("找到符合条件的结点\n该结点为%d\t%f",p->num,p->score);
return p; //返回查找到的指针
}
}
/*输出链表各结点的值,也称对链表的遍历*/
void print(struct student *head)
{
struct student *p;
printf(" 链表如下: \n");
p=head->next;
while(p!=NULL)
{
printf("%d\t%.1f\n",p->num,p->score);
p=p->next;
}
}

/*释放链表*/
void free_list(struct student *head)
{
struct student *p=head ;
printf("释放链表:\n");
while(p!=NULL)
{
head=head->next;
free(p);
p=head;
}
printf("释放链表成功!\n");
}
/*删除链表中值为num的结点,并返回链表的首指针*/
struct student *delete_note(struct student *head,int num_x)
{
struct student *p1=head->next , *p2=head ;
while(p1!=NULL&&p1->num!=num_x) //特别注意&&左右条件不能调换,若调换如果p1指向NULL时p1->num运行出错
{
p2=p1;
p1=p1->next;
}
if(p1==NULL) //特别注意两个if不能调换,若调换如果p1指向NULL时,p1->num运行出错
printf("找不到符合删除要求的结点!!!\n");
if(p1->num==num_x)
{
p2->next=p1->next;
free(p1);
printf("结点删除成功!\n");
}
return head;
}

/*完整的有头结点链表操作程序*/
void main()
{
struct student *p , *head ;
char c;
int num ;
float score ;
printf("有头结点链表操作程序:\n");
head=create_head();
while(1)
{
printf("I:插入结点(自动升序) P:输出链表 S:查找结点 D:删除结点 E:释放链表并退出程序! ");
c=getch();
switch(c)
{
case'I':
printf("请分别输入要插入学生的学号和分数:\n");
scanf("%d%f",&num,&score);
p=(struct student*)malloc( sizeof(struct student) );
if(p==NULL)
{
printf("申请该结点失败!!!\n");
exit (0) ;
}
p->num=num; p->score=score; //给p赋值
insert(head,p);
printf("插入成功!\n");
break;
case'P':
print(head);
break;
case'S':
search(head);
break;
case'D':
printf("请输入要删除的学生的学号:\n");
scanf("%d",&num);
delete_note(head,num);
break;
case'E':
free_list(head);
exit (0);
}
}

}本回答被提问者和网友采纳
第3个回答  推荐于2019-08-27

#include<iostream>

using namespace std;

struct stu{

char name[20];

int num;

int age;

char sex;

int grade;

struct stu *next;

};

struct stu *mythis,*mynew;

void newrecord(struct stu *head)

{

mythis=head->next;

while(mythis!=NULL)

mythis=mythis->next;

mynew=(struct stu *)malloc(sizeof(struct stu));             

cin>>mynew->name>>mynew->num>>mynew->age>>mynew->sex>>mynew->grade;

mynew->next=NULL;

if(mythis==NULL)

{

mythis=(struct stu *)malloc(sizeof(struct stu));  

mythis=mynew;

}

}

void listall(stu *head)

{

mythis=head->next;

while(mythis!=NULL)

{

cout<<mythis->name<<mythis->num<<mythis->age<<mythis->sex<<mythis->grade;

mythis=mythis->next;

}

}

int main()

{

char decide;

struct stu *head;

head=(struct stu *)malloc(sizeof(struct stu));

head->next=NULL;

while(1)

{

cout<<"Please input decide:"<<endl;

cin>>decide;

if(decide=='n')

newrecord(head);

else

if(decide=='1')

listall(head);

else

return 0;

}

}



拓展资料

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

二十世纪八十年代,为了避免各开发厂商用的C语言语法产生差异,由美国国家标准局为C语言制定了一套完整的美国国家标准语法,称为ANSI C,作为C语言最初的标准。目前2011年12月8日,国际标准化组织(ISO)和国际电工委员会(IEC)发布的C11标准是C语言的第三个官方标准,也是C语言的最新标准,该标准更好的支持了汉字函数名和汉字标识符,一定程度上实现了汉字编程。



本回答被网友采纳
第4个回答  推荐于2019-10-21

链表制作教程

输入样例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0

输出样例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85


ANSWER


void input()
{
struct stud_node *q;
q=(struct stud_node *)malloc(sizeof(struct stud_node));
scanf("%d", &q->num);
while(q->num != 0)
{
scanf("%s %d", q->name, &q->score);
if(head == NULL)
{
head = q;
head->next = NULL;
}
if(tail != NULL)
{
tail->next = q;
}
tail = q;
tail->next = NULL;
q=(struct stud_node *)malloc(sizeof(struct stud_node));
scanf("%d", &q->num);
}


}

代码运行

本回答被网友采纳
相似回答