//没有做完整
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
typedef struct student {
int num;
char name[20];
int score;
struct student *next;
}STU;
void add_new_node(STU** p_head, STU *new_node) {
system("cls");
STU *move = *p_head;
if (move == NULL) {
*p_head = new_node;
new_node->next = NULL;
}//添加新节点(学生)
else {
while(move->next != NULL) {
move = move->next;
}
move->next = new_node;
new_node->next = NULL;
}
}
void print_list(STU *head) {
printf("输入所有信息\n");
STU *move = head;//遍历链表(输出所有学生信息)
while(move != NULL) {
printf("%d %s %d\n", move->num, move->name,move->score);
move = move->next;
}
}
STU *search_num(STU *head, int num) {
STU *move = head;//根据学号查找学生信息
while (move != NULL) {
if (move ->num == num)
return move;
move = move->next;
}
}
STU *search_name(STU *head, char *s) {
STU *move = head;//根据名字查找学生信息
while (move != NULL) {
if (strcmp(move->name , s)==0)
return move;
move = move->next;
}
}
void delete_name(STU **p_head, char *s) {
if (*p_head == NULL) {//根据名字删除学生信息
printf("没有你要删的节点");
return ;
}
STU *move = *p_head;
STU *pf = NULL;
while (move -> next != NULL) {
if (move->next != NULL) {
break;
}
pf = move;
move = move->next;
}
if (move->name == s){
if (strcmp(move ->name , s)==0) {
*p_head = move->next;
}
else {
pf->next = move->next;
}
free(move);
}
else {
printf("未找到要删除的学生\n");
}
}
void delete_num(STU **p_head, int num) {
if (*p_head == NULL) {//根据学号删除学生信息
printf("没有你要删的节点");
return ;
}
STU * move = *p_head;
STU *pf = NULL;
while (move -> next != NULL) {
if (move->next != NULL) {
break;
}
pf = move;
move = move->next;
}
if (move->num == num){
if (move == *p_head) {
*p_head = move->next;
}
else {
pf->next = move->next;
}
free(move);
}
else {
printf("未找到要删除的学生\n");
}
}
void sort_num(STU *head) {
STU *i = NULL;
STU *j = NULL;
for (i = head; i != NULL; i = i->next) {
for (j = head; j != NULL; j = j -> next)
if (i ->num < j ->num) {
swap(i->num, j->num);
swap(i ->name , j->name);
swap(i ->score, j->score);
}
}
}
void sort_name(STU *head) {
STU *i = NULL;
STU *j = NULL;
for (i = head; i != NULL; i = i->next) {
for (j = head; j != NULL; j = j -> next)
if (i ->name > j ->name) {
swap(i->num, j->num);
swap(i ->name , j->name);
swap(i ->score, j->score);
}
}
}
//链表逆序
void link_reverse(STU** ppNode) {
STU* prev;
STU* cur;
if (*ppNode == NULL || ppNode == NULL) {
return ;
}
else {
cur = (*ppNode);
if (cur->next!=NULL) {
prev = cur;
cur = cur->next;
prev->next = NULL;
}
while (cur->next != NULL) {
STU *temp = prev;
prev = cur;
cur = cur->next;
prev->next = temp;
}
cur->next = prev;
(*ppNode) = cur;
}
}
void show_info() {
printf("选择你要操作的功能\n");
printf("输入:1.输入学生信息\n");
printf("输入:2.遍历学生\n");
printf("输入:3.按姓名找查\n") ;
printf("输入:4.按学号找查\n");
printf("输入:5.按学号删除\n");
printf("输入:6.按姓名删除\n");
printf("输入:7.按姓名排序\n");
printf("输入:8.按学号排序\n");
printf("输入:9.实现链表的逆序\n") ;
}
int main() {
system("color F2");
while (1) {
int arg;
show_info();
printf("请输入指令\n");
scanf("%d",&arg);
//头指针
STU *temp_head;
if (arg == 1) {
system("cls");
//1. 创建链表的功能
int count;
printf("请输入需要存储的学生数量\n");
scanf("%d",&count);
//链表的头部
STU *head = NULL;
//新学生
STU *new_stu = NULL;
for(int i = 0; i < count;i++){
// 为学生信息申请存储空间
new_stu = (STU*)malloc(sizeof(STU));
printf("请输入第%d个学生的学号,姓名,分数\n",i+1);
scanf("%d %s %d",&(new_stu->num),new_stu->name,&(new_stu->score));
//2.添加新学生的功能,将指向链表头部的指针和指向新学生结构体的指针传入
add_new_node(&head,new_stu);
temp_head = head;
}
}
else if (arg == 2) {
//3.遍历链表的功能
system("cls");
print_list(temp_head);
system("pause");
}
else if (arg == 3) {
//3.根据姓名循环查找学生的功能
system("cls");
while(1){
printf("输入要查找的姓名,输入“结束”以结束找查\n");
char name[20];
scanf("%s",name);
if (strcmp(name,"结束") == 0)
break;
STU* result = search_name(temp_head,name);
if(result != NULL){
printf("查找到的学生为:%d,%s,%d\n",result->num,result->name,result->score);
}else{
printf("您要找的学生不存在\n");
}
}
}
else if (arg == 4) {
system("cls");
//4.根据学号循环查找学生的功能
while(1){
printf("输入要查找的学号输入-1以结束找查\n");
int num;
scanf("%d",&num);
if (num == -1)
break;
STU* result = search_num(temp_head,num);
if(result != NULL){
printf("查找到的学生为:%d,%s,%d\n",result->num,result->name,result->score);
}else{
printf("您要找的学生不存在\n");
}
}
}
else if (arg == 5) {
system("cls");
//5.根据学号删除学生的功能
while(1){
printf("输入要删除学生的学号,输入-1以结束删除\n");
int num;
scanf("%d",&num);
if (num == -1)
break;
delete_num(&temp_head,num);
print_list(temp_head);
}
}
else if (arg == 6) {
system("cls");
//6.根据姓名删除学生的功能
while(1){
printf("输入要删除学生的姓名,输入“结束”以停止删除\n");
char name[20];
scanf("%s",name);
if (strcmp(name,"结束") == 0)
break;
delete_name(&temp_head,name);
print_list(temp_head);
}
}
else if (arg == 7) {
system("cls");
sort_name(temp_head);
print_list(temp_head);
system("pause");
}
else if (arg == 8) {
system("cls");
//link_reverse(&temp_head);
sort_num(temp_head);
print_list(temp_head);
system("pause");
}
else if (arg == 9) {
link_reverse(&temp_head);
print_list(temp_head);
}
}
return 0;
}