关于动态链表嵌套问题的解决方法及思路

链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。
场景:一个年级,相当链表A
该年级5个班,每个班5个人,相当于链表B1–B5
做一个学生成绩管理系统
学生成绩有语文 数学 英语
功能: 录入成绩 找最三科总分的最高分 最低分 算出平均分

思路:先创建链表B录入5个学生的各科成绩,然后返回链表头,将链表头给链表A的各个节点

#include <stdio.h>
#include <stdlib.h>
struct Class
{
	struct Person *top;
	struct Class *next;
};
struct Person
{
	double yuwen;
  	double shuxue;
   	double yingyu;
	struct Person *next;
};
struct Class *A = NULL;
struct Person *BinitNode(int i)
{
	int j;
    struct Person *B = NULL;
   	struct Person *new = NULL;
    	for(j=1;j<=5;j++){
			new = (struct Person *)malloc(sizeof(struct Person));
       		printf("请输入%d班的第%d个学生的语文成绩:",i,j);
        	scanf("%lf",&(new->yuwen));
        	if(new->yuwen < 0 || new->yuwen > 100){
				printf("输入错误,请重新输入:");
                scanf("%lf",(&new->yuwen));
        	}
        	printf("请输入%d班的第%d个学生的数学成绩:",i,j);
        	scanf("%lf",&(new->shuxue));
        	if(new->shuxue < 0 || new->shuxue > 100){
				printf("输入错误,请重新输入:");
        		scanf("%lf",(&new->shuxue));
        	}
			printf("请输入%d班的第%d个学生的英语成绩:",i,j);
        	scanf("%lf",&(new->yingyu));
        	if(new->yingyu < 0 || new->yingyu > 100){
				printf("输入错误,请重新输入:");
            	scanf("%lf",(&new->yingyu));
        	}
        	if(B == NULL){
				B = new;
        	}else{
				new->next = B;
            	B = new;
        	}
    	}
	return B;
}
void AinitNode()
{
	struct Class *new = NULL;
	int i;
	for(i=1;i<=5;i++){
		new = (struct Class *)malloc(sizeof(struct Class));
		new->top = BinitNode(i);
		if(A == NULL){
			A = new;
		}else{
			new->next = A;
			A = new;
		}
	} 
}
void printALL()
{
	double Mmax = A->top->shuxue;
	double Cmax = A->top->yuwen;
	double Emax = A->top->yingyu;
	double Mmin = A->top->shuxue;
	double Cmin = A->top->yuwen;
	double Emin = A->top->yingyu;
	double Msum = 0;
	double Csum = 0;
	double Esum = 0;
	double Mav = 0;
	double Cav = 0;
	double Eav = 0;
	double av = 0;
	while(A != NULL){
		while(A->top != NULL){
			if(A->top->next != NULL){
				if(Mmax < A->top->next->shuxue){
					Mmax = A->top->next->shuxue;
				}else{
					Mmin = A->top->next->shuxue;
				}
				if(Cmax < A->top->next->yuwen){
					Cmax = A->top->next->yuwen;
				}else{
					Cmin = A->top->next->yuwen;
				}
				if(Emax < A->top->next->yingyu){
					Emax = A->top->next->yingyu;
				}else{
					Emin = A->top->next->yingyu;
				}
			}
			Msum += A->top->shuxue;
			Csum += A->top->yuwen;
			Esum += A->top->yingyu;
			av++;
			A->top = A->top->next;
		}
		A = A->next;
	}
	Mav = Msum / av;
	Cav = Csum / av;
	Eav = Esum / av;
	printf("mathsMax = %.1lf,chineseMax = %.1lf,englishMax = %.1lf\n",Mmax,Cmax,Emax);
	printf("mathsMin = %.1lf,chineseMin = %.1lf,englishMin = %.1lf\n",Mmin,Cmin,Emin);
	printf("mathsAV = %.2lf,chineseAV = %.2lf,englishAV = %.2lf\n",Mav,Cav,Eav);
}
int main()
{
	AinitNode();
	printALL();
	return 0;
}
  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,约瑟夫问题是一个经典的数学问题,可以通过使用链表解决。下面是一个使用C++代码实现约瑟夫问题的设计思路: 1. 定义一个单向链表的结构体,包含两个成员变量:一个数据域和一个指向下一个节点的指针域。 ```c++ struct Node { int data; Node* next; }; ``` 2. 编写一个函数,用于创建一个含有n个节点的链表。在创建链表时,需要考虑每个节点的数据和下一个节点的指针。 ```c++ Node* createList(int n) { Node* head = new Node(); Node* p = head; for (int i = 1; i <= n; ++i) { Node* node = new Node(); node->data = i; p->next = node; p = p->next; } p->next = head->next; return head->next; } ``` 3. 编写一个函数,用于删除链表中的第m个节点。在删除节点时,需要修改前一个节点的指针,使其指向下一个节点。 ```c++ Node* removeNode(Node* head, int m) { Node* p = head; while (p->next != p) { for (int i = 1; i < m; ++i) { p = p->next; } Node* temp = p->next; p->next = temp->next; delete temp; p = p->next; } return p; } ``` 4. 在主函数中,调用上述函数,完成约瑟夫问题的求解。具体来说,我们可以先创建一个含有n个节点的链表,然后在循环中不断删除第m个节点,直到链表中只剩下一个节点为止。 ```c++ int main() { int n = 10; int m = 3; Node* head = createList(n); Node* result = removeNode(head, m); cout << "The last one is: " << result->data << endl; return 0; } ``` 这就是使用C++代码解决约瑟夫问题的基本思路。需要注意的是,在实际编写中,还需要考虑输入的合法性、内存的释放等问题
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值