星危 2020-12-11 20:39 采纳率: 33.3%
浏览 18
已采纳

链表建立以及结构体的嵌套实例问题求解

 小白求解答

 也不知道是什么问题总是这个样子

#include <iostream>
#include <string>
using namespace std;
const int num=6;
typedef struct Date
{
	string name;
	int score;
}Date;
typedef struct Student
{
	Date *date;
	Student *next;
}Student;
Student * Created()
{
	Student *head,*node,*t;
	head=(Student *)malloc(sizeof(Student));
	head->date=(Date *)malloc(sizeof(Date));
	node=(Student *)malloc(sizeof(Student));
	node->date=(Date *)malloc(sizeof(Date));
	head->date=NULL;
	cout<<"第"<<num<<"个节点的数据"<<endl;
	cout<<"name:";
	cin>>node->date->name;
	cout<<"\nscore:";
	cin>>node->date->score;
	head->next=node;
	node->next =NULL;
	for(int i=num-1;i>0;i--)
	{
	node=(Student *)malloc(sizeof(Student));
	node->date=(Date *)malloc(sizeof(Date));
	cout<<"第"<<i<<"个节点的数据"<<endl;
	cout<<"name:";
	cin>>node->date->name;
	cout<<"\nscore:";
	cin>>node->date->score;
	node->next=head->next;
	head->next=node;
	}
	t=head->next;
	cout<<"遍历一遍"<<endl;
	while(t!=NULL)
	{
		cout<<"name: "<<t->date->name<<"score: "<<t->date->score<<endl;
		t=t->next;
	}
	return head;
}
void main()
{
	Student *s=Created();	
}
  • 写回答

3条回答 默认 最新

  • dd__db 2020-12-12 11:14
    关注
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    const int num=6;
    
    typedef struct Date
    {
    	int score;
    	char name[10];
    }Date;
    
    typedef struct Student
    {
    	Date *date;
    	Student *next;
    }Student;
    
    Student * Created()
    {
    	Student *head,*node,*t;
    	
    	head=(Student *)malloc(sizeof(Student));
    	head->date=(Date *)malloc(sizeof(Date));
    	
    	node=(Student *)malloc(sizeof(Student));
    	node->date=(Date *)malloc(sizeof(Date));
    	
    	head->date=NULL;
    	cout<<"第"<<num<<"个节点的数据"<<endl;
    	
    	cout<<"name:";
    	cin>>node->date->name;
    	cout <<node->date->name<< endl;
    	
    	cout<<"score:";
    	cin>>node->date->score;
    	cout <<node->date->score<< endl;
    	
    	head->next=node;
    	node->next =NULL;
    	
    	for(int i=num-1;i>0;i--)
    	{
        	node=(Student *)malloc(sizeof(Student));
        	node->date=(Date *)malloc(sizeof(Date));
        	
        	cout<<"第"<<i<<"个节点的数据"<<endl;
        	
        	cout<<"name:";
        	cin>>node->date->name;
        	cout <<node->date->name<< endl;
        	
        	cout<<"score:";
        	cin>>node->date->score;
        	cout <<node->date->score<< endl;
        	
        	node->next=head->next;
        	head->next=node;
    	}
    	
    	t=head->next;
    	cout<<"遍历一遍"<<endl;
    	
    	while(t!=NULL)
    	{
    		cout<<"name: "<<t->date->name<<"\tscore: "<<t->date->score<<endl;
    		t=t->next;
    	}
    	return head;
    }
    
    int main()
    {
    	Student *s=Created();	
    }
    

    输入:

    1
    20
    2
    30
    3
    40
    4
    50
    5
    60
    6
    70
     

    输出:

    第6个节点的数据
    name:1
    score:20
    第5个节点的数据
    name:2
    score:30
    第4个节点的数据
    name:3
    score:40
    第3个节点的数据
    name:4
    score:50
    第2个节点的数据
    name:5
    score:60
    第1个节点的数据
    name:6
    score:70
    遍历一遍
    name: 6    score: 70
    name: 5    score: 60
    name: 4    score: 50
    name: 3    score: 40
    name: 2    score: 30
    name: 1    score: 20

     

    好像是cin和String的问题我改成char name[10]了,顺便改了一下输出格式

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘