两个链表
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student{
int Grade;
char Major[50];
int Class;
int ID;
char Name[20];
struct Student *next;
};
struct GMC{ //Grade Major Class
int Grade;
char Major[50];
int Class;
struct Student *down;
struct GMC *next;
};
char major[3][50] = { "电子信息技术","计算机科学与技术", "数字媒体与技术" };
int sizeGMC = sizeof(struct GMC);
int sizeSTU = sizeof(struct Student);
struct GMC* CreatGMC1(struct GMC *L, int dian, struct GMC *q,int i,int k)
{
struct GMC *p = NULL;
for (p = L; p->next != NULL; p = p->next);
p->next = q;
q->Grade = i;
strcpy(q->Major, major[0]);
q->Class = k;
q->down = NULL;
q->next = NULL;
p = q;
return L;
}
struct GMC* CreatGMC2(struct GMC *L, int ji, struct GMC *q,int i,int k) //创建计科
{
struct GMC *p = NULL;
for (p = L; p->next != NULL; p = p->next);
q->Grade = i;
strcpy(q->Major, major[1]);
q->Class = k;
q->down = NULL;
q->next = NULL;
p->next = q;
p = q;
return L;
}
struct GMC* CreatGMC3(struct GMC *L,int shu,struct GMC *q,int i,int k) //创建数媒
{
struct GMC *p = NULL;
for (p = L; p->next != NULL; p = p->next);
p->next = q;
q->Grade = i;
strcpy(q->Major, major[2]);
q->Class = k;
q->down = NULL;
q->next = NULL;
p = q;
return L;
}
void Mean1()
{
printf("
********************************
");
printf("****** 1-插入学生信息***********
");
printf("****** 2-查询学生信息***********
");
printf("****** 3-删除学生信息***********
");
printf("****** 4-列出学生信息***********
");
printf("****** 5-退出系统 ***********
");
printf(" 请输入您的选择: ");
}
void PrintOneStu(struct Student *k)
{
printf("
年级:%d ", k->Grade);
printf("专业:%s ", k->Major);
printf("班级:%d ", k->Class);
printf("学号:%d ", k->ID);
printf("姓名:%s ", k->Name);
printf("
");
}
struct GMC * Insert(struct GMC *L,struct Student *q) //插入一个学生信息
{
int m;
struct GMC *p = L->next;
struct Student *k1=NULL,*k2=NULL,*k=NULL;
printf("请选择年级:1-大一 2-大二 3-大三 4-大四 ");
scanf("%d",&q->Grade);
printf("请选择专业:1-电子 2-计科 3-数媒 ");
scanf("%d", &m);
strcpy(q->Major, major[m-1]);
printf("请输入班级:");
scanf("%d", &q->Class);
printf("请输入学号 ");
scanf("%d",& q->ID);
printf("请输入姓名 ");
scanf("%s", q->Name);
q->next = NULL;
while ((p->Grade!=q->Grade) || (p->Class!=q->Class)|| (strcmp(p->Major,q->Major)!=0)|| !p) //定位指针到班级
{
p = p->next;
}
if (p->down == NULL) //插入班级第一个人
{
p->down = q;
}
else //将指针定位到班级第一个人
{
k = p->down;
k2 = k;
while ((q->ID > k2->ID) && (k2->next != NULL))
{
k1 = k2;
k2 = k2->next;
}
if (q->ID <= k2->ID)
{
if (k2 == k)
{
p->down = q;
q->next = k2;
}
else
{
k1->next = q;
q->next = k2;
}
}
else
{
k2->next = q;
}
}
return L;
}
void PrintMajor(struct GMC *L)
{
int choice;
struct GMC *p = NULL;
struct Student *k;
printf("选择要列出的专业:1-电子 2-计科 3-数媒 ");
scanf("%d", &choice);
for (p = L->next; p != NULL; p = p->next)
{
if (strcmp(p->Major,major[choice-1])==0)
{
for (k = p->down; k != NULL; k = k->next)
{
PrintOneStu(k);
}
}
}
}
void PrintClass(struct GMC *L)
{
int choice1,choice2,choice3;
struct GMC *p = NULL;
struct Student *k;
printf("需要列出的年级:1-大一 2-大二 3-大三 4-大四");
scanf("%d", &choice1);
printf("选择要列出的专业:1-电子 2-计科 3-数媒 ");
scanf("%d", &choice2);
printf("选择要列出的班级:");
scanf("%d", &choice3);
for (p = L->next; p != NULL; p = p->next)
{
if ((p->Grade == choice1) && (strcmp(p->Major,major[choice2])) && (p->Class==choice3))
{
for (k = p->down; k != NULL; k = k->next)
PrintOneStu(k);
}
}
}
void PrintAll(struct GMC *L) //打印出所有的学生
{
struct GMC *p=L->next;
struct Student *k;
while (p->next!=NULL)
{
for (k = p->down; k != NULL; k = k->next)
{
PrintOneStu(k);
}
p = p->next;
}
}
void Print(struct GMC *L)
{
int choice;
printf("请选择列出学生信息方式:1-列出全部 2-分类列出 ");
scanf("%d", &choice);
if (choice == 1)
{
PrintAll(L);
}
else
{
printf("请选择: 1-分专业列出 2-分班级列出");
scanf("%d", &choice);
if (choice == 1)
PrintMajor(L);
else if (choice == 2)
PrintClass(L);
else
printf("选择错误
");
}
}
void SearchID(struct GMC *L,int id) //按学号查询
{
struct GMC *p;
struct Student *k;
for (p = L->next; p != NULL; p = p->next)
{
for (k = p->down; k != NULL; k = k->next)
{
if (k->ID == id)
{
PrintOneStu(k);
return ;
}
}
}
}
void SearchName(struct GMC *L,char name[]) //按名字查询
{
struct GMC *p;
struct Student *k;
for (p = L->next; p != NULL; p = p->next)
{
for (k = p->down; k != NULL; k = k->next)
{
if (strcmp(k->Name,name)==0)
{
PrintOneStu(k);
return;
}
}
}
}
void Search(struct GMC *L)
{
int choice,id;
char name[20];
printf("请选择查询方法:1-按学号查询 2-按姓名查询 ");
scanf("%d", &choice);
if (choice == 1)
{
printf("请输入您的学号:");
scanf("%d", &id);
SearchID(L,id);
}
else
{
printf("请输入需要查询的名字:");
scanf("%s", name);
SearchName(L,name);
}
}
void Delete(struct GMC *L,int DelID) //删除一个学生信息
{
struct GMC *p;
struct Student *k=NULL, *k2=NULL;
for (p = L->next; p != NULL; p = p->next)
{
for (k = p->down; k != NULL; k = k->next)
{
if (k->ID == DelID)
{
if (k == p->down)
{
p->down = k->next;
}
else
k2->next = k->next;
free(k);
return;
}
k2 = k;
}
}
}
int main()
{
int i = 0,k = 0;
int dian=0,ji=0,shu=0,choice = 0;
struct GMC *L = NULL,*q=NULL;
struct Student *q1 = NULL;
int DelID;
L = (struct GMC*)malloc(sizeGMC);
L->next = NULL;
printf("请输入电子班级个数:");
scanf("%d", &dian);
printf("请输入计科班级个数:");
scanf("%d", &ji);
printf("请输入数媒班级个数:");
scanf("%d", &shu);
for (i = 1; i <= 4; i++) // 4个年级
{
for (k = 1; k <= dian; k++) //创建电子
{
q = (struct GMC*)malloc(sizeGMC);
L = CreatGMC1(L,dian,q,i,k);
}
for (k = 1; k <= dian; k++) //创建计科
{
q = (struct GMC*)malloc(sizeGMC);
L = CreatGMC2(L,ji,q,i,k);
}
for (k = 1; k <= dian; k++) //创建数媒
{
q = (struct GMC*)malloc(sizeGMC);
L = CreatGMC3(L,shu,q,i,k);
}
}
while (1)
{
Mean1();
scanf("%d", &choice);
switch (choice)
{
case 1:
q1 = (struct Student *)malloc(sizeSTU);
L=Insert(L,q1);
break;
case 2:
Search(L);
break;
case 3:
printf("请输入要删除的学号:");
scanf("%d", &DelID);
Delete(L,DelID);
break;
case 4:
Print(L);
break;
case 5:
return 0;
default:
printf("输入错误");
}
}
return 0;
}