64,646
社区成员
发帖
与我相关
我的任务
分享
char name[16];
cin>>name
typedef struct{
string name; //这里有类类型的数据成员
int classid;
int age;
}Student;
#include<iostream>
#include "stdlib.h"
#include "string.h"
using namespace std;
#define F 0
#define T 1
typedef struct{
char name[16];
int classid;
int age;
}Student;
typedef struct Node{
Student stu;
struct Node *next;
}Node,*LinkList;
void initlist(LinkList H){
H=(LinkList)malloc(sizeof(Node));
H->next=NULL;
}//初始化单链表
void createlist(LinkList H){
LinkList n,r;
r=H;
char name[16];
int age,classid,flag=1;
while(flag){
cout<<"请输入姓名: ";
cin>>name;
cout<<"\n请输入年龄: ";
cin>>age;
cout<<"\n请输入学号: ";
cin>>classid;
n=(LinkList)malloc(sizeof(Node));
strcpy(n->stu.name,name);
n->stu.age=age;
n->stu.classid=classid;
r->next=n;
r=n;
r->next=NULL;
cout<<"\n请输入 1 继续 0 退出\n";
cin>>flag;
}
}//创建单链表
int main(){
LinkList h;
initlist(h);
createlist(h);
LinkList s=h->next;
while(s!=NULL){
cout<<s->stu.name<<" "<<s->stu.age<<" "<<s->stu.classid<<endl;
s=s->next;
}
return 0;
}