【C语言】构建新生的学籍卡管理程序。学籍卡内容包括:学号、姓名、性别、专业等内容。
作者:互联网
题目(数据结构测试)
代码:
#include<stdio.h> #include<malloc.h> #include<string.h> #define MAX 50 /*创建一个学生表结构体*/ typedef struct student_node { unsigned id; char name[MAX]; char sex[MAX]; char major[MAX]; struct student_node *next; }Slink; /*创建一个有n个学生带头结点的单链表*/ Slink *CreateSlink(int n) { Slink *head,*p,*s; int i; p = head = (Slink *)malloc(sizeof(Slink)); //创建头结点 printf("请输入%d个学生的学籍信息:\n",n); for(i = 0;i < n;i++) { s = (Slink *)malloc(sizeof(Slink)); printf("**********请输入学号**********\n"); scanf("%u",&s->id); printf("**********请输入姓名**********\n"); getchar(); //消除scanf函数不接收的 回车符(\n) gets(s->name); //name中可以包含空格 printf("**********请输入性别**********\n"); scanf("%s",&s->sex); printf("**********请输入专业**********\n"); scanf("%s",&s->major); p->next = s; //将新结点s插入链表的尾结点 p = s; //修改尾指针指向 p 指向当前的尾结点 s } p->next = NULL; return head; } /*在单链表中按照学号顺序重新调整结点的顺序,形成一个新的链表*/ void BubbleSort(Slink *head) { Slink *firstNode = head->next; Slink *secondNode = head; char temp[1024]; while(firstNode != NULL) { while(firstNode->next != NULL) { if(firstNode->id > firstNode->next->id) { int t = firstNode->id; firstNode->id = firstNode->next->id; firstNode->next->id = t; //交换姓名 strcpy(temp,firstNode->name); strcpy(firstNode->name,firstNode->next->name); strcpy(firstNode->next->name,temp); //交换性别 strcpy(temp,firstNode->sex); strcpy(firstNode->sex,firstNode->next->sex); strcpy(firstNode->next->sex,temp); //交换专业 strcpy(temp,firstNode->major); strcpy(firstNode->major,firstNode->next->major); strcpy(firstNode->next->major,temp); } firstNode = firstNode->next; } firstNode = secondNode->next; secondNode = firstNode; } } /*输出按学号从小到大排序后的结果*/ void display(Slink *head) { Slink *p; p = head->next; /*从第一个实际结点开始*/ if(!p) { printf("\n带头结点的单链表是空的!"); } else { printf("\n新生登记卡按学号排序后的表为:\n"); while(p) { printf("%u %s %s %s\n",p->id,p->name,p->sex,p->major); p = p->next; } } } int main() { Slink *head = CreateSlink(3); BubbleSort(head); display(head); }
运行结果
标签:head,管理程序,Slink,next,strcpy,firstNode,id,C语言,学籍 来源: https://www.cnblogs.com/HGNET/p/13875005.html