1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python列表输出学生姓名学号链表_c语言!!!程序设计:建立一个学生信息链表 包括学

python列表输出学生姓名学号链表_c语言!!!程序设计:建立一个学生信息链表 包括学

时间:2019-07-22 04:23:24

相关推荐

python列表输出学生姓名学号链表_c语言!!!程序设计:建立一个学生信息链表 包括学

展开全部

代码如下:

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

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

#include

#include

#include

#include

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<

<

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

cout<entry.num<entry.name<entry.sex<

<entry.score1<entry.score2<entry.score3<

}

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

cout<

cout<

cout<

cout<

cout<

cout<

cout<

//cout<

cout<

//cout<

//PrintLink(link);

int n;

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

if(n==0) break;

else if(n==1){

cout<

SortLinkID(link);

PrintLink(link);

}

else if(n==2){

cout<

SortLinkNameLength(link);

PrintLink(link);

}

else if(n==3){

cout<

SortLinkName(link);

PrintLink(link);

}

else if(n==4){

cout<

SortLinkChinese(link);

PrintLink(link);

}

else if(n==5){

cout<

SortLinkMath(link);

PrintLink(link);

}

else if(n==6){

cout<

SortLinkEnglish(link);

PrintLink(link);

}

else cout<

}

return 0;

}

拓展资料:

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。