7,765
社区成员
发帖
与我相关
我的任务
分享
#include <STDLIB.H>
#include <STDIO.H>
#define OK 0
#define TRUE 1
typedef struct student
{
int num; //学号
char name[20]; //姓名
char sex; //性别
int age; //年龄
float score; //分数
char add[40]; //地址
};
typedef struct StudentNode
{
struct student* stu_data; //学生的数据信息指针
struct StudentNode* next; //指向下一个结点的指针
}StudentNode, *LinkStudentNode;
//创建具有n个学生信息的链表
int create_student_node_info(LinkStudentNode &stu_node, int n);
#include "student.h"
void case1(LinkStudentNode &stu_node)
{
int n;
printf("输入需要创建的学生信息的个数n:");
scanf("%d", &n);
create_student_node_info(stu_node, n);
}
void case2(LinkStudentNode stu_node)
{
LinkStudentNode p;
int i = 0;
p = stu_node->next;//初始化p,使其指向第一个结点
if(p)
{
//如果p不为NULL
//打印其数据
printf("第%d个学生信息:\n", i + 1);
printf("学号:%d", p->stu_data->num);
printf("姓名:%s", p->stu_data->name);
printf("性别:%c", p->stu_data->sex);
printf("年龄:%d", p->stu_data->age);
printf("分数:%f", p->stu_data->score);
printf("地址:%s", p->stu_data->add);
p = p->next;
i++;
}
}
void main()
{
int choice;
LinkStudentNode stu_node;
printf("1:创建具有n个学生信息的链表\n");
printf("2:打印所有学生信息\n");
printf("0:退出\n");
while(TRUE)
{
printf("请输入您的选择:");
scanf("%d", &choice);
switch(choice)
{
case 0:exit(0);break;
case 1:case1(stu_node);break;
case 2:case2(stu_node);break;
}
}
getchar();
}
//创建具有n个学生信息的链表
int create_student_node_info(LinkStudentNode &stu_node, int n)
{
int i, nu;
LinkStudentNode p;
//给头指针stu_node分配内存空间
stu_node = (LinkStudentNode)malloc(sizeof(StudentNode));
stu_node->next = NULL; //将头结点指向NULL,建立带头结点的链表
for(i = n; i > 0; --i)
{
p = (LinkStudentNode)malloc(sizeof(StudentNode));//生成新结点
printf("输入一个学生信息\n");
//输入元素信息
printf("学号:");
scanf("%d", &p->stu_data->num);
printf("\n姓名:");
scanf("%s", p->stu_data->name);
printf("\n性别:");//注意此处性别是char型,默认其输入M为男,W为女吧
scanf("%c", &p->stu_data->sex);
printf("\n年龄:");
scanf("%d", &p->stu_data->age);
printf("\n分数:");
scanf("%f", &p->stu_data->score);
printf("\n地址:");
scanf("%s", p->stu_data->add);
//将当前指针指向NULL
p->next = stu_node->next;
//将当前结点插入到链表中
stu_node->next = p;
}
return OK;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
int num;
}StudentStruct, *LinkStudentStruct;
typedef struct StudentNode
{
LinkStudentStruct stu_data;
struct StudentNode* next;
}StudentNode, *LinkStudentNode;
int _tmain(int argc, _TCHAR* argv[])
{
LinkStudentNode p;
p = (LinkStudentNode)malloc(sizeof(StudentNode));
p->stu_data = (LinkStudentStruct)malloc(sizeof(StudentStruct));
scanf("%d", &p->stu_data->num);
printf("Input value is: %d\n",p->stu_data->num);
free(p);
return 0;
}